From 4122946ece9f6d2b40c591dae6fadbcdaa4dbf64 Mon Sep 17 00:00:00 2001 From: Max Duval Date: Wed, 3 Dec 2025 17:48:08 +0000 Subject: [PATCH] feat: day 3 part 2 joltage working for test but not actually working --- src/solve_2025_03.gleam | 59 ++++++++++++++++++++++++++++++++++++++++- test/test_2025_03.gleam | 2 +- 2 files changed, 59 insertions(+), 2 deletions(-) diff --git a/src/solve_2025_03.gleam b/src/solve_2025_03.gleam index a702a08..11ada05 100644 --- a/src/solve_2025_03.gleam +++ b/src/solve_2025_03.gleam @@ -1,5 +1,6 @@ import gleam/int import gleam/list +import gleam/order import gleam/string pub fn part_one(input: String) -> Int { @@ -19,8 +20,64 @@ pub fn part_one(input: String) -> Int { accumulator + tens * 10 + ones } +type Battery { + Battery(joltage: Int, index: Int) +} + pub fn part_two(input: String) -> Int { - -1 + use accumulator, digits <- list.fold(parse(input), 0) + + let sorted = + digits + |> list.index_map(Battery) + |> list.sort(fn(left, right) { + order.break_tie( + in: int.compare(right.joltage, left.joltage), + with: int.compare(left.index, right.index), + ) + }) + // |> echo + + let added = + filler([], sorted, 12, list.length(sorted)) + |> echo + |> list.fold(0, fn(accumulator, battery) { + accumulator * 10 + battery.joltage + }) + |> echo + + accumulator + added +} + +fn filler( + so_far: List(Battery), + left_to_check: List(Battery), + banks_to_fill: Int, + length: Int, +) -> List(Battery) { + case banks_to_fill { + 0 -> so_far + _ -> { + case + left_to_check + |> list.find(fn(battery) { battery.index + banks_to_fill <= length }) + { + Ok(battery) -> { + let left_to_check = + list.filter(left_to_check, fn(b) { b.index > battery.index }) + echo #(battery, banks_to_fill - 1, length, so_far) + echo left_to_check + filler( + list.append(so_far, [battery]), + left_to_check, + banks_to_fill - 1, + length, + ) + } + Error(_) -> [] + } + } + } } fn parse(input: String) -> List(List(Int)) { diff --git a/test/test_2025_03.gleam b/test/test_2025_03.gleam index 569a5a9..e749556 100644 --- a/test/test_2025_03.gleam +++ b/test/test_2025_03.gleam @@ -21,5 +21,5 @@ pub fn part_two_test() { input |> string.trim |> solve_2025_03.part_two - assert result == -1 + assert result == 3_121_910_778_619 } -- 2.51.2