From f35ba521e1b2e07580575395ce6968645e5df09e Mon Sep 17 00:00:00 2001 From: sloane <1699281+sloanelybutsurely@users.noreply.github.com> Date: Mon, 4 Dec 2023 08:35:12 -0500 Subject: [PATCH] solve 2023 4.1 --- 2023/README.md | 3 +- 2023/elixir/lib/2023/4.ex | 62 +++++++++++++++++++++++++++++++++++++++ README.md | 2 +- 3 files changed, 65 insertions(+), 2 deletions(-) create mode 100644 2023/elixir/lib/2023/4.ex diff --git a/2023/README.md b/2023/README.md index 84b69c5..8e88a98 100644 --- a/2023/README.md +++ b/2023/README.md @@ -3,7 +3,7 @@ | S | M | T | W | T | F | S | | :-: | :-: | :-: | :-: | :-: | :-: | :-: | | | | | | | [1] | [2] | -| [3] | 4 | 5 | 6 | 7 | 8 | 9 | +| [3] | [4] | 5 | 6 | 7 | 8 | 9 | | 10 | 11 | 12 | 13 | 14 | 15 | 16 | | 17 | 18 | 19 | 20 | 21 | 22 | 23 | | 24 | 25 | | | | | | @@ -12,3 +12,4 @@ [1]: ./elixir/lib/2023/1.ex [2]: ./elixir/lib/2023/2.ex [3]: ./elixir/lib/2023/3.ex +[4]: ./elixir/lib/2023/4.ex diff --git a/2023/elixir/lib/2023/4.ex b/2023/elixir/lib/2023/4.ex new file mode 100644 index 0000000..360dbbf --- /dev/null +++ b/2023/elixir/lib/2023/4.ex @@ -0,0 +1,62 @@ +import AOC + +aoc 2023, 4 do + def p1(input) do + input + |> String.split("\n") + |> Stream.map(&parse_line/1) + |> Stream.map(&score_1/1) + |> Enum.sum() + end + + def p2(_input) do + # input + # |> String.split("\n") + # |> Enum.map(&parse_line/1) + # |> Enum.map(&score_2/1) + end + + def parse_line(line) do + ["Card " <> card_string, numbers_string] = String.split(line, ": ") + [winning_string, you_have_string] = String.split(numbers_string, " | ") + + card = + card_string + |> String.trim() + |> String.to_integer() + + winning = + winning_string + |> String.split(" ", trim: true) + |> Enum.map(&String.to_integer/1) + + you_have = + you_have_string + |> String.split(" ", trim: true) + |> Enum.map(&String.to_integer/1) + + {card, winning, you_have} + end + + def score_1({_, winning, you_have}) do + number_of_winners = + winning + |> Enum.filter(&Enum.member?(you_have, &1)) + |> Enum.count() + + if number_of_winners < 1 do + 0 + else + 2 ** (number_of_winners - 1) + end + end + + def score_2({id, winning, you_have}) do + number_of_winners = + winning + |> Enum.filter(&Enum.member?(you_have, &1)) + |> Enum.count() + + {id, number_of_winners} + end +end diff --git a/README.md b/README.md index 8a4b0de..389032c 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ 1. [2020] **17/50** 🌟 1. [2021] **43/50** 🌟 1. [2022] **14/50** 🌟 -1. [2023] **6/50** 🌟 +1. [2023] **7/50** 🌟 [2015]: ./2015 [2017]: ./2017