Mix.install([:kino])
input = Kino.Input.textarea("Please enter your data here")
input =
input
|> Kino.Input.read()
|> String.split(["\n", " "], trim: true)
cycle_sample = [20, 60, 100, 140, 180, 220]
input
|> Enum.reduce([{1, 1}], fn command, [{cycle, x} | _tl] = list ->
tick =
case command do
"noop" -> {cycle + 1, x}
"addx" -> {cycle + 1, x}
number -> {cycle + 1, x + String.to_integer(number)}
end
[tick | list]
end)
|> Enum.filter(&Enum.member?(cycle_sample, elem(&1, 0)))
|> Enum.map(fn {c, x} -> c * x end)
|> Enum.sum()
|> dbg
width = 40
input
|> Enum.reduce([{1, 1}], fn command, [{cycle, x} | _tl] = list ->
tick =
case command do
"noop" -> {cycle + 1, x}
"addx" -> {cycle + 1, x}
number -> {cycle + 1, x + String.to_integer(number)}
end
[tick | list]
end)
|> Enum.reverse()
|> Enum.reduce([], fn {c, x}, l ->
cond do
rem(c - 1, 40) in (x - 1)..(x + 1) -> ["#" | l]
true -> ["." | l]
end
end)
|> Enum.reverse()
|> Enum.chunk_every(width, width, :discard)
|> Enum.map(&Enum.join(&1))
|> Enum.each(&IO.puts(&1))
BGKAEREZ