diff --git a/src/gloat.gleam b/src/gloat.gleam index 9beef03..1cd55e9 100644 --- a/src/gloat.gleam +++ b/src/gloat.gleam @@ -5,6 +5,7 @@ import gleam/string import simplifile import solve_2024_01 import solve_2024_02 +import solve_2025_01 pub fn main() -> Nil { case argv.load().arguments { @@ -13,6 +14,8 @@ pub fn main() -> Nil { ["2024", "2"] -> display(#(2024, 2), solve_2024_02.part_one, solve_2024_02.part_two) ["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", _] -> 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 new file mode 100644 index 0000000..0c7ce91 --- /dev/null +++ b/src/solve_2025_01.gleam @@ -0,0 +1,54 @@ +import gleam/int +import gleam/list +import gleam/pair +import gleam/result +import gleam/string + +pub fn part_one(input: String) -> Int { + input + |> parse + |> count_zeros + |> pair.second +} + +pub fn part_two(input: String) -> Int { + let rotations = parse(input) + let #(_, seen) = { + use accumulator, rotation <- list.fold(rotations, #(50, 0)) + let #(previous, seen) = accumulator + echo #(previous, seen, rotation, rotation / 100) + // const via_zero = ??? + #({ previous + rotation } % 100, seen + { rotation / 100 }) + } + + seen +} + +fn count_zeros(numbers: List(Int)) -> #(Int, Int) { + use accumulator, rotation <- list.fold(numbers, #(50, 0)) + let #(previous, seen) = accumulator + case { previous + rotation } % 100 { + 0 -> #(0, seen + 1) + next -> #(next, seen) + } +} + +fn parse(input: String) -> List(Int) { + use line <- list.flat_map(string.split(input, "\n")) + let value = case line { + "L" <> rotations -> int.parse(rotations) |> result.map(int.negate) + "R" <> rotations -> int.parse(rotations) + _ -> Error(Nil) + } + case value { + Ok(v) -> [v] + Error(_) -> [] + } +} + +fn rotate(value: Int, cycle: Int) -> #(Int, Int) { + case value % cycle { + value if value < 0 -> #(value % cycle, value / cycle) + _ -> #(value, 0) + } +} diff --git a/test/test_2025_01.gleam b/test/test_2025_01.gleam new file mode 100644 index 0000000..f9c49dc --- /dev/null +++ b/test/test_2025_01.gleam @@ -0,0 +1,30 @@ +import gleam/string +import solve_2025_01 + +const input = " +L68 +L30 +R48 +L5 +R60 +L55 +L1 +L99 +R14 +L82 +" + +pub fn part_one_test() { + let result = + input + |> string.trim + |> solve_2025_01.part_one + assert result == 3 +} +// pub fn part_two_test() { +// let result = +// input +// |> string.trim +// |> solve_2025_01.part_two +// assert result == 6 +// }