Skip to content

Commit 7fa80a4

Browse files
committed
🖥 See why their computers are having issues again
1 parent 4c8e42f commit 7fa80a4

File tree

3 files changed

+54
-1
lines changed

3 files changed

+54
-1
lines changed

‎src/aoc24.gleam

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ import gleam/io
33

44
import day01
55
import day02
6+
import day03
67

78
pub fn main() {
89
case argv.load().arguments {
910
["1"] -> day01.main()
1011
["2"] -> day02.main()
12+
["3"] -> day03.main()
1113
_ -> io.println("No day specified. Usage: aoc24 <day>")
1214
}
1315
}

‎src/day03.gleam

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import gleam/int
2+
import gleam/io
3+
import gleam/list
4+
import gleam/string
5+
6+
import shared
7+
8+
pub fn main() {
9+
let memory =
10+
shared.read_input([])
11+
|> string.join("")
12+
io.println("Part 1: " <> int.to_string(part_1(memory)))
13+
io.println("Part 2: " <> int.to_string(part_2(memory)))
14+
}
15+
16+
fn part_1(memory: String) -> Int {
17+
memory
18+
|> string.split("mul(")
19+
|> list.fold([], fn(acc, operands) {
20+
list.flatten([acc, string.split(operands, ")")])
21+
})
22+
|> list.map(mul)
23+
|> list.fold(0, fn(acc, value) { acc + value })
24+
}
25+
26+
fn mul(operands: String) -> Int {
27+
operands
28+
|> string.split(",")
29+
|> list.map(int.parse)
30+
|> list.fold(1, fn(acc, operand) {
31+
case operand {
32+
Ok(value) -> acc * value
33+
Error(_) -> 0
34+
}
35+
})
36+
}
37+
38+
fn part_2(memory: String) -> Int {
39+
memory
40+
|> string.split("do()")
41+
|> list.fold([], fn(acc, expressions) {
42+
case string.split(expressions, "don't()") {
43+
[] -> acc
44+
["", ..] -> acc
45+
[expressions, ..] -> list.flatten([acc, [expressions]])
46+
}
47+
})
48+
|> string.join("")
49+
|> part_1()
50+
}

‎src/shared.gleam

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import gleam/erlang
22
import gleam/int
3+
import gleam/list
34
import gleam/string
45

56
pub fn read_input(lines: List(String)) -> List(String) {
@@ -10,7 +11,7 @@ pub fn read_input(lines: List(String)) -> List(String) {
1011
[line, ..lines]
1112
|> read_input()
1213
}
13-
Error(_) -> lines
14+
Error(_) -> list.reverse(lines)
1415
}
1516
}
1617

0 commit comments

Comments
 (0)