Day 11: remove debug code, simpler datastructure
This commit is contained in:
parent
212e979cbd
commit
dba1986b34
2 changed files with 82 additions and 119 deletions
|
@ -5,52 +5,47 @@ defmodule Day11Part1 do
|
||||||
|> Enum.map(&String.to_charlist/1)
|
|> Enum.map(&String.to_charlist/1)
|
||||||
|> parse_seat_plan()
|
|> parse_seat_plan()
|
||||||
|> run_until_stable()
|
|> run_until_stable()
|
||||||
|> elem(2)
|
|
||||||
|> count_occupied()
|
|> count_occupied()
|
||||||
|> IO.inspect()
|
|> IO.inspect()
|
||||||
end
|
end
|
||||||
|
|
||||||
def run_until_stable(state) do
|
def run_until_stable(seats) do
|
||||||
new_state = tick(state)
|
new_seats = tick(seats)
|
||||||
|
|
||||||
if new_state == state do
|
if new_seats == seats do
|
||||||
state
|
seats
|
||||||
else
|
else
|
||||||
run_until_stable(new_state)
|
run_until_stable(new_seats)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def tick({width, height, old_seats}) do
|
def tick(seats) do
|
||||||
new_seats =
|
Map.new(seats, fn {{x, y}, old_seat} -> toggle(old_seat, x, y, seats) end)
|
||||||
for(
|
|
||||||
x <- 0..(width - 1),
|
|
||||||
y <- 0..(height - 1),
|
|
||||||
do: {x, y}
|
|
||||||
)
|
|
||||||
|> Enum.reduce(%{}, fn {x, y}, new_seats ->
|
|
||||||
toggle(old_seats[{x, y}], x, y, old_seats, new_seats)
|
|
||||||
end)
|
|
||||||
|
|
||||||
{width, height, new_seats}
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def toggle(:empty, x, y, old_seats, new_seats) do
|
def toggle(:empty, x, y, seats) do
|
||||||
if occupied_adjacent(x, y, old_seats) == 0 do
|
new_value =
|
||||||
Map.put(new_seats, {x, y}, :occupied)
|
if occupied_adjacent(x, y, seats) == 0 do
|
||||||
|
:occupied
|
||||||
else
|
else
|
||||||
Map.put(new_seats, {x, y}, :empty)
|
:empty
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def toggle(:occupied, x, y, old_seats, new_seats) do
|
{{x, y}, new_value}
|
||||||
if occupied_adjacent(x, y, old_seats) >= 4 do
|
end
|
||||||
Map.put(new_seats, {x, y}, :empty)
|
|
||||||
|
def toggle(:occupied, x, y, seats) do
|
||||||
|
new_value =
|
||||||
|
if occupied_adjacent(x, y, seats) >= 4 do
|
||||||
|
:empty
|
||||||
else
|
else
|
||||||
Map.put(new_seats, {x, y}, :occupied)
|
:occupied
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def toggle(:floor, x, y, _, new_seats), do: Map.put(new_seats, {x, y}, :floor)
|
{{x, y}, new_value}
|
||||||
|
end
|
||||||
|
|
||||||
|
def toggle(:floor, x, y, _), do: {{x, y}, :floor}
|
||||||
|
|
||||||
def occupied_adjacent(x, y, seats) do
|
def occupied_adjacent(x, y, seats) do
|
||||||
adjacent = [
|
adjacent = [
|
||||||
|
@ -74,37 +69,24 @@ defmodule Day11Part1 do
|
||||||
end
|
end
|
||||||
|
|
||||||
def parse_seat_plan(input) do
|
def parse_seat_plan(input) do
|
||||||
Enum.reduce(input, {0, 0, %{}}, fn row, {_x, y, seats} ->
|
{_, seats} =
|
||||||
{x, seats} =
|
Enum.reduce(input, {0, %{}}, fn row, {y, seats} ->
|
||||||
|
{_, seats} =
|
||||||
Enum.reduce(row, {0, seats}, fn seat, {x, seats} ->
|
Enum.reduce(row, {0, seats}, fn seat, {x, seats} ->
|
||||||
seat =
|
seat =
|
||||||
case seat do
|
case seat do
|
||||||
?L -> :empty
|
?L -> :empty
|
||||||
?. -> :floor
|
?. -> :floor
|
||||||
|
?# -> :occupied
|
||||||
end
|
end
|
||||||
|
|
||||||
{x + 1, Map.put(seats, {x, y}, seat)}
|
{x + 1, Map.put(seats, {x, y}, seat)}
|
||||||
end)
|
end)
|
||||||
|
|
||||||
{x, y + 1, seats}
|
{y + 1, seats}
|
||||||
end)
|
end)
|
||||||
end
|
|
||||||
|
|
||||||
def visualize({width, height, seats}) do
|
seats
|
||||||
for y <- 0..(height - 1) do
|
|
||||||
for x <- 0..(width - 1) do
|
|
||||||
char =
|
|
||||||
case seats[{x, y}] do
|
|
||||||
:empty -> "L"
|
|
||||||
:floor -> "."
|
|
||||||
:occupied -> "#"
|
|
||||||
end
|
|
||||||
|
|
||||||
IO.write(char)
|
|
||||||
end
|
|
||||||
|
|
||||||
IO.write("\n")
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -5,52 +5,47 @@ defmodule Day11Part2 do
|
||||||
|> Enum.map(&String.to_charlist/1)
|
|> Enum.map(&String.to_charlist/1)
|
||||||
|> parse_seat_plan()
|
|> parse_seat_plan()
|
||||||
|> run_until_stable()
|
|> run_until_stable()
|
||||||
|> elem(2)
|
|
||||||
|> count_occupied()
|
|> count_occupied()
|
||||||
|> IO.inspect()
|
|> IO.inspect()
|
||||||
end
|
end
|
||||||
|
|
||||||
def run_until_stable(state) do
|
def run_until_stable(seats) do
|
||||||
new_state = tick(state)
|
new_seats = tick(seats)
|
||||||
|
|
||||||
if new_state == state do
|
if new_seats == seats do
|
||||||
state
|
seats
|
||||||
else
|
else
|
||||||
run_until_stable(new_state)
|
run_until_stable(new_seats)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def tick({width, height, old_seats}) do
|
def tick(seats) do
|
||||||
new_seats =
|
Map.new(seats, fn {{x, y}, old_seat} -> toggle(old_seat, x, y, seats) end)
|
||||||
for(
|
|
||||||
x <- 0..(width - 1),
|
|
||||||
y <- 0..(height - 1),
|
|
||||||
do: {x, y}
|
|
||||||
)
|
|
||||||
|> Enum.reduce(%{}, fn {x, y}, new_seats ->
|
|
||||||
toggle(old_seats[{x, y}], x, y, old_seats, new_seats)
|
|
||||||
end)
|
|
||||||
|
|
||||||
{width, height, new_seats}
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def toggle(:empty, x, y, old_seats, new_seats) do
|
def toggle(:empty, x, y, seats) do
|
||||||
if occupied_visible(x, y, old_seats) == 0 do
|
new_value =
|
||||||
Map.put(new_seats, {x, y}, :occupied)
|
if occupied_visible(x, y, seats) == 0 do
|
||||||
|
:occupied
|
||||||
else
|
else
|
||||||
Map.put(new_seats, {x, y}, :empty)
|
:empty
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def toggle(:occupied, x, y, old_seats, new_seats) do
|
{{x, y}, new_value}
|
||||||
if occupied_visible(x, y, old_seats) >= 5 do
|
end
|
||||||
Map.put(new_seats, {x, y}, :empty)
|
|
||||||
|
def toggle(:occupied, x, y, seats) do
|
||||||
|
new_value =
|
||||||
|
if occupied_visible(x, y, seats) >= 5 do
|
||||||
|
:empty
|
||||||
else
|
else
|
||||||
Map.put(new_seats, {x, y}, :occupied)
|
:occupied
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def toggle(:floor, x, y, _, new_seats), do: Map.put(new_seats, {x, y}, :floor)
|
{{x, y}, new_value}
|
||||||
|
end
|
||||||
|
|
||||||
|
def toggle(:floor, x, y, _), do: {{x, y}, :floor}
|
||||||
|
|
||||||
def occupied_visible(x, y, seats) do
|
def occupied_visible(x, y, seats) do
|
||||||
directions = [
|
directions = [
|
||||||
|
@ -83,8 +78,9 @@ defmodule Day11Part2 do
|
||||||
end
|
end
|
||||||
|
|
||||||
def parse_seat_plan(input) do
|
def parse_seat_plan(input) do
|
||||||
Enum.reduce(input, {0, 0, %{}}, fn row, {_x, y, seats} ->
|
{_, seats} =
|
||||||
{x, seats} =
|
Enum.reduce(input, {0, %{}}, fn row, {y, seats} ->
|
||||||
|
{_, seats} =
|
||||||
Enum.reduce(row, {0, seats}, fn seat, {x, seats} ->
|
Enum.reduce(row, {0, seats}, fn seat, {x, seats} ->
|
||||||
seat =
|
seat =
|
||||||
case seat do
|
case seat do
|
||||||
|
@ -96,25 +92,10 @@ defmodule Day11Part2 do
|
||||||
{x + 1, Map.put(seats, {x, y}, seat)}
|
{x + 1, Map.put(seats, {x, y}, seat)}
|
||||||
end)
|
end)
|
||||||
|
|
||||||
{x, y + 1, seats}
|
{y + 1, seats}
|
||||||
end)
|
end)
|
||||||
end
|
|
||||||
|
|
||||||
def visualize({width, height, seats}) do
|
seats
|
||||||
for y <- 0..(height - 1) do
|
|
||||||
for x <- 0..(width - 1) do
|
|
||||||
char =
|
|
||||||
case seats[{x, y}] do
|
|
||||||
:empty -> "L"
|
|
||||||
:floor -> "."
|
|
||||||
:occupied -> "#"
|
|
||||||
end
|
|
||||||
|
|
||||||
IO.write(char)
|
|
||||||
end
|
|
||||||
|
|
||||||
IO.write("\n")
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue