diff options
author | J.J <thechairman@thechairman.info> | 2023-12-05 03:30:12 -0500 |
---|---|---|
committer | J.J <thechairman@thechairman.info> | 2023-12-05 03:30:12 -0500 |
commit | e3fc8bc189f87e2d42c54e340404fbcfd1e64f65 (patch) | |
tree | 723cb182e169bc3099eba284e6c02a00d5e722f4 /aoc2023 | |
parent | 8549ac47456f91ffc760d097f51c1aba9d689f70 (diff) | |
parent | a2c26c374a2b450557cff307144812537a30a744 (diff) | |
download | gleam_aoc-e3fc8bc189f87e2d42c54e340404fbcfd1e64f65.tar.gz gleam_aoc-e3fc8bc189f87e2d42c54e340404fbcfd1e64f65.zip |
Merge branch 'main' of https://github.com/hunkyjimpjorps/AdventOfCode
Diffstat (limited to 'aoc2023')
-rw-r--r-- | aoc2023/gleam.toml | 2 | ||||
-rw-r--r-- | aoc2023/manifest.toml | 11 | ||||
-rw-r--r-- | aoc2023/src/day4/solve.gleam | 91 | ||||
-rw-r--r-- | aoc2023/test/day2/day2_test.gleam | 5 | ||||
-rw-r--r-- | aoc2023/test/day4/day4_test.gleam | 18 |
5 files changed, 60 insertions, 67 deletions
diff --git a/aoc2023/gleam.toml b/aoc2023/gleam.toml index a437c55..387036d 100644 --- a/aoc2023/gleam.toml +++ b/aoc2023/gleam.toml @@ -1,5 +1,6 @@ name = "aoc2023" version = "0.1.0" +gleam = ">= 0.32.0" # Fill out these fields if you intend to generate HTML documentation or publish # your project to the Hex package manager. @@ -12,6 +13,7 @@ version = "0.1.0" [dependencies] gleam_stdlib = "~> 0.32" simplifile = "~> 1.0" +gleam_erlang = "~> 0.23" [dev-dependencies] gleeunit = "~> 1.0" diff --git a/aoc2023/manifest.toml b/aoc2023/manifest.toml index 31434f0..aea86fb 100644 --- a/aoc2023/manifest.toml +++ b/aoc2023/manifest.toml @@ -2,17 +2,17 @@ # You typically do not need to edit this file packages = [ - { name = "adglent", version = "1.2.0", build_tools = ["gleam"], requirements = ["gleam_community_ansi", "gap", "gleam_httpc", "gleam_otp", "gleam_stdlib", "snag", "glint", "gleam_erlang", "simplifile", "gleam_http", "tom"], 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 = "adglent", version = "1.2.0", build_tools = ["gleam"], requirements = ["tom", "gleam_http", "gleam_stdlib", "simplifile", "gleam_community_ansi", "gap", "gleam_httpc", "gleam_otp", "snag", "glint", "gleam_erlang"], 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_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_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_stdlib", "gleam_erlang"], otp_app = "gleam_otp", source = "hex", outer_checksum = "18EF8242A5E54BA92F717C7222F03B3228AEE00D1F286D4C56C3E8C18AA2588E" }, + { name = "gleam_httpc", version = "2.1.1", build_tools = ["gleam"], requirements = ["gleam_stdlib", "gleam_http"], 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", "snag", "gleam_community_ansi"], otp_app = "glint", source = "hex", outer_checksum = "46E56049CD370D61F720D319D0AB970408C9336EEB918F08B5DCB1DCE9845FA3" }, + { name = "glint", version = "0.13.0", build_tools = ["gleam"], requirements = ["gleam_community_ansi", "gleam_stdlib", "gleam_community_colour", "snag"], 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" }, @@ -20,6 +20,7 @@ packages = [ [requirements] adglent = { version = "~> 1.2" } +gleam_erlang = { version = "~> 0.23" } gleam_stdlib = { version = "~> 0.32" } gleeunit = { version = "~> 1.0" } simplifile = { version = "~> 1.0" } diff --git a/aoc2023/src/day4/solve.gleam b/aoc2023/src/day4/solve.gleam index eab700b..d0da835 100644 --- a/aoc2023/src/day4/solve.gleam +++ b/aoc2023/src/day4/solve.gleam @@ -1,103 +1,86 @@ import adglent.{First, Second} -import gleam/io -import gleam/string +import gleam/bool +import gleam/dict.{type Dict} import gleam/int +import gleam/io import gleam/list -import gleam/dict -import gleam/result import gleam/option.{None, Some} +import gleam/result +import gleam/set.{type Set} +import gleam/string type Card { - Card(number: Int, winning: List(Int), has: List(Int)) + Card(number: Int, winners: Int) } -fn numbers_to_list(str: String) -> List(Int) { +fn numbers_to_set(str: String) -> Set(Int) { str |> string.split(" ") |> list.map(int.parse) |> result.values() + |> set.from_list() } fn parse_card(card: String) -> Card { let assert Ok(#("Card" <> n_str, rest)) = string.split_once(card, ": ") let assert Ok(#(winning_str, has_str)) = string.split_once(rest, " | ") - let assert Ok(n) = - n_str - |> string.trim - |> int.parse + let assert Ok(n) = int.parse(string.trim(n_str)) - let winning = numbers_to_list(winning_str) - let has = numbers_to_list(has_str) + let winning = numbers_to_set(winning_str) + let has = numbers_to_set(has_str) + let winners = set.size(set.intersection(winning, has)) - Card(number: n, winning: winning, has: has) + Card(number: n, winners: winners) } -fn win_points(n) { - case n { - 0 -> 0 - 1 -> 1 - n -> 2 * win_points(n - 1) - } -} - -fn count_wins(card: Card) { - use acc, c <- list.fold(card.has, 0) - case list.contains(card.winning, c) { - True -> acc + 1 - False -> acc - } +fn win_points(n: Int) { + bool.guard(n < 2, n, fn() { 2 * win_points(n - 1) }) } pub fn part1(input: String) { use acc, c <- list.fold(string.split(input, "\n"), 0) c |> parse_card - |> count_wins - |> win_points + |> fn(c: Card) { win_points(c.winners) } |> int.add(acc) } -fn win_more_cards(cards: List(String), card_count: dict.Dict(Int, Int)) { +fn win_more_cards(cards: List(String), count: Dict(Int, Int)) { case cards { [] -> - card_count + count + |> io.debug |> dict.values |> int.sum [raw_card, ..rest] -> { let card = parse_card(raw_card) - let wins = count_wins(card) - case wins { - 0 -> win_more_cards(rest, card_count) - n -> { - let assert Ok(bonus) = dict.get(card_count, card.number) - let won_cards = list.range(card.number + 1, card.number + n) - { - use acc, n <- list.fold(won_cards, card_count) - use c <- dict.update(acc, n) - case c { - Some(i) -> i + bonus - None -> panic as "won a card that doesn't exist in the card pile" - } - } - |> win_more_cards(rest, _) - } + case card.winners { + 0 -> win_more_cards(rest, count) + n -> win_more_cards(rest, update_counts(n, card, count)) } } } } +fn update_counts(n: Int, card: Card, count: Dict(Int, Int)) -> Dict(Int, Int) { + let assert Ok(bonus) = dict.get(count, card.number) + use acc, n <- list.fold(list.range(card.number + 1, card.number + n), count) + use c <- dict.update(acc, n) + case c { + Some(i) -> i + bonus + None -> panic as "won a card that doesn't exist in the card pile" + } +} + pub fn part2(input: String) { - let raw_cards = - input - |> string.split("\n") + let cards = string.split(input, "\n") - let card_count = - list.range(1, list.length(raw_cards)) + let count = + list.range(1, list.length(cards)) |> list.map(fn(n) { #(n, 1) }) |> dict.from_list() - raw_cards - |> win_more_cards(card_count) + win_more_cards(cards, count) } pub fn main() { diff --git a/aoc2023/test/day2/day2_test.gleam b/aoc2023/test/day2/day2_test.gleam index 6cddda1..28a65da 100644 --- a/aoc2023/test/day2/day2_test.gleam +++ b/aoc2023/test/day2/day2_test.gleam @@ -37,10 +37,7 @@ Game 4: 1 green, 3 red, 6 blue; 3 green, 6 red; 3 green, 15 blue, 14 red Game 5: 6 red, 1 blue, 3 green; 2 blue, 1 red, 2 green", 2286, ), - Example( - "Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green", - 48, - ), + Example("Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green", 48), ] pub fn part1_test() { diff --git a/aoc2023/test/day4/day4_test.gleam b/aoc2023/test/day4/day4_test.gleam index 9fc4bf7..324fe36 100644 --- a/aoc2023/test/day4/day4_test.gleam +++ b/aoc2023/test/day4/day4_test.gleam @@ -13,23 +13,33 @@ type Problem2AnswerType = /// ```gleam ///const part1_examples: List(Example(Problem1AnswerType)) = [Example("some input", "")] /// ``` -const part1_examples: List(Example(Problem1AnswerType)) = [Example("Card 1: 41 48 83 86 17 | 83 86 6 31 17 9 48 53 +const part1_examples: List(Example(Problem1AnswerType)) = [ + Example( + "Card 1: 41 48 83 86 17 | 83 86 6 31 17 9 48 53 Card 2: 13 32 20 16 61 | 61 30 68 82 17 32 24 19 Card 3: 1 21 53 59 44 | 69 82 63 72 16 21 14 1 Card 4: 41 92 73 84 69 | 59 84 76 51 58 5 54 83 Card 5: 87 83 26 28 32 | 88 30 70 12 93 22 82 36 -Card 6: 31 18 13 56 72 | 74 77 10 23 35 67 36 11", 13)] +Card 6: 31 18 13 56 72 | 74 77 10 23 35 67 36 11", + 13, + ), +] /// Add examples for part 2 here: /// ```gleam ///const part2_examples: List(Example(Problem2AnswerType)) = [Example("some input", "")] /// ``` -const part2_examples: List(Example(Problem2AnswerType)) = [Example("Card 1: 41 48 83 86 17 | 83 86 6 31 17 9 48 53 +const part2_examples: List(Example(Problem2AnswerType)) = [ + Example( + "Card 1: 41 48 83 86 17 | 83 86 6 31 17 9 48 53 Card 2: 13 32 20 16 61 | 61 30 68 82 17 32 24 19 Card 3: 1 21 53 59 44 | 69 82 63 72 16 21 14 1 Card 4: 41 92 73 84 69 | 59 84 76 51 58 5 54 83 Card 5: 87 83 26 28 32 | 88 30 70 12 93 22 82 36 -Card 6: 31 18 13 56 72 | 74 77 10 23 35 67 36 11", 30)] +Card 6: 31 18 13 56 72 | 74 77 10 23 35 67 36 11", + 30, + ), +] pub fn part1_test() { part1_examples |