Advent of Code Day 10

sethcalebweeks

Caleb Weeks

Posted on December 11, 2022

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
💖 💪 🙅 🚩
sethcalebweeks
Caleb Weeks

Posted on December 11, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Advent of Code Day 11
adventofcode Advent of Code Day 11

December 12, 2022

Advent of Code Day 10
adventofcode Advent of Code Day 10

December 11, 2022

Advent of Code Day 9
adventofcode Advent of Code Day 9

December 10, 2022

Advent of Code Day 8
adventofcode Advent of Code Day 8

December 8, 2022

Advent of Code Day 7
adventofcode Advent of Code Day 7

December 7, 2022