+34
-3
day6/day6.gleam
+34
-3
day6/day6.gleam
···
1
1
import gleam/int
2
2
import gleam/io
3
3
import gleam/list
4
+
import gleam/pair
4
5
import gleam/result
5
6
import gleam/string
6
7
import simplifile
7
8
8
9
pub fn main() {
9
-
let assert Ok(raw_input) = simplifile.read("src/day6/test.txt")
10
+
let assert Ok(raw_input) = simplifile.read("src/day6/input.txt")
10
11
11
-
let input =
12
+
let input_p1 =
12
13
string.trim(raw_input)
13
14
|> string.split("\n")
14
15
|> list.map(fn(line) {
···
17
18
|> list.transpose
18
19
19
20
let part1 =
20
-
list.map(input, fn(col) {
21
+
list.map(input_p1, fn(col) {
21
22
case list.reverse(col) {
22
23
["*", ..rest] ->
23
24
list.fold(rest, 1, fn(acc, x) {
···
33
34
|> int.sum
34
35
35
36
io.println("Part 1: " <> int.to_string(part1))
37
+
38
+
let assert [o, ..ns] =
39
+
string.trim(raw_input)
40
+
|> string.split("\n")
41
+
|> list.reverse
42
+
43
+
let ops =
44
+
string.split(o, " ")
45
+
|> list.filter(fn(e) { e != "" })
46
+
47
+
let part2 =
48
+
list.reverse(ns)
49
+
|> list.map(string.to_graphemes)
50
+
|> list.transpose
51
+
|> list.map(fn(x) { string.join(x, "") |> string.trim |> int.parse })
52
+
|> list.fold(#([], ops), fn(acc, x) {
53
+
case x, acc {
54
+
Ok(n), #([], ["*", ..]) -> #([1 * n], acc.1)
55
+
Ok(n), #([], ["+", ..]) -> #([0 + n], acc.1)
56
+
Ok(n), #([l, ..rest], ["*", ..]) -> #([l * n, ..rest], acc.1)
57
+
Ok(n), #([l, ..rest], ["+", ..]) -> #([l + n, ..rest], acc.1)
58
+
Error(Nil), #(x, [_, "*", ..rest]) -> #([1, ..x], ["*", ..rest])
59
+
Error(Nil), #(x, [_, "+", ..rest]) -> #([0, ..x], ["+", ..rest])
60
+
_, _ -> panic
61
+
}
62
+
})
63
+
|> pair.first
64
+
|> int.sum
65
+
66
+
io.println("Part 2: " <> int.to_string(part2))
36
67
}