Powered by AppSignal & Oban Pro

Day 8 - Advent of Code 2016

lib/advent_of_code/2016/day-08.livemd

Day 8 - Advent of Code 2016

Mix.install([:kino, :benchee])
:ok

Links

Prompt

--- Day 8: Two-Factor Authentication ---

You come across a door implementing what you can only assume is an implementation of two-factor authentication after a long game of requirements telephone.

To get past the door, you first swipe a keycard (no problem; there was one on a nearby desk). Then, it displays a code on a little screen, and you type that code on a keypad. Then, presumably, the door unlocks.

Unfortunately, the screen has been smashed. After a few minutes, you've taken everything apart and figured out how it works. Now you just have to work out what the screen would have displayed.

The magnetic strip on the card you swiped encodes a series of instructions for the screen; these instructions are your puzzle input. The screen is 50 pixels wide and 6 pixels tall, all of which start off, and is capable of three somewhat peculiar operations:

  • rect AxB turns on all of the pixels in a rectangle at the top-left of the screen which is A wide and B tall.
  • rotate row y=A by B shifts all of the pixels in row A (0 is the top row) right by B pixels. Pixels that would fall off the right end appear at the left end of the row.
  • rotate column x=A by B shifts all of the pixels in column A (0 is the left column) down by B pixels. Pixels that would fall off the bottom appear at the top of the column.

For example, here is a simple sequence on a smaller screen:

  • rect 3x2 creates a small rectangle in the top-left corner:
    ###....
    ###....
    .......
    
  • rotate column x=1 by 1 rotates the second column down by one pixel:
    #.#....
    ###....
    .#.....
    
  • rotate row y=0 by 4 rotates the top row right by four pixels:
    ....#.#
    ###....
    .#.....
    
  • rotate column x=1 by 1 again rotates the second column down by one pixel, causing the bottom pixel to wrap back to the top:
    .#..#.#
    #.#....
    .#.....
    

As you can see, this display technology is extremely powerful, and will soon dominate the tiny-code-displaying-screen market. That's what the advertisement on the back of the display tries to convince you, anyway.

There seems to be an intermediate check of the voltage used by the display: after you swipe your card, if the screen did work, how many pixels should be lit?

To begin, get your puzzle input.

--- Part Two ---

Part Two prompt

QUESTION TWO?

Although it hasn't changed, you can still get your puzzle input.

Input

input = Kino.Input.textarea("Please paste your input file:")
input = input |> Kino.Input.read()
""

Solution

defmodule Day08 do
  defdelegate parse(input), to: __MODULE__.Input

  def part1(input) do
    input
    |> parse()
    |> Enum.map(& &1)
  end

  def part2(input) do
    input
    |> parse()
    |> Enum.map(& &1)
  end

  defmodule Input do
    def parse(input) when is_binary(input) do
      input
      |> String.split("\n")
      |> parse()
    end

    def parse(input) when is_list(input) do
      Stream.map(input, &parse_line/1)
    end

    def parse_line(line) do
      line
      # |> String.split(["-", ","])
      # |> Enum.map(&String.to_integer/1)
    end
  end
end
{:module, Day08, <<70, 79, 82, 49, 0, 0, 8, ...>>,
 {:module, Day08.Input, <<70, 79, 82, ...>>, {:parse_line, 1}}}

QUESTION ONE?

Your puzzle answer was answer one.

Day08.part1(input)
[""]

QUESTION TWO?

Your puzzle answer was answer two.

Day08.part2(input)
[""]

Both parts of this puzzle are complete! They provide two gold stars: **

At this point, you should return to your Advent calendar and try another puzzle.

If you still want to see it, you can get your puzzle input.

Tests

ExUnit.start(auto_run: false)

defmodule Day08Test do
  use ExUnit.Case, async: false

  setup_all do
    [
      input: ""
    ]
  end

  describe "part1/1" do
    test "returns expected value", %{input: input} do
      assert Day08.part1(input) == [""]
    end
  end

  describe "part2/1" do
    test "returns expected value", %{input: input} do
      assert Day08.part2(input) == [""]
    end
  end
end

ExUnit.run()
..
Finished in 0.00 seconds (0.00s async, 0.00s sync)
2 tests, 0 failures

Randomized with seed 162351
%{excluded: 0, failures: 0, skipped: 0, total: 2}

Benchmarking

# https://github.com/bencheeorg/benchee

Benchee.run(
  %{
    "Part 1" => fn -> Day08.part1(input) end,
    "Part 2" => fn -> Day08.part2(input) end
  },
  memory_time: 2,
  reduction_time: 2
)

nil

nil