+53
-3
src/aoc_2025/day_7.gleam
+53
-3
src/aoc_2025/day_7.gleam
···
1
1
import gleam/dict
2
2
import gleam/list
3
+
import gleam/option.{None, Some}
3
4
import gleam/pair
4
5
import gleam/set
5
6
import gleam/string
6
7
7
-
pub fn pt_1(input: String) {
8
+
pub fn parse(input: String) -> List(List(String)) {
8
9
input
9
10
// rows
10
11
|> string.split("\n")
11
12
// rows as chars
12
13
|> list.map(string.to_graphemes)
14
+
}
15
+
16
+
pub fn pt_1(input: List(List(String))) {
17
+
input
13
18
|> list.fold(#(0, set.new()), fn(acc, row) {
14
19
let #(split_sum, columns_with_beam) = acc
15
20
···
42
47
|> pair.first()
43
48
}
44
49
45
-
pub fn pt_2(input: String) {
46
-
todo as "part 2 not implemented"
50
+
pub fn pt_2(input: List(List(String))) {
51
+
input
52
+
|> list.fold(dict.new(), fn(columns_with_beam, row) {
53
+
let columns_with_beam =
54
+
list.index_fold(
55
+
row,
56
+
columns_with_beam,
57
+
fn(columns_with_beam, item, column) {
58
+
case item {
59
+
"S" -> columns_with_beam |> dict.upsert(column, add(_, 1))
60
+
"^" -> {
61
+
case dict.get(columns_with_beam, column) {
62
+
Ok(count) if count > 0 -> {
63
+
// echo count
64
+
let columns_with_beam =
65
+
columns_with_beam
66
+
|> dict.delete(column)
67
+
|> dict.upsert(column - 1, add(_, count))
68
+
|> dict.upsert(column + 1, add(_, count))
69
+
70
+
columns_with_beam
71
+
}
72
+
73
+
_ -> columns_with_beam
74
+
}
75
+
}
76
+
// "."
77
+
_ -> columns_with_beam
78
+
}
79
+
},
80
+
)
81
+
82
+
columns_with_beam
83
+
})
84
+
|> dict.fold(0, fn(acc, _key, value) { acc + value })
85
+
}
86
+
87
+
fn add(item: option.Option(Int), amount) -> Int {
88
+
let amount = case item {
89
+
Some(count) -> count + amount
90
+
None -> amount
91
+
}
92
+
93
+
case amount {
94
+
amount if amount < 0 -> 0
95
+
_ -> amount
96
+
}
47
97
}