Day 14. There is probably a much smarter way to solve part 2 but I couldn\'t think of it
parent
1511d22b8c
commit
9d8e3f05a6
@ -0,0 +1,88 @@
|
|||||||
|
# 94154500 too low
|
||||||
|
|
||||||
|
defmodule Day14 do
|
||||||
|
def run do
|
||||||
|
robots =
|
||||||
|
File.read!("data.txt")
|
||||||
|
|> String.trim()
|
||||||
|
|> String.split("\n")
|
||||||
|
|> Enum.map(&parse_robot/1)
|
||||||
|
|
||||||
|
width = 101
|
||||||
|
height = 103
|
||||||
|
steps = 100
|
||||||
|
|
||||||
|
new_robots =
|
||||||
|
Enum.reduce(1..steps, robots, fn idx, acc ->
|
||||||
|
step(acc, {width, height})
|
||||||
|
end)
|
||||||
|
|
||||||
|
quads = [
|
||||||
|
Enum.filter(new_robots, fn r -> r.p.x < trunc(width / 2) and r.p.y < trunc(height / 2) end),
|
||||||
|
Enum.filter(new_robots, fn r -> r.p.x > trunc(width / 2) and r.p.y < trunc(height / 2) end),
|
||||||
|
Enum.filter(new_robots, fn r -> r.p.x < trunc(width / 2) and r.p.y > trunc(height / 2) end),
|
||||||
|
Enum.filter(new_robots, fn r -> r.p.x > trunc(width / 2) and r.p.y > trunc(height / 2) end)
|
||||||
|
]
|
||||||
|
|
||||||
|
part1 = Enum.map(quads, &length/1) |> Enum.reduce(1, &*/2)
|
||||||
|
dbg(part1)
|
||||||
|
|
||||||
|
# part 2, print until i guess i see a tree
|
||||||
|
Enum.reduce(1..100_000, robots, fn idx, acc ->
|
||||||
|
IO.puts(idx)
|
||||||
|
|
||||||
|
res = step(acc, {width, height})
|
||||||
|
|
||||||
|
# ok only print if we see something interesting like an empty corner?
|
||||||
|
if corner_block(res) do
|
||||||
|
render(res, {width, height})
|
||||||
|
end
|
||||||
|
|
||||||
|
res
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
|
||||||
|
defp corner_block(robots) do
|
||||||
|
Enum.all?(0..10, fn col ->
|
||||||
|
Enum.all?(0..10, fn row ->
|
||||||
|
rs = Enum.filter(robots, fn %{p: %{x: x, y: y}} -> x == col and y == row end)
|
||||||
|
length(rs) == 0
|
||||||
|
end)
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
|
||||||
|
defp render(robots, {width, height}) do
|
||||||
|
Enum.each(0..(height - 1), fn row ->
|
||||||
|
Enum.each(0..(width - 1), fn col ->
|
||||||
|
r = Enum.filter(robots, fn %{p: %{x: x, y: y}} -> x == col and y == row end) |> length
|
||||||
|
if r == 0, do: IO.write("."), else: IO.write(r)
|
||||||
|
end)
|
||||||
|
|
||||||
|
IO.write("\n")
|
||||||
|
end)
|
||||||
|
|
||||||
|
IO.write("\n\n\n\n\n")
|
||||||
|
Process.sleep(300)
|
||||||
|
IO.puts(IO.ANSI.clear())
|
||||||
|
|
||||||
|
robots
|
||||||
|
end
|
||||||
|
|
||||||
|
defp step(robots, {width, height}) do
|
||||||
|
Enum.map(robots, fn %{p: p, v: v} ->
|
||||||
|
x = Integer.mod(p.x + v.x, width)
|
||||||
|
y = Integer.mod(p.y + v.y, height)
|
||||||
|
%{p: %{x: x, y: y}, v: v}
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
|
||||||
|
defp parse_robot(str) do
|
||||||
|
~r/p=(.+),(.+) v=(.+),(.+)/
|
||||||
|
|> Regex.run(str)
|
||||||
|
|> tl()
|
||||||
|
|> Enum.map(&String.to_integer/1)
|
||||||
|
|> (fn [a, b, c, d] -> %{p: %{x: a, y: b}, v: %{x: c, y: d}} end).()
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
Day14.run()
|
Loading…
Reference in New Issue