advent of code 2025 solutions in Gleam
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

day07: solve part 2

+78 -17
+56 -17
src/day07.gleam
··· 5 5 import utils 6 6 7 7 pub type Tile { 8 - Takyon 8 + Takyon(Int) 9 9 Splitter 10 10 ActivatedSplitter 11 11 Empty ··· 26 26 case col { 27 27 "^" -> #(#(x, y), Splitter) 28 28 "." -> #(#(x, y), Empty) 29 - "S" -> #(#(x, y), Takyon) 29 + "S" -> #(#(x, y), Takyon(1)) 30 30 _ -> panic as "Invalid character" 31 31 } 32 32 }) ··· 38 38 pub fn process_row(diagram: Diagram, row: Int) -> Diagram { 39 39 let current_takyons = 40 40 diagram 41 - |> dict.filter(fn(key, value) { key.1 == row && value == Takyon }) 42 - |> dict.keys 41 + |> dict.filter(fn(key, value) { 42 + key.1 == row 43 + && case value { 44 + Takyon(_) -> True 45 + _ -> False 46 + } 47 + }) 48 + |> dict.to_list 43 49 44 50 process_row_loop(diagram, current_takyons) 45 51 } 46 52 47 - pub fn process_row_loop(diagram: Diagram, takyons: List(#(Int, Int))) { 53 + pub fn add_takyon(diagram: Diagram, takyon: Tile, at coord: Coord) -> Diagram { 54 + let assert Takyon(new_takyons) = takyon 55 + case { dict.get(diagram, coord) } { 56 + Ok(Empty) -> dict.insert(diagram, for: coord, insert: takyon) 57 + Ok(Takyon(existing_takyons)) -> 58 + dict.insert( 59 + diagram, 60 + for: coord, 61 + insert: Takyon(new_takyons + existing_takyons), 62 + ) 63 + _ -> panic as "I don't think this ever happens" 64 + } 65 + } 66 + 67 + pub fn process_row_loop(diagram: Diagram, takyons: List(#(Coord, Tile))) { 48 68 case takyons { 49 69 [takyon, ..rest] -> { 50 - let death_yon = #(takyon.0, takyon.1 + 1) 70 + let death_yon = #(takyon.0.0, takyon.0.1 + 1) 71 + 51 72 case dict.get(diagram, death_yon) { 52 73 Ok(Empty) -> { 53 74 process_row_loop( 54 - dict.insert(diagram, for: death_yon, insert: Takyon), 75 + dict.insert(diagram, for: death_yon, insert: takyon.1), 55 76 rest, 56 77 ) 57 78 } ··· 59 80 process_row_loop( 60 81 diagram 61 82 |> dict.insert(for: death_yon, insert: ActivatedSplitter) 62 - |> dict.insert( 63 - for: #(death_yon.0 - 1, death_yon.1), 64 - insert: Takyon, 65 - ) 66 - |> dict.insert( 67 - for: #(death_yon.0 + 1, death_yon.1), 68 - insert: Takyon, 69 - ), 83 + |> add_takyon(takyon.1, #(death_yon.0 - 1, death_yon.1)) 84 + |> add_takyon(takyon.1, #(death_yon.0 + 1, death_yon.1)), 85 + rest, 86 + ) 87 + } 88 + Ok(Takyon(_)) -> { 89 + process_row_loop( 90 + diagram 91 + |> add_takyon(takyon.1, death_yon), 70 92 rest, 71 93 ) 72 94 } ··· 98 120 |> list.count(where: fn(x) { x == ActivatedSplitter }) 99 121 } 100 122 101 - pub fn part2(_input) { 102 - todo 123 + pub fn part2(input) { 124 + let diagram = build_diagram(input) 125 + let assert Ok(n_rows) = 126 + diagram 127 + |> dict.keys 128 + |> list.map(fn(x) { x.1 }) 129 + |> list.max(with: int.compare) 130 + 131 + let complete_diagram = oh_shit_im_feeling_it(diagram, list.range(0, n_rows)) 132 + complete_diagram 133 + |> dict.filter(keeping: fn(key, _) { key.1 == n_rows }) 134 + |> dict.values 135 + |> list.map(with: fn(val) { 136 + case val { 137 + Takyon(n) -> n 138 + _ -> 0 139 + } 140 + }) 141 + |> int.sum 103 142 } 104 143 105 144 pub fn solve() {
+22
test/aoc2025_test.gleam
··· 75 75 76 76 assert day07.part1(input) == 21 77 77 } 78 + 79 + pub fn day07_part2_test() { 80 + let input = 81 + ".......S....... 82 + ............... 83 + .......^....... 84 + ............... 85 + ......^.^...... 86 + ............... 87 + .....^.^.^..... 88 + ............... 89 + ....^.^...^.... 90 + ............... 91 + ...^.^...^.^... 92 + ............... 93 + ..^...^.....^.. 94 + ............... 95 + .^.^.^.^.^...^. 96 + ..............." 97 + 98 + assert day07.part2(input) == 40 99 + }