Skip to content

Latest commit

 

History

History
71 lines (58 loc) · 1.2 KB

day_10.livemd

File metadata and controls

71 lines (58 loc) · 1.2 KB

Day 10

Mix.install([:kino])

Input

input = Kino.Input.textarea("Please enter your data here")
input =
  input
  |> Kino.Input.read()
  |> String.split(["\n", " "], trim: true)

Part 1

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

Part 2

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