2021 day 4 part 2

This commit is contained in:
Adam Millerchip 2022-11-28 23:01:03 +09:00
parent 07820fe7dd
commit 3b1d14580a
1 changed files with 20 additions and 6 deletions

View File

@ -7,7 +7,10 @@ defmodule Day4 do
sum_unmarked(winning_board) * current_number
end
def play({_numbers = [], boards}), do: boards
def part2(input) do
{current_number, last_board} = play_until_last(input)
sum_unmarked(last_board) * current_number
end
def play({[current_number | next_numbers], boards}) do
boards = for board <- boards, do: mark(board, current_number)
@ -18,6 +21,21 @@ defmodule Day4 do
end
end
def play_until_last({_numbers, [_last_board]} = input), do: play(input)
def play_until_last({numbers, boards} = input) do
{_current_number, current_winner} = play(input)
current_winner_no_markers = Map.new(current_winner, fn {k, {v, _marker}} -> {k, v} end)
remaining_boards =
Enum.reject(boards, fn board ->
current_winner_no_markers == Map.new(board, fn {k, {v, _marker}} -> {k, v} end)
end)
play_until_last({numbers, remaining_boards})
end
def sum_unmarked(board) do
board
|> Enum.filter(&match?({_key, {_number, false}}, &1))
@ -54,10 +72,6 @@ defmodule Day4 do
end
end
def part2(_input) do
:ok
end
def input do
with [input_filename] <- System.argv(),
{:ok, input} <- File.read(input_filename) do
@ -116,4 +130,4 @@ defmodule Day4 do
end
end
# Day4.run()
Day4.run()