diff --git a/src/gloat.gleam b/src/gloat.gleam index 16f1ce1..0f7fd78 100644 --- a/src/gloat.gleam +++ b/src/gloat.gleam @@ -13,6 +13,7 @@ import solve_2025_05 import solve_2025_06 import solve_2025_07 import solve_2025_08 +import solve_2025_09 pub fn main() -> Nil { case argv.load().arguments { @@ -37,6 +38,8 @@ pub fn main() -> Nil { display(#(2025, 7), solve_2025_07.part_one, solve_2025_07.part_two) ["2025", "8"] -> display(#(2025, 8), solve_2025_08.part_one, solve_2025_08.part_two) + ["2025", "9"] -> + display(#(2025, 9), solve_2025_09.part_one, solve_2025_09.part_two) ["2025", _] -> io.print("No solutions for 2025") _ -> io.print("Please provide the following arguments: ") } diff --git a/src/solve_2025_09.gleam b/src/solve_2025_09.gleam new file mode 100644 index 0000000..077faa7 --- /dev/null +++ b/src/solve_2025_09.gleam @@ -0,0 +1,54 @@ +import gleam/int +import gleam/list +import gleam/result +import gleam/string + +type Tile { + Tile(x: Int, y: Int) +} + +type Rectangle { + Rectangle(south_west: Tile, north_east: Tile, area: Int) +} + +fn area(south_west: Tile, north_east: Tile) { + let dx = north_east.x - south_west.x |> int.absolute_value |> int.add(1) + let dy = north_east.y - south_west.y |> int.absolute_value |> int.add(1) + dx * dy +} + +fn compare(left: Rectangle, right: Rectangle) { + int.compare(left.area, right.area) +} + +pub fn part_one(input: String) -> Int { + input + |> parse + |> list.combination_pairs + |> list.map(fn(pair) { + let #(south_west, north_east) = pair + let area = area(south_west, north_east) + Rectangle(south_west:, north_east:, area:) + }) + |> list.sort(compare) + // |> echo + |> list.reverse + |> list.map(fn(rectangle) { rectangle.area }) + |> list.first + |> result.unwrap(-1) +} + +pub fn part_two(input: String) -> Int { + // echo parse(input) + -1 +} + +fn parse(input: String) -> List(Tile) { + use line <- list.flat_map(input |> string.trim |> string.split("\n")) + case + line |> string.split(",") |> list.map(string.trim) |> list.map(int.parse) + { + [Ok(x), Ok(y)] -> [Tile(x:, y:)] + _ -> [] |> echo |> list.take(0) + } +} diff --git a/test/test_2025_09.gleam b/test/test_2025_09.gleam new file mode 100644 index 0000000..29ac6aa --- /dev/null +++ b/test/test_2025_09.gleam @@ -0,0 +1,29 @@ +import gleam/string +import solve_2025_09 + +const input = " +7,1 +11,1 +11,7 +9,7 +9,5 +2,5 +2,3 +7,3 +" + +pub fn part_one_test() { + let result = + input + |> string.trim + |> solve_2025_09.part_one + assert result == 50 +} + +pub fn part_two_test() { + let result = + input + |> string.trim + |> solve_2025_09.part_two + assert result == -1 +}