solve 2023 4.2
This commit is contained in:
parent
f35ba521e1
commit
fc7ae556c1
2 changed files with 35 additions and 6 deletions
|
@ -9,11 +9,40 @@ aoc 2023, 4 do
|
|||
|> Enum.sum()
|
||||
end
|
||||
|
||||
def p2(_input) do
|
||||
# input
|
||||
# |> String.split("\n")
|
||||
# |> Enum.map(&parse_line/1)
|
||||
# |> Enum.map(&score_2/1)
|
||||
def p2(input) do
|
||||
cards =
|
||||
input
|
||||
|> String.split("\n")
|
||||
|> 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
|
||||
|
||||
def parse_line(line) do
|
||||
|
|
|
@ -14,7 +14,7 @@
|
|||
1. [2020] **17/50** 🌟
|
||||
1. [2021] **43/50** 🌟
|
||||
1. [2022] **14/50** 🌟
|
||||
1. [2023] **7/50** 🌟
|
||||
1. [2023] **8/50** 🌟
|
||||
|
||||
[2015]: ./2015
|
||||
[2017]: ./2017
|
||||
|
|
Loading…
Reference in a new issue