diff options
author | HJ <thechairman@thechairman.info> | 2023-12-11 08:38:04 -0500 |
---|---|---|
committer | HJ <thechairman@thechairman.info> | 2023-12-11 08:38:04 -0500 |
commit | d2c6b88d2e290b1aa3a04a10e80b5f33fabcb654 (patch) | |
tree | 3b704bb4799468028a41c971291bacdd7880adcb /aoc2023 | |
parent | 0223d8e8e374e6f1e3af2e0be93229b549489fcc (diff) | |
download | gleam_aoc-d2c6b88d2e290b1aa3a04a10e80b5f33fabcb654.tar.gz gleam_aoc-d2c6b88d2e290b1aa3a04a10e80b5f33fabcb654.zip |
day 11 gleam complete
Diffstat (limited to 'aoc2023')
-rw-r--r-- | aoc2023/manifest.toml | 10 | ||||
-rw-r--r-- | aoc2023/src/day11/.gitignore | 1 | ||||
-rw-r--r-- | aoc2023/src/day11/solve.gleam | 90 | ||||
-rw-r--r-- | aoc2023/test/day11/day11_test.gleam | 66 |
4 files changed, 162 insertions, 5 deletions
diff --git a/aoc2023/manifest.toml b/aoc2023/manifest.toml index b6965da..8e87d48 100644 --- a/aoc2023/manifest.toml +++ b/aoc2023/manifest.toml @@ -2,18 +2,18 @@ # You typically do not need to edit this file packages = [ - { name = "adglent", version = "1.2.0", build_tools = ["gleam"], requirements = ["gleam_otp", "gap", "glint", "gleam_http", "snag", "gleam_stdlib", "tom", "gleam_community_ansi", "gleam_erlang", "gleam_httpc", "simplifile"], otp_app = "adglent", source = "hex", outer_checksum = "A20D35001061F8AD602E3B92FB3AC0E1E4EEC642AD2AAE0ACEAD3A85F37DA7F0" }, - { name = "gap", version = "1.0.1", build_tools = ["gleam"], requirements = ["gleam_stdlib", "gleam_community_ansi"], otp_app = "gap", source = "hex", outer_checksum = "5E369751DB547BFBDA7735878DC04DA31FCA3112193D61D5D7566010C7C8BA98" }, - { name = "gleam_community_ansi", version = "1.2.0", build_tools = ["gleam"], requirements = ["gleam_stdlib", "gleam_community_colour"], otp_app = "gleam_community_ansi", source = "hex", outer_checksum = "8B5A9677BC5A2738712BBAF2BA289B1D8195FDF962BBC769569976AD5E9794E1" }, + { name = "adglent", version = "1.2.0", build_tools = ["gleam"], requirements = ["simplifile", "gleam_otp", "gleam_erlang", "gleam_community_ansi", "tom", "gleam_httpc", "gleam_http", "snag", "gleam_stdlib", "glint", "gap"], otp_app = "adglent", source = "hex", outer_checksum = "A20D35001061F8AD602E3B92FB3AC0E1E4EEC642AD2AAE0ACEAD3A85F37DA7F0" }, + { name = "gap", version = "1.0.1", build_tools = ["gleam"], requirements = ["gleam_community_ansi", "gleam_stdlib"], otp_app = "gap", source = "hex", outer_checksum = "5E369751DB547BFBDA7735878DC04DA31FCA3112193D61D5D7566010C7C8BA98" }, + { name = "gleam_community_ansi", version = "1.2.0", build_tools = ["gleam"], requirements = ["gleam_community_colour", "gleam_stdlib"], otp_app = "gleam_community_ansi", source = "hex", outer_checksum = "8B5A9677BC5A2738712BBAF2BA289B1D8195FDF962BBC769569976AD5E9794E1" }, { name = "gleam_community_colour", version = "1.2.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_community_colour", source = "hex", outer_checksum = "036C206886AFB9F153C552700A7A0B4D2864E3BC96A20C77E5F34A013C051BE3" }, - { name = "gleam_community_maths", version = "1.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_community_maths", source = "hex", outer_checksum = "2F85C076830150347FBA286C3C440EC2CFB3538407105E5EF3FC64D17E711AC2" }, + { name = "gleam_community_maths", version = "1.0.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_community_maths", source = "hex", outer_checksum = "1B9DB313E94A0E4674CA84C5D29F45ECFE211BFB38ABBD8B23737395F47D08B3" }, { name = "gleam_erlang", version = "0.23.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "C21CFB816C114784E669FFF4BBF433535EEA9960FA2F216209B8691E87156B96" }, { name = "gleam_http", version = "3.5.2", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_http", source = "hex", outer_checksum = "AECDA43AFD523D07A8F09068598A6E271C505278A0CB6F9C7A2E4365EAE8D11E" }, { name = "gleam_httpc", version = "2.1.1", build_tools = ["gleam"], requirements = ["gleam_http", "gleam_stdlib"], otp_app = "gleam_httpc", source = "hex", outer_checksum = "06AC1CA52C9BAA66C9D5C0303B2BF34E39AA1546BB96AEE496E4B06D513AB8C7" }, { name = "gleam_otp", version = "0.8.0", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_stdlib"], otp_app = "gleam_otp", source = "hex", outer_checksum = "18EF8242A5E54BA92F717C7222F03B3228AEE00D1F286D4C56C3E8C18AA2588E" }, { name = "gleam_stdlib", version = "0.33.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "539E37A2AA5EBE8E75F4B74755E4CC604BD957C3000AC8D705A2024886A2738B" }, { name = "gleeunit", version = "1.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "D3682ED8C5F9CAE1C928F2506DE91625588CC752495988CBE0F5653A42A6F334" }, - { name = "glint", version = "0.13.0", build_tools = ["gleam"], requirements = ["gleam_community_colour", "gleam_stdlib", "gleam_community_ansi", "snag"], otp_app = "glint", source = "hex", outer_checksum = "46E56049CD370D61F720D319D0AB970408C9336EEB918F08B5DCB1DCE9845FA3" }, + { name = "glint", version = "0.13.0", build_tools = ["gleam"], requirements = ["gleam_community_ansi", "gleam_community_colour", "snag", "gleam_stdlib"], otp_app = "glint", source = "hex", outer_checksum = "46E56049CD370D61F720D319D0AB970408C9336EEB918F08B5DCB1DCE9845FA3" }, { name = "simplifile", version = "1.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "simplifile", source = "hex", outer_checksum = "0BD6F0E7DA1A7E11D18B8AD48453225CAFCA4C8CFB4513D217B372D2866C501C" }, { name = "snag", version = "0.2.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "snag", source = "hex", outer_checksum = "8FD70D8FB3728E08AC425283BB509BB0F012BE1AE218424A597CDE001B0EE589" }, { name = "tom", version = "0.2.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "tom", source = "hex", outer_checksum = "5C5A9B8586C547F1F39542B1A3BBD9FEE17AFEAB51CE53B32B13D0D46B421249" }, diff --git a/aoc2023/src/day11/.gitignore b/aoc2023/src/day11/.gitignore new file mode 100644 index 0000000..ae40cea --- /dev/null +++ b/aoc2023/src/day11/.gitignore @@ -0,0 +1 @@ +input.txt
\ No newline at end of file diff --git a/aoc2023/src/day11/solve.gleam b/aoc2023/src/day11/solve.gleam new file mode 100644 index 0000000..8ab2ee1 --- /dev/null +++ b/aoc2023/src/day11/solve.gleam @@ -0,0 +1,90 @@ +import adglent.{First, Second} +import gleam/io +import gleam/int +import gleam/string +import gleam/list + +type Posn { + Posn(x: Int, y: Int) +} + +fn manhattan_distance(p1: Posn, p2: Posn) -> Int { + int.absolute_value(p1.x - p2.x) + int.absolute_value(p1.y - p2.y) +} + +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 apply_expansion(p: Posn, empty_r: Int, empty_c: Int, add: Int) -> Posn { + Posn(p.x + empty_r * add, p.y + empty_c * add) +} + +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 empty_rows = count_prior_empty_ranks(r, empty_row_list) + let empty_cols = count_prior_empty_ranks(c, empty_col_list) + case cell { + "#" -> [apply_expansion(Posn(r, c), empty_rows, empty_cols, add), ..acc] + _empty -> acc + } + } + |> list.flatten() +} + +fn all_distances(stars: List(Posn)) -> Int { + use acc, pair <- list.fold(list.combination_pairs(stars), 0) + let #(star1, star2) = pair + acc + manhattan_distance(star1, star2) +} + +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 + } +} diff --git a/aoc2023/test/day11/day11_test.gleam b/aoc2023/test/day11/day11_test.gleam new file mode 100644 index 0000000..8bb8c06 --- /dev/null +++ b/aoc2023/test/day11/day11_test.gleam @@ -0,0 +1,66 @@ +import gleam/list +import showtime/tests/should +import adglent.{type Example, Example} +import day11/solve + +type Problem1AnswerType = + String + +type Problem2AnswerType = + String + +/// Add examples for part 1 here: +/// ```gleam +///const part1_examples: List(Example(Problem1AnswerType)) = [Example("some input", "")] +/// ``` +const part1_examples: List(Example(Problem1AnswerType)) = [ + Example( + "...#...... +.......#.. +#......... +.......... +......#... +.#........ +.........# +.......... +.......#.. +#...#.....", + "374", + ), +] + +/// Add examples for part 2 here: +/// ```gleam +///const part2_examples: List(Example(Problem2AnswerType)) = [Example("some input", "")] +/// ``` +const part2_examples: List(Example(Problem2AnswerType)) = [ + Example( + "...#...... +.......#.. +#......... +.......... +......#... +.#........ +.........# +.......... +.......#.. +#...#.....", + "8410", + ), +] + +pub fn part1_test() { + part1_examples + |> should.not_equal([]) + use example <- list.map(part1_examples) + solve.part1(example.input) + |> should.equal(example.answer) +} + +pub fn part2_test() { + part2_examples + |> should.not_equal([]) + use example <- list.map(part2_examples) + solve.part2(example.input) + |> should.equal(example.answer) +} |