1
0
Fork 0

solve 2023 4.2

This commit is contained in:
sloane 2023-12-04 08:54:37 -05:00
parent f35ba521e1
commit fc7ae556c1
No known key found for this signature in database
2 changed files with 35 additions and 6 deletions

View file

@ -9,11 +9,40 @@ aoc 2023, 4 do
|> Enum.sum() |> Enum.sum()
end end
def p2(_input) do def p2(input) do
# input cards =
# |> String.split("\n") input
# |> Enum.map(&parse_line/1) |> String.split("\n")
# |> Enum.map(&score_2/1) |> Enum.map(&parse_line/1)
|> Enum.map(&score_2/1)
card_counts =
cards
|> Stream.map(fn {id, _} -> {id, 1} end)
|> Enum.into(%{})
cards
|> Enum.reduce(card_counts, fn {id, winners}, counts ->
if winners > 0 do
copies = Map.get(counts, id)
copied_range =
(id + 1)..(id + winners)
for copy_id <- copied_range, reduce: counts do
counts ->
if Map.has_key?(counts, copy_id) do
Map.update!(counts, copy_id, &(&1 + copies))
else
counts
end
end
else
counts
end
end)
|> Map.values()
|> Enum.sum()
end end
def parse_line(line) do def parse_line(line) do

View file

@ -14,7 +14,7 @@
1. [2020] **17/50** 🌟 1. [2020] **17/50** 🌟
1. [2021] **43/50** 🌟 1. [2021] **43/50** 🌟
1. [2022] **14/50** 🌟 1. [2022] **14/50** 🌟
1. [2023] **7/50** 🌟 1. [2023] **8/50** 🌟
[2015]: ./2015 [2015]: ./2015
[2017]: ./2017 [2017]: ./2017