solve 2023 4.1
This commit is contained in:
parent
663165a30d
commit
f35ba521e1
3 changed files with 65 additions and 2 deletions
|
@ -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
|
||||
|
|
62
2023/elixir/lib/2023/4.ex
Normal file
62
2023/elixir/lib/2023/4.ex
Normal file
|
@ -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
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue