Day 10: Cathode-Ray Tube
Mix.install([:kino])
input = Kino.Input.textarea("Please paste your input:")
Part 1
https://adventofcode.com/2022/day/10
signals =
input
|> Kino.Input.read()
|> String.split("\n", trim: true)
|> Enum.map(fn
"noop" -> :noop
"addx " <> value -> {:add, String.to_integer(value)}
end)
normalized_signals =
signals
|> Enum.flat_map(fn
:noop -> [0]
{:add, value} -> [0, value]
end)
registers =
[0 | normalized_signals |> Enum.drop(-1)]
|> Enum.scan(1, &(&1 + &2))
registers
|> Enum.with_index(1)
|> Enum.reduce([], fn {value, cycle}, signal_strength ->
case rem(cycle - 20, 40) do
0 -> [{cycle, value} | signal_strength]
_ -> signal_strength
end
end)
|> Enum.map(fn {cycle, register} -> cycle * register end)
|> Enum.sum()
Part 2
https://adventofcode.com/2022/day/10#part2
pixels =
registers
|> Enum.with_index(1)
|> Enum.reduce([], fn {register, cycle}, pixels ->
position = rem(cycle - 1, 40)
pixel =
case abs(register - position) <= 1 do
true -> "#"
false -> "."
end
case position do
39 -> ["\n", pixel | pixels]
_ -> [pixel | pixels]
end
end)
|> Enum.reverse()
pixels
|> IO.iodata_to_binary()
|> IO.puts()