DEV Community

Caleb Weeks
Caleb Weeks

Posted on • Originally published at sethcalebweeks.com

Advent of Code Day 10

Links

Highlights

  • I had a lot of fun with today's puzzle. The CRT idea was brilliant, and it was awesome to be able to play with something visual.
  • The code was also relatively straightforward. After parsing all the register instructions, we just needed to play with the output to calculate/reveal the answer.
  • The function guard does all the heavy lifting for the second part. It checks to make sure the register value puts the sprite within the index that is currently being drawn using a modulus and a range check.
defmodule Day10 do
  use AOC

  def execute("noop", {cycles, register}), do: {cycles ++ [register], register}
  def execute("addx " <> value, {cycles, register}) do
    updated_register = register + String.to_integer(value)
    {cycles ++ [register, updated_register], updated_register}
  end

  def part1 do
    input(10)
    |> String.split("\n", trim: true)
    |> Enum.reduce({[1], 1}, &execute/2)
    |> elem(0)
    |> (&Enum.slice(&1, 19..length(&1))).()
    |> Enum.take_every(40)
    |> Enum.zip(20..220//40)
    |> Enum.reduce(0, fn {register, cycle}, signal -> signal + register * cycle end)
  end

  def part2 do
    input(10)
    |> String.trim
    |> String.split("\n", trim: true)
    |> Enum.reduce({[1], 1}, &execute/2)
    |> elem(0)
    |> Enum.with_index
    |> Enum.map(fn
      {register, position} when register - rem(position, 40) in -1..1 -> "#"
      _ -> "."
    end)
    |> Enum.chunk_every(40)
    |> Enum.map(&Enum.join/1)
  end

end

Enter fullscreen mode Exit fullscreen mode

Top comments (0)