diff options
author | Hunky Jimpjorps <thechairman@thechairman.info> | 2024-02-02 17:05:12 -0500 |
---|---|---|
committer | Hunky Jimpjorps <thechairman@thechairman.info> | 2024-02-02 17:05:12 -0500 |
commit | 48e35ad3b0b0c62f936784e4aca70b17c3b0e3f9 (patch) | |
tree | f59a13e0b5e80ab925220b4488c6e36b1bec660a /aoc2023-gleam/src/day11/solve.gleam | |
parent | 87e9ab25ff70e215b537939a4bc23ab101f41dbe (diff) | |
download | gleam_aoc-48e35ad3b0b0c62f936784e4aca70b17c3b0e3f9.tar.gz gleam_aoc-48e35ad3b0b0c62f936784e4aca70b17c3b0e3f9.zip |
renaming
Diffstat (limited to 'aoc2023-gleam/src/day11/solve.gleam')
-rw-r--r-- | aoc2023-gleam/src/day11/solve.gleam | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/aoc2023-gleam/src/day11/solve.gleam b/aoc2023-gleam/src/day11/solve.gleam new file mode 100644 index 0000000..35464a1 --- /dev/null +++ b/aoc2023-gleam/src/day11/solve.gleam @@ -0,0 +1,84 @@ +import adglent.{First, Second} +import gleam/io +import gleam/int +import gleam/string +import gleam/list + +type Posn { + Posn(x: Int, y: Int) +} + +fn find_empty(grid: List(List(String))) { + use acc, row, r <- list.index_fold(grid, []) + case list.unique(row) { + ["."] -> [r, ..acc] + _ -> acc + } +} + +fn count_prior_empty_ranks(rank: Int, empty_ranks: List(Int)) -> Int { + empty_ranks + |> list.drop_while(fn(r_empty) { r_empty > rank }) + |> list.length +} + +fn parse_with_expansion(input: String, expansion: Int) -> List(Posn) { + let add = expansion - 1 + let grid = + input + |> string.split("\n") + |> list.map(string.to_graphemes) + + let empty_row_list = find_empty(grid) + let empty_col_list = find_empty(list.transpose(grid)) + + { + use r, row <- list.index_map(grid) + use acc, cell, c <- list.index_fold(over: row, from: []) + + let p = Posn(r, c) + let empty_r = count_prior_empty_ranks(r, empty_row_list) + let empty_c = count_prior_empty_ranks(c, empty_col_list) + case cell { + "#" -> [Posn(p.x + empty_r * add, p.y + empty_c * add), ..acc] + _empty -> acc + } + } + |> list.flatten() +} + +fn all_distances(stars: List(Posn)) -> Int { + use acc, pair <- list.fold(list.combination_pairs(stars), 0) + let #(s1, s2) = pair + acc + int.absolute_value(s1.x - s2.x) + int.absolute_value(s1.y - s2.y) +} + +fn find_distances(input: String, expand_by: Int) -> String { + input + |> parse_with_expansion(expand_by) + |> all_distances + |> string.inspect +} + +pub fn part1(input: String) { + find_distances(input, 2) +} + +pub fn part2(input: String) { + find_distances(input, 1_000_000) +} + +pub fn main() { + let assert Ok(part) = adglent.get_part() + let assert Ok(input) = adglent.get_input("11") + case part { + First -> + part1(input) + |> adglent.inspect + |> io.println + Second -> + part2(input) + |> adglent.inspect + |> io.println + } +} |