+15
-43
day1/day1.gleam
+15
-43
day1/day1.gleam
···
14
14
|> string.trim
15
15
|> string.split("\n")
16
16
17
-
let part1 =
18
-
list.fold(input, #(50, 0), fn(acc, x) {
17
+
let result =
18
+
list.fold(input, #(50, 0, 0), fn(acc, x) {
19
19
case x {
20
20
"L" <> n -> {
21
-
do_the_thing(n, acc, int.subtract)
21
+
do_the_thing(n, acc, int.subtract, fn(a, b) { { 100 - a } % 100 + b })
22
22
}
23
23
"R" <> n -> {
24
-
do_the_thing(n, acc, int.add)
24
+
do_the_thing(n, acc, int.add, int.add)
25
25
}
26
26
_ -> panic as "what the helly"
27
27
}
28
28
})
29
29
30
-
let part2 =
31
-
list.fold(input, #(50, 0), fn(acc, x) {
32
-
case x {
33
-
"L" <> n -> {
34
-
do_the_thing2(n, acc, int.subtract, fn(a, b) {
35
-
case a {
36
-
0 -> a + b
37
-
_ -> 100 - a + b
38
-
}
39
-
})
40
-
}
41
-
"R" <> n -> {
42
-
do_the_thing2(n, acc, int.add, int.add)
43
-
}
44
-
_ -> panic as "what the helly"
45
-
}
46
-
})
47
-
48
-
io.println("Part 1: " <> int.to_string(part1.1))
49
-
io.println("Part 2: " <> int.to_string(part2.1))
30
+
io.println("Part 1: " <> int.to_string(result.1))
31
+
io.println("Part 2: " <> int.to_string(result.2))
50
32
}
51
33
52
34
fn do_the_thing(
53
35
amount: String,
54
-
state: #(Int, Int),
36
+
state: #(Int, Int, Int),
55
37
operation: fn(Int, Int) -> Int,
56
-
) -> #(Int, Int) {
38
+
p2_operation: fn(Int, Int) -> Int,
39
+
) -> #(Int, Int, Int) {
57
40
let assert Ok(n) = int.parse(amount)
58
-
let assert Ok(num) = operation(state.0, n) |> int.modulo(100)
59
-
let password = case num {
41
+
let assert Ok(pos) = operation(state.0, n) |> int.modulo(100)
42
+
let part1 = case pos {
60
43
0 -> state.1 + 1
61
44
_ -> state.1
62
45
}
63
-
#(num, password)
64
-
}
65
-
66
-
fn do_the_thing2(
67
-
amount: String,
68
-
state: #(Int, Int),
69
-
operation: fn(Int, Int) -> Int,
70
-
pass_operation: fn(Int, Int) -> Int,
71
-
) -> #(Int, Int) {
72
-
let assert Ok(n) = int.parse(amount)
73
-
let assert Ok(num) = operation(state.0, n) |> int.modulo(100)
74
-
let password =
75
-
state.1
46
+
let part2 =
47
+
state.2
76
48
+ {
77
-
pass_operation(state.0, n)
49
+
p2_operation(state.0, n)
78
50
|> int.divide(100)
79
51
|> result.unwrap(0)
80
52
}
81
-
#(num, password)
53
+
#(pos, part1, part2)
82
54
}