Day 3: Perfectly Spherical Houses in a Vacuum
Mix.install([:kino])
Input
input = Kino.Input.textarea("Input")
update_pos = fn
{x, y}, ">" -> {x + 1, y}
{x, y}, "<" -> {x - 1, y}
{x, y}, "^" -> {x, y + 1}
{x, y}, "v" -> {x, y - 1}
end
update_trace = fn trace, pos ->
Map.update(trace, pos, 1, &(&1 + 1))
end
instructions =
input
|> Kino.Input.read()
|> String.graphemes()
Part 1
instructions
|> Enum.reduce({{0, 0}, %{{0, 0} => 1}}, fn
d, {pos, trace} ->
pos
|> update_pos.(d)
|> then(&{&1, update_trace.(trace, &1)})
end)
|> elem(1)
|> Enum.count()
Part 2
instructions
|> Enum.chunk_every(2)
|> Enum.reduce({{0, 0}, {0, 0}, %{{0, 0} => 1}}, fn
[s, r], {s0, r0, trace} ->
s1 = update_pos.(s0, s)
r1 = update_pos.(r0, r)
trace =
trace
|> update_trace.(s1)
|> update_trace.(r1)
{s1, r1, trace}
end)
|> elem(2)
|> Enum.count()