my aoc solutions
0
fork

Configure Feed

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

day 5 pt 2

nnuuvv c6535e49 77ea117f

+67 -1
+44 -1
src/aoc_2025/day_5.gleam
··· 55 55 } 56 56 57 57 pub fn pt_2(input: #(List(#(Int, Int)), List(Int))) { 58 - todo as "part 2 not implemented" 58 + let #(ranges, _) = input 59 + 60 + let sum = 61 + ranges 62 + // |> echo 63 + |> list.fold([], fn(processed, range) { pt_2_loop(range, processed, []) }) 64 + |> list.fold(0, fn(sum, range) { 65 + case range { 66 + #(0, 0) -> sum 67 + #(start, end) -> { 68 + sum + { { end - start } + 1 } 69 + } 70 + } 71 + }) 72 + 73 + sum 74 + } 75 + 76 + pub fn pt_2_loop(range, processed_ranges: List(#(Int, Int)), acc) { 77 + let #(start, end) = range 78 + 79 + case processed_ranges { 80 + // we've gone through all of them 81 + // add range to acc for next round of processing 82 + [] -> [range, ..acc] 83 + // fully fits into a different range, we are done 84 + [p_range, ..rest] if p_range.0 <= start && p_range.1 >= end -> 85 + list.append([p_range, ..acc], rest) 86 + 87 + // already processed one fits fully 88 + [p_range, ..rest] if p_range.0 >= start && p_range.1 <= end -> 89 + pt_2_loop(range, rest, acc) 90 + 91 + // merge at front 92 + [#(p_start, p_end), ..rest] if start >= p_start && start <= p_end -> 93 + pt_2_loop(#(p_start, end), rest, acc) 94 + 95 + // merge at end 96 + [#(p_start, p_end), ..rest] if end >= p_start && end <= p_end -> 97 + pt_2_loop(#(start, p_end), rest, acc) 98 + 99 + // keep the unmatched 100 + [unmatched, ..rest] -> pt_2_loop(range, rest, [unmatched, ..acc]) 101 + } 59 102 }
+23
test/advent_of_code_test.gleam
··· 1 1 import aoc_2025/day_3 2 + import aoc_2025/day_5 2 3 import gleam/int 4 + import gleam/list 3 5 import gleeunit 4 6 import gleeunit/should 5 7 ··· 39 41 let answer = day_3.pt_2([bank]) 40 42 should.equal(answer, 444_444_444_444) 41 43 } 44 + 45 + pub fn day_5_pt_2_icky_test() { 46 + [ 47 + // overlap right 48 + [#(1, 5), #(3, 7)], 49 + // fully fits 50 + [#(3, 5), #(1, 7)], 51 + [#(1, 7), #(3, 5)], 52 + // overlap left 53 + [#(3, 7), #(1, 5)], 54 + 55 + [#(1, 3), #(5, 7), #(2, 6)], 56 + 57 + [#(1, 3), #(5, 7), #(2, 6)], 58 + [#(1, 3), #(4, 4), #(5, 7)], 59 + ] 60 + |> list.map(fn(in) { 61 + echo in 62 + assert day_5.pt_2(#(in, [])) == 7 63 + }) 64 + }