diff --git a/src/gloat.gleam b/src/gloat.gleam index 1cd55e9..f277c76 100644 --- a/src/gloat.gleam +++ b/src/gloat.gleam @@ -6,6 +6,7 @@ import simplifile import solve_2024_01 import solve_2024_02 import solve_2025_01 +import solve_2025_02 pub fn main() -> Nil { case argv.load().arguments { @@ -16,6 +17,8 @@ pub fn main() -> Nil { ["2024", day] -> io.print("No solution for 2024 day " <> day) ["2025", "1"] -> display(#(2025, 1), solve_2025_01.part_one, solve_2025_01.part_two) + ["2025", "2"] -> + display(#(2025, 2), solve_2025_02.part_one, solve_2025_02.part_one) ["2025", _] -> io.print("No solutions for 2025") _ -> io.print("Please provide the following arguments: ") } diff --git a/src/solve_2025_01.gleam b/src/solve_2025_01.gleam index bda4e4f..b2abbe1 100644 --- a/src/solve_2025_01.gleam +++ b/src/solve_2025_01.gleam @@ -24,7 +24,7 @@ pub fn part_two(input: String) -> Int { let #(from, to) = pair list.range(from, to) |> list.drop(1) - |> echo + // |> echo |> list.count(fn(position) { position % 100 == 0 }) |> int.add(accumulator) }) diff --git a/src/solve_2025_02.gleam b/src/solve_2025_02.gleam new file mode 100644 index 0000000..1780b13 --- /dev/null +++ b/src/solve_2025_02.gleam @@ -0,0 +1,27 @@ +import gleam/int +import gleam/list +import gleam/string + +pub fn part_one(input: String) -> Int { + use accumulator, x <- list.fold(parse(input), 0) + let string = int.to_string(x) + + case string.length(string) { + length if length % 2 == 0 -> { + let half = length / 2 + case string.drop_start(string, half) == string.drop_end(string, half) { + True -> accumulator + x + False -> accumulator + } + } + _ -> accumulator + } +} + +fn parse(input: String) -> List(Int) { + use line <- list.flat_map(string.split(input, ",")) + case list.map(string.split(line, "-"), int.parse) { + [Ok(left), Ok(right)] -> list.range(left, right) + _ -> [] + } +} diff --git a/test/test_2025_02.gleam b/test/test_2025_02.gleam new file mode 100644 index 0000000..3dca273 --- /dev/null +++ b/test/test_2025_02.gleam @@ -0,0 +1,14 @@ +import gleam/string +import solve_2025_02 + +const input = " +11-22,95-115,998-1012,1188511880-1188511890,222220-222224,1698522-1698528,446443-446449,38593856-38593862,565653-565659,824824821-824824827,2121212118-2121212124 +" + +pub fn part_one_test() { + let result = + input + |> string.trim + |> solve_2025_02.part_one + assert result == 1_227_775_554 +}