Skip to content

Instantly share code, notes, and snippets.

@mjm
Last active December 18, 2021 15:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mjm/57f4879101dae4555c575070102e1c1a to your computer and use it in GitHub Desktop.
Save mjm/57f4879101dae4555c575070102e1c1a to your computer and use it in GitHub Desktop.
Advent of Code 2021 solutions

Advent of Code 2021

Setup

The code below is not specific to any one Advent of Code problem, but instead is used by some or all of the problems.

defmodule AdventOfCode do
  def write_input_text(day_input, text_input) do
    text = Kino.Input.read(text_input) |> String.trim()
    day = Kino.Input.read(day_input)

    if day != 0 and day != nil and text != "" do
      if day < 1 or day > 25 do
        IO.puts("Unexpected day: #{day}. Should be between 1 and 25.")
        :error
      else
        File.write("/var/db/livebook/aoc2021/day#{day}.txt", text)
      end
    else
      :noop
    end
  end

  def read_input(day) when is_integer(day) do
    File.read!("/var/db/livebook/aoc2021/day#{day}.txt")
  end

  def read_input_lines(day) when is_integer(day) do
    read_input(day) |> String.trim() |> String.split("\n")
  end
end
{:module, AdventOfCode, <<70, 79, 82, 49, 0, 0, 11, ...>>, {:read_input_lines, 1}}
Mix.install([
  {:kino, "~> 0.4.1"},
  {:heap, "~> 2.0.2"},
  {:jason, "~> 1.2"}
])
Resolving Hex dependencies...
Dependency resolution completed:
New:
  heap 2.0.2
  jason 1.2.2
  kino 0.4.1
* Getting kino (Hex package)
* Getting heap (Hex package)
* Getting jason (Hex package)
==> heap
Compiling 4 files (.ex)
Generated heap app
==> kino
Compiling 19 files (.ex)
Generated kino app
==> jason
Compiling 8 files (.ex)
Generated jason app
:ok

Daily Preparation

Use the inputs below when starting a new day's problem to write the problem input to disk so that it can be read in later by the problem's code.

day_input = Kino.Input.number("Day") |> Kino.render()
text_input = Kino.Input.textarea("Input Text")

Run the cell below to write the new problem's input to disk.

AdventOfCode.write_input_text(day_input, text_input)
:noop

Day 1: Sonar Sweep

You're minding your own business on a ship at sea when the overboard alarm goes off! You rush to see if you can help. Apparently, one of the Elves tripped and accidentally sent the sleigh keys flying into the ocean!

Before you know it, you're inside a submarine the Elves keep ready for situations like this. It's covered in Christmas lights (because of course it is), and it even has an experimental antenna that should be able to track the keys if you can boost its signal strength high enough; there's a little meter that indicates the antenna's signal strength by displaying 0-50 stars.

Your instincts tell you that in order to save Christmas, you'll need to get all fifty stars by December 25th.

Collect stars by solving puzzles. Two puzzles will be made available on each day in the Advent calendar; the second puzzle is unlocked when you complete the first. Each puzzle grants one star. Good luck!

As the submarine drops below the surface of the ocean, it automatically performs a sonar sweep of the nearby sea floor. On a small screen, the sonar sweep report (your puzzle input) appears: each line is a measurement of the sea floor depth as the sweep looks further and further away from the submarine.

For example, suppose you had the following report:

199
200
208
210
200
207
240
269
260
263

This report indicates that, scanning outward from the submarine, the sonar sweep found depths of 199, 200, 208, 210, and so on.

The first order of business is to figure out how quickly the depth increases, just so you know what you're dealing with - you never know if the keys will get carried into deeper water by an ocean current or a fish or something.

To do this, count the number of times a depth measurement increases from the previous measurement. (There is no measurement before the first measurement.) In the example above, the changes are as follows:

199 (N/A - no previous measurement)
200 (increased)
208 (increased)
210 (increased)
200 (decreased)
207 (increased)
240 (increased)
269 (increased)
260 (decreased)
263 (increased)

In this example, there are 7 measurements that are larger than the previous measurement.

How many measurements are larger than the previous measurement?

defmodule Day1 do
  def read_input() do
    for line <- AdventOfCode.read_input_lines(1), do: String.to_integer(line)
  end

  def count_increases(depths) do
    Enum.drop(depths, 1)
    |> Enum.zip_with(depths, fn a, b -> a - b end)
    |> Enum.count(fn x -> x > 0 end)
  end

  def count_windowed_increases(depths) do
    windowed_sums =
      [depths, Enum.drop(depths, 1), Enum.drop(depths, 2)]
      |> Enum.zip_with(&Enum.sum/1)

    Enum.drop(windowed_sums, 1)
    |> Enum.zip_with(windowed_sums, fn a, b -> a - b end)
    |> Enum.count(fn x -> x > 0 end)
  end
end
{:module, Day1, <<70, 79, 82, 49, 0, 0, 11, ...>>, {:count_windowed_increases, 1}}
Day1.read_input()
|> Day1.count_increases()
1752

Part Two

Considering every single measurement isn't as useful as you expected: there's just too much noise in the data.

Instead, consider sums of a three-measurement sliding window. Again considering the above example:

199  A      
200  A B    
208  A B C  
210    B C D
200  E   C D
207  E F   D
240  E F G  
269    F G H
260      G H
263        H

Start by comparing the first and second three-measurement windows. The measurements in the first window are marked A (199, 200, 208); their sum is 199 + 200 + 208 = 607. The second window is marked B (200, 208, 210); its sum is 618. The sum of measurements in the second window is larger than the sum of the first, so this first comparison increased.

Your goal now is to count the number of times the sum of measurements in this sliding window increases from the previous sum. So, compare A with B, then compare B with C, then C with D, and so on. Stop when there aren't enough measurements left to create a new three-measurement sum.

In the above example, the sum of each three-measurement window is as follows:

A: 607 (N/A - no previous sum)
B: 618 (increased)
C: 618 (no change)
D: 617 (decreased)
E: 647 (increased)
F: 716 (increased)
G: 769 (increased)
H: 792 (increased)

In this example, there are 5 sums that are larger than the previous sum.

Consider sums of a three-measurement sliding window. How many sums are larger than the previous sum?

Day1.read_input()
|> Day1.count_windowed_increases()
1781

Day 2: Dive!

Now, you need to figure out how to pilot this thing.

It seems like the submarine can take a series of commands like forward 1, down 2, or up 3:

  • forward X increases the horizontal position by X units.
  • down X increases the depth by X units.
  • up X decreases the depth by X units.

Note that since you're on a submarine, down and up affect your depth, and so they have the opposite result of what you might expect.

The submarine seems to already have a planned course (your puzzle input). You should probably figure out where it's going. For example:

forward 5
down 5
forward 8
up 3
down 8
forward 2

Your horizontal position and depth both start at 0. The steps above would then modify them as follows:

  • forward 5 adds 5 to your horizontal position, a total of 5.
  • down 5 adds 5 to your depth, resulting in a value of 5.
  • forward 8 adds 8 to your horizontal position, a total of 13.
  • up 3 decreases your depth by 3, resulting in a value of 2.
  • down 8 adds 8 to your depth, resulting in a value of 10.
  • forward 2 adds 2 to your horizontal position, a total of 15.

After following these instructions, you would have a horizontal position of 15 and a depth of 10. (Multiplying these together produces 150.)

Calculate the horizontal position and depth you would have after following the planned course. What do you get if you multiply your final horizontal position by your final depth?

defmodule Day2 do
  def read_input() do
    for line <- AdventOfCode.read_input_lines(2) do
      case line do
        "forward " <> n ->
          {:forward, String.to_integer(n)}

        "down " <> n ->
          {:down, String.to_integer(n)}

        "up " <> n ->
          {:up, String.to_integer(n)}
      end
    end
  end

  def pilot(instrs, pos \\ {0, 0})

  def pilot([], pos), do: pos

  def pilot([instr | rest], pos) do
    pilot(rest, move(instr, pos))
  end

  def move({:forward, n}, {x, y}), do: {x + n, y}
  def move({:down, n}, {x, y}), do: {x, y + n}
  def move({:up, n}, {x, y}), do: {x, y - n}

  def pilot3(instrs, pos \\ {0, 0, 0})

  def pilot3([], pos), do: pos

  def pilot3([instr | rest], pos) do
    pilot3(rest, move3(instr, pos))
  end

  def move3({:forward, n}, {x, y, aim}), do: {x + n, y + aim * n, aim}
  def move3({:down, n}, {x, y, aim}), do: {x, y, aim + n}
  def move3({:up, n}, {x, y, aim}), do: {x, y, aim - n}
end
{:module, Day2, <<70, 79, 82, 49, 0, 0, 14, ...>>, {:move3, 2}}
{x, y} =
  Day2.read_input()
  |> Day2.pilot()

x * y
1714950

Part Two

Based on your calculations, the planned course doesn't seem to make any sense. You find the submarine manual and discover that the process is actually slightly more complicated.

In addition to horizontal position and depth, you'll also need to track a third value, aim, which also starts at 0. The commands also mean something entirely different than you first thought:

  • down X increases your aim by X units.
  • up X decreases your aim by X units.
  • forward X does two things:
    • It increases your horizontal position by X units.
    • It increases your depth by your aim multiplied by X.

Again note that since you're on a submarine, down and up do the opposite of what you might expect: "down" means aiming in the positive direction.

Now, the above example does something different:

  • forward 5 adds 5 to your horizontal position, a total of 5. Because your aim is 0, your depth does not change.
  • down 5 adds 5 to your aim, resulting in a value of 5.
  • forward 8 adds 8 to your horizontal position, a total of 13. Because your aim is 5, your depth increases by 8*5=40.
  • up 3 decreases your aim by 3, resulting in a value of 2.
  • down 8 adds 8 to your aim, resulting in a value of 10.
  • forward 2 adds 2 to your horizontal position, a total of 15. Because your aim is 10, your depth increases by 2*10=20 to a total of 60.

After following these new instructions, you would have a horizontal position of 15 and a depth of 60. (Multiplying these produces 900.)

Using this new interpretation of the commands, calculate the horizontal position and depth you would have after following the planned course. What do you get if you multiply your final horizontal position by your final depth?

{x, y, _aim} =
  Day2.read_input()
  |> Day2.pilot3()

x * y
1281977850

Day 3: Binary Diagnostic

The submarine has been making some odd creaking noises, so you ask it to produce a diagnostic report just in case.

The diagnostic report (your puzzle input) consists of a list of binary numbers which, when decoded properly, can tell you many useful things about the conditions of the submarine. The first parameter to check is the power consumption.

You need to use the binary numbers in the diagnostic report to generate two new binary numbers (called the gamma rate and the epsilon rate). The power consumption can then be found by multiplying the gamma rate by the epsilon rate.

Each bit in the gamma rate can be determined by finding the most common bit in the corresponding position of all numbers in the diagnostic report. For example, given the following diagnostic report:

00100
11110
10110
10111
10101
01111
00111
11100
10000
11001
00010
01010

Considering only the first bit of each number, there are five 0 bits and seven 1 bits. Since the most common bit is 1, the first bit of the gamma rate is 1.

The most common second bit of the numbers in the diagnostic report is 0, so the second bit of the gamma rate is 0.

The most common value of the third, fourth, and fifth bits are 1, 1, and 0, respectively, and so the final three bits of the gamma rate are 110.

So, the gamma rate is the binary number 10110, or 22 in decimal.

The epsilon rate is calculated in a similar way; rather than use the most common bit, the least common bit from each position is used. So, the epsilon rate is 01001, or 9 in decimal. Multiplying the gamma rate (22) by the epsilon rate (9) produces the power consumption, 198.

Use the binary numbers in your diagnostic report to calculate the gamma rate and epsilon rate, then multiply them together. What is the power consumption of the submarine? (Be sure to represent your answer in decimal, not binary.)

defmodule Day3 do
  use Bitwise

  def read_input() do
    for line <- AdventOfCode.read_input_lines(3) do
      String.to_integer(line, 2)
    end
  end

  def compute_rates(nums, digits \\ 12) do
    gamma_rate_digits =
      for i <- (digits - 1)..0//-1 do
        most_common_digit(nums, i)
      end

    epsilon_rate_digits = for x <- gamma_rate_digits, do: 1 - x

    {Integer.undigits(gamma_rate_digits, 2), Integer.undigits(epsilon_rate_digits, 2)}
  end

  def compute_ratings(nums, digits \\ 12) do
    {compute_rating(nums, digits - 1, 1), compute_rating(nums, digits - 1, 0)}
  end

  def compute_rating([n], _idx, _preferred), do: n

  def compute_rating(nums, idx, preferred) do
    most_common = most_common_digit(nums, idx)

    digit_to_use =
      cond do
        is_nil(most_common) -> preferred
        most_common == preferred -> 1
        true -> 0
      end

    new_nums = Enum.filter(nums, fn n -> (n >>> idx &&& 1) == digit_to_use end)
    compute_rating(new_nums, idx - 1, preferred)
  end

  defp most_common_digit(nums, i) do
    half_num_count = Enum.count(nums) / 2
    num_ones = Enum.count(nums, fn n -> (n >>> i &&& 1) == 1 end)

    cond do
      num_ones > half_num_count -> 1
      num_ones < half_num_count -> 0
      true -> nil
    end
  end
end
{:module, Day3, <<70, 79, 82, 49, 0, 0, 17, ...>>, {:most_common_digit, 2}}
{gamma, epsilon} =
  Day3.read_input()
  |> Day3.compute_rates()
{935, 3160}
gamma * epsilon
2954600

Part Two

Next, you should verify the life support rating, which can be determined by multiplying the oxygen generator rating by the CO2 scrubber rating.

Both the oxygen generator rating and the CO2 scrubber rating are values that can be found in your diagnostic report - finding them is the tricky part. Both values are located using a similar process that involves filtering out values until only one remains. Before searching for either rating value, start with the full list of binary numbers from your diagnostic report and consider just the first bit of those numbers. Then:

  • Keep only numbers selected by the bit criteria for the type of rating value for which you are searching. Discard numbers which do not match the bit criteria.
  • If you only have one number left, stop; this is the rating value for which you are searching.
  • Otherwise, repeat the process, considering the next bit to the right.

The bit criteria depends on which type of rating value you want to find:

  • To find oxygen generator rating, determine the most common value (0 or 1) in the current bit position, and keep only numbers with that bit in that position. If 0 and 1 are equally common, keep values with a 1 in the position being considered.
  • To find CO2 scrubber rating, determine the least common value (0 or 1) in the current bit position, and keep only numbers with that bit in that position. If 0 and 1 are equally common, keep values with a 0 in the position being considered.

For example, to determine the oxygen generator rating value using the same example diagnostic report from above:

  • Start with all 12 numbers and consider only the first bit of each number. There are more 1 bits (7) than 0 bits (5), so keep only the 7 numbers with a 1 in the first position: 11110, 10110, 10111, 10101, 11100, 10000, and 11001.
  • Then, consider the second bit of the 7 remaining numbers: there are more 0 bits (4) than 1 bits (3), so keep only the 4 numbers with a 0 in the second position: 10110, 10111, 10101, and 10000.
  • In the third position, three of the four numbers have a 1, so keep those three: 10110, 10111, and 10101.
  • In the fourth position, two of the three numbers have a 1, so keep those two: 10110 and 10111.
  • In the fifth position, there are an equal number of 0 bits and 1 bits (one each). So, to find the oxygen generator rating, keep the number with a 1 in that position: 10111.
  • As there is only one number left, stop; the oxygen generator rating is 10111, or 23 in decimal.

Then, to determine the CO2 scrubber rating value from the same example above:

  • Start again with all 12 numbers and consider only the first bit of each number. There are fewer 0 bits (5) than 1 bits (7), so keep only the 5 numbers with a 0 in the first position: 00100, 01111, 00111, 00010, and 01010.
  • Then, consider the second bit of the 5 remaining numbers: there are fewer 1 bits (2) than 0 bits (3), so keep only the 2 numbers with a 1 in the second position: 01111 and 01010.
  • In the third position, there are an equal number of 0 bits and 1 bits (one each). So, to find the CO2 scrubber rating, keep the number with a 0 in that position: 01010.
  • As there is only one number left, stop; the CO2 scrubber rating is 01010, or 10 in decimal.

Finally, to find the life support rating, multiply the oxygen generator rating (23) by the CO2 scrubber rating (10) to get 230.

Use the binary numbers in your diagnostic report to calculate the oxygen generator rating and CO2 scrubber rating, then multiply them together. What is the life support rating of the submarine? (Be sure to represent your answer in decimal, not binary.)

{oxygen, co2} =
  Day3.read_input()
  |> Day3.compute_ratings()
{573, 2902}
oxygen * co2
1662846

Day 4: Giant Squid

You're already almost 1.5km (almost a mile) below the surface of the ocean, already so deep that you can't see any sunlight. What you can see, however, is a giant squid that has attached itself to the outside of your submarine.

Maybe it wants to play bingo?

Bingo is played on a set of boards each consisting of a 5x5 grid of numbers. Numbers are chosen at random, and the chosen number is marked on all boards on which it appears. (Numbers may not appear on all boards.) If all numbers in any row or any column of a board are marked, that board wins. (Diagonals don't count.)

The submarine has a bingo subsystem to help passengers (currently, you and the giant squid) pass the time. It automatically generates a random order in which to draw numbers and a random set of boards (your puzzle input). For example:

7,4,9,5,11,17,23,2,0,14,21,24,10,16,13,6,15,25,12,22,18,20,8,19,3,26,1

22 13 17 11  0
 8  2 23  4 24
21  9 14 16  7
 6 10  3 18  5
 1 12 20 15 19

 3 15  0  2 22
 9 18 13 17  5
19  8  7 25 23
20 11 10 24  4
14 21 16 12  6

14 21 17 24  4
10 16 15  9 19
18  8 23 26 20
22 11 13  6  5
 2  0 12  3  7

After the first five numbers are drawn (7, 4, 9, 5, and 11), there are no winners, but the boards are marked as follows (shown here adjacent to each other to save space):

22 13 17 11  0         3 15  0  2 22        14 21 17 24  4
 8  2 23  4 24         9 18 13 17  5        10 16 15  9 19
21  9 14 16  7        19  8  7 25 23        18  8 23 26 20
 6 10  3 18  5        20 11 10 24  4        22 11 13  6  5
 1 12 20 15 19        14 21 16 12  6         2  0 12  3  7

After the next six numbers are drawn (17, 23, 2, 0, 14, and 21), there are still no winners:

22 13 17 11  0         3 15  0  2 22        14 21 17 24  4
 8  2 23  4 24         9 18 13 17  5        10 16 15  9 19
21  9 14 16  7        19  8  7 25 23        18  8 23 26 20
 6 10  3 18  5        20 11 10 24  4        22 11 13  6  5
 1 12 20 15 19        14 21 16 12  6         2  0 12  3  7

Finally, 24 is drawn:

22 13 17 11  0         3 15  0  2 22        14 21 17 24  4
 8  2 23  4 24         9 18 13 17  5        10 16 15  9 19
21  9 14 16  7        19  8  7 25 23        18  8 23 26 20
 6 10  3 18  5        20 11 10 24  4        22 11 13  6  5
 1 12 20 15 19        14 21 16 12  6         2  0 12  3  7

At this point, the third board wins because it has at least one complete row or column of marked numbers (in this case, the entire top row is marked: 14 21 17 24 4).

The score of the winning board can now be calculated. Start by finding the sum of all unmarked numbers on that board; in this case, the sum is 188. Then, multiply that sum by the number that was just called when the board won, 24, to get the final score, 188 * 24 = 4512.

To guarantee victory against the giant squid, figure out which board will win first. What will your final score be if you choose that board?

defmodule Day4 do
  def read_input() do
    [nums, "" | rest] = AdventOfCode.read_input_lines(4)

    nums =
      nums
      |> String.split(",")
      |> Enum.map(&String.to_integer/1)

    boards =
      rest
      |> Enum.filter(&(&1 != ""))
      |> Enum.chunk_every(5)
      |> Enum.map(fn board_lines ->
        Enum.flat_map(board_lines, fn line ->
          line
          |> String.split(" ", trim: true)
          |> Enum.map(&String.to_integer/1)
        end)
      end)

    {nums, boards}
  end

  def winning_board_score(boards, [next_num | remaining_nums]) do
    boards = mark_number_many(boards, next_num)

    case winning_boards(boards) do
      [] ->
        winning_board_score(boards, remaining_nums)

      [board | _rest] ->
        unmarked_total(board) * next_num
    end
  end

  def last_winning_board_score(boards, [next_num | remaining_nums]) do
    boards = mark_number_many(boards, next_num)

    case winning_boards(boards) do
      [] ->
        last_winning_board_score(boards, remaining_nums)

      winners ->
        remaining_boards =
          Enum.filter(boards, fn b ->
            Enum.all?(winners, &(&1 != b))
          end)

        if Enum.empty?(remaining_boards) do
          board = List.first(winners)
          unmarked_total(board) * next_num
        else
          last_winning_board_score(remaining_boards, remaining_nums)
        end
    end
  end

  def mark_number_many(boards, num) do
    for board <- boards, do: mark_number(board, num)
  end

  def mark_number(board, num) do
    for n <- board do
      if n == num do
        {:marked, n}
      else
        n
      end
    end
  end

  def winning_boards(boards) do
    Enum.filter(boards, &winning?/1)
  end

  def winning?(board) do
    board
    |> possible_wins()
    |> Enum.any?(&all_marked?/1)
  end

  def possible_wins(board) do
    Enum.concat([
      # rows
      Enum.chunk_every(board, 5),
      # columns
      for(i <- 0..4, do: board |> Enum.drop(i) |> Enum.take_every(5))
    ])
  end

  def all_marked?(nums) do
    Enum.all?(nums, fn
      {:marked, _} -> true
      _ -> false
    end)
  end

  def unmarked_total(board) do
    board
    |> Enum.map(fn
      {:marked, _} -> 0
      n -> n
    end)
    |> Enum.sum()
  end
end
{:module, Day4, <<70, 79, 82, 49, 0, 0, 25, ...>>, {:unmarked_total, 1}}
{nums, boards} = Day4.read_input()
Day4.winning_board_score(boards, nums)
4662

Part Two

On the other hand, it might be wise to try a different strategy: let the giant squid win.

You aren't sure how many bingo boards a giant squid could play at once, so rather than waste time counting its arms, the safe thing to do is to figure out which board will win last and choose that one. That way, no matter which boards it picks, it will win for sure.

In the above example, the second board is the last to win, which happens after 13 is eventually called and its middle column is completely marked. If you were to keep playing until this point, the second board would have a sum of unmarked numbers equal to 148 for a final score of 148 * 13 = 1924.

Figure out which board will win last. Once it wins, what would its final score be?

Day4.last_winning_board_score(boards, nums)
12080

Day 5: Hydrothermal Venture

You come across a field of hydrothermal vents on the ocean floor! These vents constantly produce large, opaque clouds, so it would be best to avoid them if possible.

They tend to form in lines; the submarine helpfully produces a list of nearby lines of vents (your puzzle input) for you to review. For example:

0,9 -> 5,9
8,0 -> 0,8
9,4 -> 3,4
2,2 -> 2,1
7,0 -> 7,4
6,4 -> 2,0
0,9 -> 2,9
3,4 -> 1,4
0,0 -> 8,8
5,5 -> 8,2

Each line of vents is given as a line segment in the format x1,y1 -> x2,y2 where x1,y1 are the coordinates of one end the line segment and x2,y2 are the coordinates of the other end. These line segments include the points at both ends. In other words:

  • An entry like 1,1 -> 1,3 covers points 1,1, 1,2, and 1,3.
  • An entry like 9,7 -> 7,7 covers points 9,7, 8,7, and 7,7.

For now, only consider horizontal and vertical lines: lines where either x1 = x2 or y1 = y2.

So, the horizontal and vertical lines from the above list would produce the following diagram:

.......1..
..1....1..
..1....1..
.......1..
.112111211
..........
..........
..........
..........
222111....

In this diagram, the top left corner is 0,0 and the bottom right corner is 9,9. Each position is shown as the number of lines which cover that point or . if no line covers that point. The top-left pair of 1s, for example, comes from 2,2 -> 2,1; the very bottom row is formed by the overlapping lines 0,9 -> 5,9 and 0,9 -> 2,9.

To avoid the most dangerous areas, you need to determine the number of points where at least two lines overlap. In the above example, this is anywhere in the diagram with a 2 or larger - a total of 5 points.

Consider only horizontal and vertical lines. At how many points do at least two lines overlap?

defmodule Day5 do
  def read_input() do
    for line <- AdventOfCode.read_input_lines(5) do
      [start_point, end_point] = line |> String.split(" -> ") |> Enum.map(&read_point/1)
      {start_point, end_point}
    end
  end

  defp read_point(str) do
    [x, y] = str |> String.split(",") |> Enum.map(&String.to_integer/1)
    {x, y}
  end

  def count_overlapping_points(lines) do
    lines
    |> Enum.flat_map(&line_points/1)
    |> Enum.reduce(%{}, fn point, points ->
      Map.update(points, point, 1, &(&1 + 2))
    end)
    |> Enum.count(fn {_k, v} -> v > 1 end)
  end

  def horizontal_or_vertical?(line) do
    horizontal?(line) or vertical?(line)
  end

  def horizontal?({{_, y1}, {_, y2}}), do: y1 == y2
  def vertical?({{x1, _}, {x2, _}}), do: x1 == x2

  def line_points({{x1, y1}, {x2, y2}} = line) do
    cond do
      horizontal?(line) ->
        for x <- x1..x2, do: {x, y1}

      vertical?(line) ->
        for y <- y1..y2, do: {x1, y}

      true ->
        for {x, y} <- Enum.zip(x1..x2, y1..y2), do: {x, y}
    end
  end
end
{:module, Day5, <<70, 79, 82, 49, 0, 0, 19, ...>>, {:line_points, 1}}
Day5.read_input()
|> Enum.filter(&Day5.horizontal_or_vertical?/1)
|> Day5.count_overlapping_points()
7269

Part Two

Unfortunately, considering only horizontal and vertical lines doesn't give you the full picture; you need to also consider diagonal lines.

Because of the limits of the hydrothermal vent mapping system, the lines in your list will only ever be horizontal, vertical, or a diagonal line at exactly 45 degrees. In other words:

  • An entry like 1,1 -> 3,3 covers points 1,1, 2,2, and 3,3.
  • An entry like 9,7 -> 7,9 covers points 9,7, 8,8, and 7,9.

Considering all lines from the above example would now produce the following diagram:

1.1....11.
.111...2..
..2.1.111.
...1.2.2..
.112313211
...1.2....
..1...1...
.1.....1..
1.......1.
222111....

You still need to determine the number of points where at least two lines overlap. In the above example, this is still anywhere in the diagram with a 2 or larger - now a total of 12 points.

Consider all of the lines. At how many points do at least two lines overlap?

Day5.read_input()
|> Day5.count_overlapping_points()
21140

Day 6: Lanternfish

The sea floor is getting steeper. Maybe the sleigh keys got carried this way?

A massive school of glowing lanternfish swims past. They must spawn quickly to reach such large numbers - maybe exponentially quickly? You should model their growth rate to be sure.

Although you know nothing about this specific species of lanternfish, you make some guesses about their attributes. Surely, each lanternfish creates a new lanternfish once every 7 days.

However, this process isn't necessarily synchronized between every lanternfish - one lanternfish might have 2 days left until it creates another lanternfish, while another might have 4. So, you can model each fish as a single number that represents the number of days until it creates a new lanternfish.

Furthermore, you reason, a new lanternfish would surely need slightly longer before it's capable of producing more lanternfish: two more days for its first cycle.

So, suppose you have a lanternfish with an internal timer value of 3:

  • After one day, its internal timer would become 2.
  • After another day, its internal timer would become 1.
  • After another day, its internal timer would become 0.
  • After another day, its internal timer would reset to 6, and it would create a new lanternfish with an internal timer of 8.
  • After another day, the first lanternfish would have an internal timer of 5, and the second lanternfish would have an internal timer of 7.

A lanternfish that creates a new fish resets its timer to 6, not 7 (because 0 is included as a valid timer value). The new lanternfish starts with an internal timer of 8 and does not start counting down until the next day.

Realizing what you're trying to do, the submarine automatically produces a list of the ages of several hundred nearby lanternfish (your puzzle input). For example, suppose you were given the following list:

3,4,3,1,2

This list means that the first fish has an internal timer of 3, the second fish has an internal timer of 4, and so on until the fifth fish, which has an internal timer of 2. Simulating these fish over several days would proceed as follows:

Initial state: 3,4,3,1,2
After  1 day:  2,3,2,0,1
After  2 days: 1,2,1,6,0,8
After  3 days: 0,1,0,5,6,7,8
After  4 days: 6,0,6,4,5,6,7,8,8
After  5 days: 5,6,5,3,4,5,6,7,7,8
After  6 days: 4,5,4,2,3,4,5,6,6,7
After  7 days: 3,4,3,1,2,3,4,5,5,6
After  8 days: 2,3,2,0,1,2,3,4,4,5
After  9 days: 1,2,1,6,0,1,2,3,3,4,8
After 10 days: 0,1,0,5,6,0,1,2,2,3,7,8
After 11 days: 6,0,6,4,5,6,0,1,1,2,6,7,8,8,8
After 12 days: 5,6,5,3,4,5,6,0,0,1,5,6,7,7,7,8,8
After 13 days: 4,5,4,2,3,4,5,6,6,0,4,5,6,6,6,7,7,8,8
After 14 days: 3,4,3,1,2,3,4,5,5,6,3,4,5,5,5,6,6,7,7,8
After 15 days: 2,3,2,0,1,2,3,4,4,5,2,3,4,4,4,5,5,6,6,7
After 16 days: 1,2,1,6,0,1,2,3,3,4,1,2,3,3,3,4,4,5,5,6,8
After 17 days: 0,1,0,5,6,0,1,2,2,3,0,1,2,2,2,3,3,4,4,5,7,8
After 18 days: 6,0,6,4,5,6,0,1,1,2,6,0,1,1,1,2,2,3,3,4,6,7,8,8,8,8

Each day, a 0 becomes a 6 and adds a new 8 to the end of the list, while each other number decreases by 1 if it was present at the start of the day.

In this example, after 18 days, there are a total of 26 fish. After 80 days, there would be a total of 5934.

Find a way to simulate lanternfish. How many lanternfish would there be after 80 days?

defmodule Day6 do
  def read_input() do
    AdventOfCode.read_input(6)
    |> String.trim()
    |> String.split(",")
    |> Enum.map(&String.to_integer/1)
    |> Enum.reduce(%{}, fn timer, map ->
      Map.update(map, timer, 1, &(&1 + 1))
    end)
  end

  def run(timers, 1) do
    new_fish_count = timers[0] || 0

    timers
    |> Map.new(fn {k, v} ->
      if k == 0 do
        {6, v}
      else
        {k - 1, v}
      end
    end)
    # special case 6 because it combines 0 and 7
    |> Map.put(6, (timers[7] || 0) + new_fish_count)
    # and add the new fish
    |> Map.put(8, new_fish_count)
  end

  def run(timers, n) do
    Enum.reduce(1..n, timers, fn _, timers -> run(timers, 1) end)
  end

  def count_fish(timers) do
    timers
    |> Enum.map(fn {_, v} -> v end)
    |> Enum.sum()
  end
end
{:module, Day6, <<70, 79, 82, 49, 0, 0, 13, ...>>, {:count_fish, 1}}
Day6.read_input()
%{1 => 209, 2 => 29, 3 => 19, 4 => 21, 5 => 22}
Day6.read_input()
|> Day6.run(80)
|> Day6.count_fish()
391888

Part Two

Suppose the lanternfish live forever and have unlimited food and space. Would they take over the entire ocean?

After 256 days in the example above, there would be a total of 26984457539 lanternfish!

How many lanternfish would there be after 256 days?

Day6.read_input()
|> Day6.run(256)
|> Day6.count_fish()
1754597645339

Day 7: The Treachery of Whales

A giant whale has decided your submarine is its next meal, and it's much faster than you are. There's nowhere to run!

Suddenly, a swarm of crabs (each in its own tiny submarine - it's too deep for them otherwise) zooms in to rescue you! They seem to be preparing to blast a hole in the ocean floor; sensors indicate a massive underground cave system just beyond where they're aiming!

The crab submarines all need to be aligned before they'll have enough power to blast a large enough hole for your submarine to get through. However, it doesn't look like they'll be aligned before the whale catches you! Maybe you can help?

There's one major catch - crab submarines can only move horizontally.

You quickly make a list of the horizontal position of each crab (your puzzle input). Crab submarines have limited fuel, so you need to find a way to make all of their horizontal positions match while requiring them to spend as little fuel as possible.

For example, consider the following horizontal positions:

16,1,2,0,4,2,7,1,2,14

This means there's a crab with horizontal position 16, a crab with horizontal position 1, and so on.

Each change of 1 step in horizontal position of a single crab costs 1 fuel. You could choose any horizontal position to align them all on, but the one that costs the least fuel is horizontal position 2:

  • Move from 16 to 2: 14 fuel
  • Move from 1 to 2: 1 fuel
  • Move from 2 to 2: 0 fuel
  • Move from 0 to 2: 2 fuel
  • Move from 4 to 2: 2 fuel
  • Move from 2 to 2: 0 fuel
  • Move from 7 to 2: 5 fuel
  • Move from 1 to 2: 1 fuel
  • Move from 2 to 2: 0 fuel
  • Move from 14 to 2: 12 fuel

This costs a total of 37 fuel. This is the cheapest possible outcome; more expensive outcomes include aligning at position 1 (41 fuel), position 3 (39 fuel), or position 10 (71 fuel).

Determine the horizontal position that the crabs can align to using the least fuel possible. How much fuel must they spend to align to that position?

defmodule Day7 do
  def read_input() do
    AdventOfCode.read_input(7)
    |> String.trim()
    |> String.split(",")
    |> Enum.map(&String.to_integer/1)
  end

  def total_fuel_used(nums) do
    midpoint = median(nums)

    nums
    |> Enum.map(&abs(&1 - midpoint))
    |> Enum.sum()
  end

  def median(nums) do
    count = Enum.count(nums)
    sorted = Enum.sort(nums)

    case rem(count, 2) do
      0 ->
        mid = div(count, 2)
        (Enum.at(sorted, mid) + Enum.at(sorted, mid - 1)) / 2

      1 ->
        Enum.at(sorted, div(count, 2))
    end
  end

  def total_fuel_used2(nums) do
    midpoint1 = floor(mean(nums))
    midpoint2 = midpoint1 + 1

    min(total_fuel_used2(nums, midpoint1), total_fuel_used2(nums, midpoint2))
  end

  def total_fuel_used2(nums, midpoint) do
    nums
    |> Enum.map(fn num ->
      n = abs(num - midpoint)
      # This is known as the triangular number series
      div(n * (n + 1), 2)
    end)
    |> Enum.sum()
  end

  def mean(nums) do
    Enum.sum(nums) / Enum.count(nums)
  end
end
{:module, Day7, <<70, 79, 82, 49, 0, 0, 14, ...>>, {:mean, 1}}
Day7.read_input()
|> Day7.total_fuel_used()
357353.0

Part Two

The crabs don't seem interested in your proposed solution. Perhaps you misunderstand crab engineering?

As it turns out, crab submarine engines don't burn fuel at a constant rate. Instead, each change of 1 step in horizontal position costs 1 more unit of fuel than the last: the first step costs 1, the second step costs 2, the third step costs 3, and so on.

As each crab moves, moving further becomes more expensive. This changes the best horizontal position to align them all on; in the example above, this becomes 5:

  • Move from 16 to 5: 66 fuel
  • Move from 1 to 5: 10 fuel
  • Move from 2 to 5: 6 fuel
  • Move from 0 to 5: 15 fuel
  • Move from 4 to 5: 1 fuel
  • Move from 2 to 5: 6 fuel
  • Move from 7 to 5: 3 fuel
  • Move from 1 to 5: 10 fuel
  • Move from 2 to 5: 6 fuel
  • Move from 14 to 5: 45 fuel

This costs a total of 168 fuel. This is the new cheapest possible outcome; the old alignment position (2) now costs 206 fuel instead.

Determine the horizontal position that the crabs can align to using the least fuel possible so they can make you an escape route! How much fuel must they spend to align to that position?

Day7.read_input()
|> Day7.total_fuel_used2()
104822130

Day 8: Seven Segment Search

You barely reach the safety of the cave when the whale smashes into the cave mouth, collapsing it. Sensors indicate another exit to this cave at a much greater depth, so you have no choice but to press on.

As your submarine slowly makes its way through the cave system, you notice that the four-digit seven-segment displays in your submarine are malfunctioning; they must have been damaged during the escape. You'll be in a lot of trouble without them, so you'd better figure out what's wrong.

Each digit of a seven-segment display is rendered by turning on or off any of seven segments named a through g:

  0:      1:      2:      3:      4:
 aaaa    ....    aaaa    aaaa    ....
b    c  .    c  .    c  .    c  b    c
b    c  .    c  .    c  .    c  b    c
 ....    ....    dddd    dddd    dddd
e    f  .    f  e    .  .    f  .    f
e    f  .    f  e    .  .    f  .    f
 gggg    ....    gggg    gggg    ....

  5:      6:      7:      8:      9:
 aaaa    aaaa    aaaa    aaaa    aaaa
b    .  b    .  .    c  b    c  b    c
b    .  b    .  .    c  b    c  b    c
 dddd    dddd    ....    dddd    dddd
.    f  e    f  .    f  e    f  .    f
.    f  e    f  .    f  e    f  .    f
 gggg    gggg    ....    gggg    gggg

So, to render a 1, only segments c and f would be turned on; the rest would be off. To render a 7, only segments a, c, and f would be turned on.

The problem is that the signals which control the segments have been mixed up on each display. The submarine is still trying to display numbers by producing output on signal wires a through g, but those wires are connected to segments randomly. Worse, the wire/segment connections are mixed up separately for each four-digit display! (All of the digits within a display use the same connections, though.)

So, you might know that only signal wires b and g are turned on, but that doesn't mean segments b and g are turned on: the only digit that uses two segments is 1, so it must mean segments c and f are meant to be on. With just that information, you still can't tell which wire (b/g) goes to which segment (c/f). For that, you'll need to collect more information.

For each display, you watch the changing signals for a while, make a note of all ten unique signal patterns you see, and then write down a single four digit output value (your puzzle input). Using the signal patterns, you should be able to work out which pattern corresponds to which digit.

For example, here is what you might see in a single entry in your notes:

acedgfb cdfbe gcdfa fbcad dab cefabd cdfgeb eafb cagedb ab |
cdfeb fcadb cdfeb cdbaf

(The entry is wrapped here to two lines so it fits; in your notes, it will all be on a single line.)

Each entry consists of ten unique signal patterns, a | delimiter, and finally the four digit output value. Within an entry, the same wire/segment connections are used (but you don't know what the connections actually are). The unique signal patterns correspond to the ten different ways the submarine tries to render a digit using the current wire/segment connections. Because 7 is the only digit that uses three segments, dab in the above example means that to render a 7, signal lines d, a, and b are on. Because 4 is the only digit that uses four segments, eafb means that to render a 4, signal lines e, a, f, and b are on.

Using this information, you should be able to work out which combination of signal wires corresponds to each of the ten digits. Then, you can decode the four digit output value. Unfortunately, in the above example, all of the digits in the output value (cdfeb fcadb cdfeb cdbaf) use five segments and are more difficult to deduce.

For now, focus on the easy digits. Consider this larger example:

be cfbegad cbdgef fgaecd cgeb fdcge agebfd fecdb fabcd edb |
fdgacbe cefdb cefbgd gcbe
edbfga begcd cbg gc gcadebf fbgde acbgfd abcde gfcbed gfec |
fcgedb cgb dgebacf gc
fgaebd cg bdaec gdafb agbcfd gdcbef bgcad gfac gcb cdgabef |
cg cg fdcagb cbg
fbegcd cbd adcefb dageb afcb bc aefdc ecdab fgdeca fcdbega |
efabcd cedba gadfec cb
aecbfdg fbg gf bafeg dbefa fcge gcbea fcaegb dgceab fcbdga |
gecf egdcabf bgf bfgea
fgeab ca afcebg bdacfeg cfaedg gcfdb baec bfadeg bafgc acf |
gebdcfa ecba ca fadegcb
dbcfg fgd bdegcaf fgec aegbdf ecdfab fbedc dacgb gdcebf gf |
cefg dcbef fcge gbcadfe
bdfegc cbegaf gecbf dfcage bdacg ed bedf ced adcbefg gebcd |
ed bcgafe cdgba cbgef
egadfb cdbfeg cegd fecab cgb gbdefca cg fgcdab egfdb bfceg |
gbdfcae bgc cg cgb
gcafb gcf dcaebfg ecagb gf abcdeg gaef cafbge fdbac fegbdc |
fgae cfgab fg bagce

Because the digits 1, 4, 7, and 8 each use a unique number of segments, you should be able to tell which combinations of signals correspond to those digits. Counting only digits in the output values (the part after | on each line), in the above example, there are 26 instances of digits that use a unique number of segments (highlighted above).

In the output values, how many times do digits 1, 4, 7, or 8 appear?

defmodule Day8 do
  def read_input() do
    for line <- AdventOfCode.read_input_lines(8) do
      [digits, outputs] = String.split(line, " | ")
      digits = String.split(digits, " ")
      outputs = String.split(outputs, " ")
      {digits, outputs}
    end
  end

  def count_easy_digits(entries) do
    entries
    |> Enum.map(fn {_, outputs} ->
      Enum.count(outputs, &easy_digit?/1)
    end)
    |> Enum.sum()
  end

  def easy_digit?(digit) do
    l = String.length(digit)
    l == 2 || l == 3 || l == 4 || l == 7
  end

  def sum_entries(entries) do
    entries
    |> Enum.map(&decode_entry/1)
    |> Enum.sum()
  end

  def decode_entry({digits, output}) do
    decoder = new_decoder(digits)

    output
    |> Enum.map(&to_number(decoder.(&1)))
    |> Integer.undigits()
  end

  def new_decoder(digits) do
    freqs =
      digits
      |> Enum.flat_map(&String.codepoints/1)
      |> Enum.frequencies()

    by_freq =
      ~w(a b c d e f g)
      |> Enum.group_by(fn wire -> freqs[wire] end)

    wire_a = find_wire_a(digits)

    wire_c =
      by_freq[8]
      |> Enum.find(&(&1 != wire_a))

    wire_d = find_wire_d(digits, by_freq)

    wire_g =
      by_freq[7]
      |> Enum.find(&(&1 != wire_d))

    decode_map = %{
      List.first(by_freq[4]) => "e",
      List.first(by_freq[6]) => "b",
      List.first(by_freq[9]) => "f",
      wire_a => "a",
      wire_c => "c",
      wire_d => "d",
      wire_g => "g"
    }

    fn digit ->
      digit
      |> String.codepoints()
      |> Enum.map(&Map.fetch!(decode_map, &1))
      |> Enum.sort()
      |> Enum.join()
    end
  end

  def find_wire_a(digits) do
    seven = get_digit_wires(digits, 3)
    one = get_digit_wires(digits, 2)

    seven
    |> MapSet.difference(one)
    |> MapSet.to_list()
    |> List.first()
  end

  def find_wire_d(digits, by_freq) do
    four = get_digit_wires(digits, 4)

    by_freq[7]
    |> Enum.find(&MapSet.member?(four, &1))
  end

  def get_digit_wires(digits, count) do
    digits
    |> Enum.find(&(String.length(&1) == count))
    |> String.codepoints()
    |> MapSet.new()
  end

  def to_number("abcefg"), do: 0
  def to_number("cf"), do: 1
  def to_number("acdeg"), do: 2
  def to_number("acdfg"), do: 3
  def to_number("bcdf"), do: 4
  def to_number("abdfg"), do: 5
  def to_number("abdefg"), do: 6
  def to_number("acf"), do: 7
  def to_number("abcdefg"), do: 8
  def to_number("abcdfg"), do: 9
end
{:module, Day8, <<70, 79, 82, 49, 0, 0, 28, ...>>, {:to_number, 1}}
Day8.read_input()
|> Day8.count_easy_digits()
532

Part Two

Through a little deduction, you should now be able to determine the remaining digits. Consider again the first example above:

acedgfb cdfbe gcdfa fbcad dab cefabd cdfgeb eafb cagedb ab |
cdfeb fcadb cdfeb cdbaf

After some careful analysis, the mapping between signal wires and segments only make sense in the following configuration:

 dddd
e    a
e    a
 ffff
g    b
g    b
 cccc

So, the unique signal patterns would correspond to the following digits:

acedgfb: 8
cdfbe: 5
gcdfa: 2
fbcad: 3
dab: 7
cefabd: 9
cdfgeb: 6
eafb: 4
cagedb: 0
ab: 1

Then, the four digits of the output value can be decoded:

cdfeb: 5
fcadb: 3
cdfeb: 5
cdbaf: 3

Therefore, the output value for this entry is 5353.

Following this same process for each entry in the second, larger example above, the output value of each entry can be determined:

fdgacbe cefdb cefbgd gcbe: 8394
fcgedb cgb dgebacf gc: 9781
cg cg fdcagb cbg: 1197
efabcd cedba gadfec cb: 9361
gecf egdcabf bgf bfgea: 4873
gebdcfa ecba ca fadegcb: 8418
cefg dcbef fcge gbcadfe: 4548
ed bcgafe cdgba cbgef: 1625
gbdfcae bgc cg cgb: 8717
fgae cfgab fg bagce: 4315

Adding all of the output values in this larger example produces 61229.

For each entry, determine all of the wire/segment connections and decode the four-digit output values. What do you get if you add up all of the output values?

Day8.read_input()
|> Day8.sum_entries()
1011284

Day 9: Smoke Basin

These caves seem to be lava tubes. Parts are even still volcanically active; small hydrothermal vents release smoke into the caves that slowly settles like rain.

If you can model how the smoke flows through the caves, you might be able to avoid it and be that much safer. The submarine generates a heightmap of the floor of the nearby caves for you (your puzzle input).

Smoke flows to the lowest point of the area it's in. For example, consider the following heightmap:

2199943210
3987894921
9856789892
8767896789
9899965678

Each number corresponds to the height of a particular location, where 9 is the highest and 0 is the lowest a location can be.

Your first goal is to find the low points - the locations that are lower than any of its adjacent locations. Most locations have four adjacent locations (up, down, left, and right); locations on the edge or corner of the map have three or two adjacent locations, respectively. (Diagonal locations do not count as adjacent.)

In the above example, there are four low points, all highlighted: two are in the first row (a 1 and a 0), one is in the third row (a 5), and one is in the bottom row (also a 5). All other locations on the heightmap have some lower adjacent location, and so are not low points.

The risk level of a low point is 1 plus its height. In the above example, the risk levels of the low points are 2, 1, 6, and 6. The sum of the risk levels of all low points in the heightmap is therefore 15.

Find all of the low points on your heightmap. What is the sum of the risk levels of all low points on your heightmap?

defmodule Day9 do
  def read_input() do
    points =
      for {line, y} <- Enum.with_index(AdventOfCode.read_input_lines(9)) do
        nums =
          line
          |> String.codepoints()
          |> Enum.map(&String.to_integer/1)

        for {height, x} <- Enum.with_index(nums) do
          {{x, y}, height}
        end
      end

    points
    |> List.flatten()
    |> Map.new()
  end

  def low_point_risk_levels(points) do
    points
    |> low_points()
    |> Enum.map(fn {_, height} -> height + 1 end)
  end

  def low_points(points) do
    Enum.filter(points, &low_point?(points, &1))
  end

  def low_point?(points, {loc, height}) do
    adjacent_values(points, loc)
    |> Enum.all?(fn {_, h} -> h > height end)
  end

  def adjacent_values(points, loc) do
    adjacent_points(loc)
    |> Enum.map(fn loc2 -> {loc2, Map.get(points, loc2)} end)
    |> Enum.filter(fn {_, h} -> h != nil end)
  end

  def adjacent_points({x, y}) do
    [{x - 1, y}, {x, y - 1}, {x + 1, y}, {x, y + 1}]
  end

  def basin_sizes(points) do
    points
    |> low_points()
    |> Enum.map(fn {loc, _} -> basin_size(points, loc) end)
  end

  def basin_size(points, loc) do
    expand_basin(points, loc, MapSet.new())
    |> MapSet.size()
  end

  def expand_basin(points, loc, used_points) do
    if MapSet.member?(used_points, loc) do
      used_points
    else
      used_points = MapSet.put(used_points, loc)
      height = Map.fetch!(points, loc)

      adjacent_values(points, loc)
      |> Enum.filter(fn {_, h} -> h != 9 and h > height end)
      |> Enum.reduce(used_points, fn {l, _}, used_points ->
        expand_basin(points, l, used_points)
      end)
    end
  end
end
{:module, Day9, <<70, 79, 82, 49, 0, 0, 23, ...>>, {:expand_basin, 3}}
Day9.read_input()
|> Day9.low_point_risk_levels()
|> Enum.sum()
526

Part Two

Next, you need to find the largest basins so you know what areas are most important to avoid.

A basin is all locations that eventually flow downward to a single low point. Therefore, every low point has a basin, although some basins are very small. Locations of height 9 do not count as being in any basin, and all other locations will always be part of exactly one basin.

The size of a basin is the number of locations within the basin, including the low point. The example above has four basins.

The top-left basin, size 3:

2199943210
3987894921
9856789892
8767896789
9899965678

The top-right basin, size 9:

2199943210
3987894921
9856789892
8767896789
9899965678

The middle basin, size 14:

2199943210
3987894921
9856789892
8767896789
9899965678

The bottom-right basin, size 9:

2199943210
3987894921
9856789892
8767896789
9899965678

Find the three largest basins and multiply their sizes together. In the above example, this is 9 * 14 * 9 = 1134.

What do you get if you multiply together the sizes of the three largest basins?

Day9.read_input()
|> Day9.basin_sizes()
|> Enum.sort(:desc)
|> Enum.take(3)
|> Enum.product()
1123524

Day 10: Syntax Scoring

You ask the submarine to determine the best route out of the deep-sea cave, but it only replies:

Syntax error in navigation subsystem on line: all of them All of them?! The damage is worse than you thought. You bring up a copy of the navigation subsystem (your puzzle input).

The navigation subsystem syntax is made of several lines containing chunks. There are one or more chunks on each line, and chunks contain zero or more other chunks. Adjacent chunks are not separated by any delimiter; if one chunk stops, the next chunk (if any) can immediately start. Every chunk must open and close with one of four legal pairs of matching characters:

  • If a chunk opens with (, it must close with ).
  • If a chunk opens with [, it must close with ].
  • If a chunk opens with {, it must close with }.
  • If a chunk opens with <, it must close with >.

So, () is a legal chunk that contains no other chunks, as is []. More complex but valid chunks include ([]), {()()()}, <([{}])>, [<>({}){}[([])<>]], and even (((((((((()))))))))).

Some lines are incomplete, but others are corrupted. Find and discard the corrupted lines first.

A corrupted line is one where a chunk closes with the wrong character - that is, where the characters it opens and closes with do not form one of the four legal pairs listed above.

Examples of corrupted chunks include (], {()()()>, (((()))}, and <([]){()}[{}]). Such a chunk can appear anywhere within a line, and its presence causes the whole line to be considered corrupted.

For example, consider the following navigation subsystem:

[({(<(())[]>[[{[]{<()<>>
[(()[<>])]({[<{<<[]>>(
{([(<{}[<>[]}>{[]{[(<()>
(((({<>}<{<{<>}{[]{[]{}
[[<[([]))<([[{}[[()]]]
[{[{({}]{}}([{[{{{}}([]
{<[[]]>}<{[{[{[]{()[[[]
[<(<(<(<{}))><([]([]()
<{([([[(<>()){}]>(<<{{
<{([{{}}[<[[[<>{}]]]>[]]

Some of the lines aren't corrupted, just incomplete; you can ignore these lines for now. The remaining five lines are corrupted:

{([(<{}[<>[]}>{[]{[(<()> - Expected ], but found } instead.
[[<[([]))<([[{}[[()]]] - Expected ], but found ) instead.
[{[{({}]{}}([{[{{{}}([] - Expected ), but found ] instead.
[<(<(<(<{}))><([]([]() - Expected >, but found ) instead.
<{([([[(<>()){}]>(<<{{ - Expected ], but found > instead.

Stop at the first incorrect closing character on each corrupted line.

Did you know that syntax checkers actually have contests to see who can get the high score for syntax errors in a file? It's true! To calculate the syntax error score for a line, take the first illegal character on the line and look it up in the following table:

  • ): 3 points.
  • ]: 57 points.
  • }: 1197 points.
  • >: 25137 points.

In the above example, an illegal ) was found twice (2*3 = 6 points), an illegal ] was found once (57 points), an illegal } was found once (1197 points), and an illegal > was found once (25137 points). So, the total syntax error score for this file is 6+57+1197+25137 = 26397 points!

Find the first illegal character in each corrupted line of the navigation subsystem. What is the total syntax error score for those errors?

defmodule Day10 do
  def read_input() do
    AdventOfCode.read_input_lines(10)
  end

  def lines_score(lines) do
    lines
    |> Enum.map(&line_score/1)
    |> Enum.sum()
  end

  def line_score(line) do
    case find_corrupt_closer(line) do
      nil -> 0
      closer -> error_score(closer)
    end
  end

  def find_corrupt_closer(line) do
    case parse_line(line, []) do
      {:error, {:unexpected_closer, closer, _opener}} ->
        closer

      _ ->
        nil
    end
  end

  defguardp is_closer(term) when term == ")" or term == "]" or term == "}" or term == ">"
  defguardp is_opener(term) when term == "(" or term == "[" or term == "{" or term == "<"

  def parse_line("", []), do: :ok
  def parse_line("", openers), do: {:error, {:incomplete, openers}}

  def parse_line(")" <> line, ["(" | openers]), do: parse_line(line, openers)
  def parse_line("]" <> line, ["[" | openers]), do: parse_line(line, openers)
  def parse_line("}" <> line, ["{" | openers]), do: parse_line(line, openers)
  def parse_line(">" <> line, ["<" | openers]), do: parse_line(line, openers)

  # if this case matches, then the closing character didn't match the opener that was expected
  def parse_line(<<char::binary-size(1), _line::binary>>, [opener | _openers])
      when is_closer(char) do
    {:error, {:unexpected_closer, char, opener}}
  end

  def parse_line(<<opener::binary-size(1), line::binary>>, openers) when is_opener(opener) do
    parse_line(line, [opener | openers])
  end

  def error_score(")"), do: 3
  def error_score("]"), do: 57
  def error_score("}"), do: 1197
  def error_score(">"), do: 25137

  def lines_autocomplete_winner(lines) do
    sorted_scores =
      lines
      |> Enum.map(&line_autocomplete_score/1)
      |> Enum.filter(&(&1 != 0))
      |> Enum.sort()

    Enum.at(sorted_scores, div(Enum.count(sorted_scores), 2))
  end

  def line_autocomplete_score(line) do
    case parse_line(line, []) do
      :ok ->
        raise "none of the lines should be complete"

      {:error, {:unexpected_closer, _, _}} ->
        0

      {:error, {:incomplete, openers}} ->
        autocomplete_score(openers)
    end
  end

  def autocomplete_score(openers) do
    Enum.reduce(openers, 0, fn opener, score ->
      next_score =
        case opener do
          "(" -> 1
          "[" -> 2
          "{" -> 3
          "<" -> 4
        end

      5 * score + next_score
    end)
  end
end
{:module, Day10, <<70, 79, 82, 49, 0, 0, 23, ...>>, {:autocomplete_score, 1}}
Day10.read_input()
|> Day10.lines_score()
392043

Part Two

Now, discard the corrupted lines. The remaining lines are incomplete.

Incomplete lines don't have any incorrect characters - instead, they're missing some closing characters at the end of the line. To repair the navigation subsystem, you just need to figure out the sequence of closing characters that complete all open chunks in the line.

You can only use closing characters (), ], }, or >), and you must add them in the correct order so that only legal pairs are formed and all chunks end up closed.

In the example above, there are five incomplete lines:

  • [({(<(())[]>[[{[]{<()<>> - Complete by adding }}]])})].
  • [(()[<>])]({[<{<<[]>>( - Complete by adding )}>]}).
  • (((({<>}<{<{<>}{[]{[]{} - Complete by adding }}>}>)))).
  • {<[[]]>}<{[{[{[]{()[[[] - Complete by adding ]]}}]}]}>.
  • <{([{{}}[<[[[<>{}]]]>[]] - Complete by adding ])}>.

Did you know that autocomplete tools also have contests? It's true! The score is determined by considering the completion string character-by-character. Start with a total score of 0. Then, for each character, multiply the total score by 5 and then increase the total score by the point value given for the character in the following table:

  • ): 1 point.
  • ]: 2 points.
  • }: 3 points.
  • >: 4 points.

So, the last completion string above - ])}> - would be scored as follows:

  • Start with a total score of 0.
  • Multiply the total score by 5 to get 0, then add the value of ] (2) to get a new total score of 2.
  • Multiply the total score by 5 to get 10, then add the value of ) (1) to get a new total score of 11.
  • Multiply the total score by 5 to get 55, then add the value of } (3) to get a new total score of 58.
  • Multiply the total score by 5 to get 290, then add the value of > (4) to get a new total score of 294.

The five lines' completion strings have total scores as follows:

  • }}]])})] - 288957 total points.
  • )}>]}) - 5566 total points.
  • }}>}>)))) - 1480781 total points.
  • ]]}}]}]}> - 995444 total points.
  • ])}> - 294 total points.

Autocomplete tools are an odd bunch: the winner is found by sorting all of the scores and then taking the middle score. (There will always be an odd number of scores to consider.) In this example, the middle score is 288957 because there are the same number of scores smaller and larger than it.

Find the completion string for each incomplete line, score the completion strings, and sort the scores. What is the middle score?

Day10.read_input()
|> Day10.lines_autocomplete_winner()
1605968119

Day 11: Dumbo Octopus

You enter a large cavern full of rare bioluminescent dumbo octopuses! They seem to not like the Christmas lights on your submarine, so you turn them off for now.

There are 100 octopuses arranged neatly in a 10 by 10 grid. Each octopus slowly gains energy over time and flashes brightly for a moment when its energy is full. Although your lights are off, maybe you could navigate through the cave without disturbing the octopuses if you could predict when the flashes of light will happen.

Each octopus has an energy level - your submarine can remotely measure the energy level of each octopus (your puzzle input). For example:

5483143223
2745854711
5264556173
6141336146
6357385478
4167524645
2176841721
6882881134
4846848554
5283751526

The energy level of each octopus is a value between 0 and 9. Here, the top-left octopus has an energy level of 5, the bottom-right one has an energy level of 6, and so on.

You can model the energy levels and flashes of light in steps. During a single step, the following occurs:

  • First, the energy level of each octopus increases by 1.
  • Then, any octopus with an energy level greater than 9 flashes. This increases the energy level of all adjacent octopuses by 1, including octopuses that are diagonally adjacent. If this causes an octopus to have an energy level greater than 9, it also flashes. This process continues as long as new octopuses keep having their energy level increased beyond 9. (An octopus can only flash at most once per step.)
  • Finally, any octopus that flashed during this step has its energy level set to 0, as it used all of its energy to flash.

Adjacent flashes can cause an octopus to flash on a step even if it begins that step with very little energy. Consider the middle octopus with 1 energy in this situation:

Before any steps:
11111
19991
19191
19991
11111

After step 1:
34543
40004
50005
40004
34543

After step 2:
45654
51115
61116
51115
45654

An octopus is highlighted when it flashed during the given step.

Here is how the larger example above progresses:

Before any steps:
5483143223
2745854711
5264556173
6141336146
6357385478
4167524645
2176841721
6882881134
4846848554
5283751526

After step 1:
6594254334
3856965822
6375667284
7252447257
7468496589
5278635756
3287952832
7993992245
5957959665
6394862637

After step 2:
8807476555
5089087054
8597889608
8485769600
8700908800
6600088989
6800005943
0000007456
9000000876
8700006848

After step 3:
0050900866
8500800575
9900000039
9700000041
9935080063
7712300000
7911250009
2211130000
0421125000
0021119000

After step 4:
2263031977
0923031697
0032221150
0041111163
0076191174
0053411122
0042361120
5532241122
1532247211
1132230211

After step 5:
4484144000
2044144000
2253333493
1152333274
1187303285
1164633233
1153472231
6643352233
2643358322
2243341322

After step 6:
5595255111
3155255222
3364444605
2263444496
2298414396
2275744344
2264583342
7754463344
3754469433
3354452433

After step 7:
6707366222
4377366333
4475555827
3496655709
3500625609
3509955566
3486694453
8865585555
4865580644
4465574644

After step 8:
7818477333
5488477444
5697666949
4608766830
4734946730
4740097688
6900007564
0000009666
8000004755
6800007755

After step 9:
9060000644
7800000976
6900000080
5840000082
5858000093
6962400000
8021250009
2221130009
9111128097
7911119976

After step 10:
0481112976
0031112009
0041112504
0081111406
0099111306
0093511233
0442361130
5532252350
0532250600
0032240000

After step 10, there have been a total of 204 flashes. Fast forwarding, here is the same configuration every 10 steps:

After step 20:
3936556452
5686556806
4496555690
4448655580
4456865570
5680086577
7000009896
0000000344
6000000364
4600009543

After step 30:
0643334118
4253334611
3374333458
2225333337
2229333338
2276733333
2754574565
5544458511
9444447111
7944446119

After step 40:
6211111981
0421111119
0042111115
0003111115
0003111116
0065611111
0532351111
3322234597
2222222976
2222222762

After step 50:
9655556447
4865556805
4486555690
4458655580
4574865570
5700086566
6000009887
8000000533
6800000633
5680000538

After step 60:
2533334200
2743334640
2264333458
2225333337
2225333338
2287833333
3854573455
1854458611
1175447111
1115446111

After step 70:
8211111164
0421111166
0042111114
0004211115
0000211116
0065611111
0532351111
7322235117
5722223475
4572222754

After step 80:
1755555697
5965555609
4486555680
4458655580
4570865570
5700086566
7000008666
0000000990
0000000800
0000000000

After step 90:
7433333522
2643333522
2264333458
2226433337
2222433338
2287833333
2854573333
4854458333
3387779333
3333333333

After step 100:
0397666866
0749766918
0053976933
0004297822
0004229892
0053222877
0532222966
9322228966
7922286866
6789998766

After 100 steps, there have been a total of 1656 flashes.

Given the starting energy levels of the dumbo octopuses in your cavern, simulate 100 steps. How many total flashes are there after 100 steps?

defmodule Day11 do
  # this is basically the same thing as Day9.read_input/0
  def read_input() do
    points =
      for {line, y} <- Enum.with_index(AdventOfCode.read_input_lines(11)) do
        nums =
          line
          |> String.codepoints()
          |> Enum.map(&String.to_integer/1)

        for {energy, x} <- Enum.with_index(nums) do
          {{x, y}, energy}
        end
      end

    points
    |> List.flatten()
    |> Map.new()
  end

  def count_flashes(points, turns) do
    {_, flashes} =
      Enum.reduce(1..turns, {points, 0}, fn _, {points, flashes} ->
        {points, new_flashes} = step(points)
        {points, flashes + new_flashes}
      end)

    flashes
  end

  def step(points) do
    {points, flash_count} =
      points
      |> Enum.map(fn {point, energy} -> {point, energy + 1} end)
      |> Map.new()
      |> flash_octopuses()

    {reset_flashed_octopuses(points), flash_count}
  end

  def flash_octopuses(points, already_flashed \\ MapSet.new()) do
    points_to_flash =
      points
      |> Enum.filter(fn {point, energy} ->
        energy > 9 and not MapSet.member?(already_flashed, point)
      end)

    if Enum.empty?(points_to_flash) do
      {points, Enum.count(already_flashed)}
    else
      {points, already_flashed} =
        points_to_flash
        |> Enum.reduce({points, already_flashed}, fn {point, _energy},
                                                     {points, already_flashed} ->
          {flash_octopus(points, point), MapSet.put(already_flashed, point)}
        end)

      flash_octopuses(points, already_flashed)
    end
  end

  def flash_octopus(points, point) do
    point
    |> adjacent_points()
    |> Enum.filter(&Map.has_key?(points, &1))
    |> Enum.reduce(points, fn neighbor, points ->
      Map.update!(points, neighbor, &(&1 + 1))
    end)
  end

  def adjacent_points({x, y}) do
    [
      {x - 1, y - 1},
      {x, y - 1},
      {x + 1, y - 1},
      {x - 1, y},
      {x + 1, y},
      {x - 1, y + 1},
      {x, y + 1},
      {x + 1, y + 1}
    ]
  end

  def reset_flashed_octopuses(points) do
    points
    |> Enum.map(fn {point, energy} ->
      if energy > 9 do
        {point, 0}
      else
        {point, energy}
      end
    end)
    |> Map.new()
  end

  def find_all_flashing(points, turn_count \\ 1) do
    {points, flash_count} = step(points)

    if flash_count == 100 do
      turn_count
    else
      find_all_flashing(points, turn_count + 1)
    end
  end

  def inspect(points) do
    ordered_points =
      points
      |> Map.keys()
      |> Enum.sort_by(fn {x, y} -> {y, x} end)

    for {x, y} <- ordered_points, into: IO.stream(:stdio, :line) do
      if x == 0 do
        "\n#{Map.get(points, {x, y})}"
      else
        "#{Map.get(points, {x, y})}"
      end
    end

    IO.puts("\n")

    points
  end
end
{:module, Day11, <<70, 79, 82, 49, 0, 0, 31, ...>>, {:inspect, 1}}
Day11.read_input()
|> Day11.count_flashes(100)
1702

Part Two

It seems like the individual flashes aren't bright enough to navigate. However, you might have a better option: the flashes seem to be synchronizing!

In the example above, the first time all octopuses flash simultaneously is step 195:

After step 193:
5877777777
8877777777
7777777777
7777777777
7777777777
7777777777
7777777777
7777777777
7777777777
7777777777

After step 194:
6988888888
9988888888
8888888888
8888888888
8888888888
8888888888
8888888888
8888888888
8888888888
8888888888

After step 195:
0000000000
0000000000
0000000000
0000000000
0000000000
0000000000
0000000000
0000000000
0000000000
0000000000

If you can calculate the exact moments when the octopuses will all flash simultaneously, you should be able to navigate through the cavern. What is the first step during which all octopuses flash?

Day11.read_input()
|> Day11.find_all_flashing()
251

Day 12: Passage Pathing

With your submarine's subterranean subsystems subsisting suboptimally, the only way you're getting out of this cave anytime soon is by finding a path yourself. Not just a path - the only way to know if you've found the best path is to find all of them.

Fortunately, the sensors are still mostly working, and so you build a rough map of the remaining caves (your puzzle input). For example:

start-A
start-b
A-c
A-b
b-d
A-end
b-end

This is a list of how all of the caves are connected. You start in the cave named start, and your destination is the cave named end. An entry like b-d means that cave b is connected to cave d - that is, you can move between them.

So, the above cave system looks roughly like this:

    start
    /   \
c--A-----b--d
    \   /
     end

Your goal is to find the number of distinct paths that start at start, end at end, and don't visit small caves more than once. There are two types of caves: big caves (written in uppercase, like A) and small caves (written in lowercase, like b). It would be a waste of time to visit any small cave more than once, but big caves are large enough that it might be worth visiting them multiple times. So, all paths you find should visit small caves at most once, and can visit big caves any number of times.

Given these rules, there are 10 paths through this example cave system:

start,A,b,A,c,A,end
start,A,b,A,end
start,A,b,end
start,A,c,A,b,A,end
start,A,c,A,b,end
start,A,c,A,end
start,A,end
start,b,A,c,A,end
start,b,A,end
start,b,end

(Each line in the above list corresponds to a single path; the caves visited by that path are listed in the order they are visited and separated by commas.)

Note that in this cave system, cave d is never visited by any path: to do so, cave b would need to be visited twice (once on the way to cave d and a second time when returning from cave d), and since cave b is small, this is not allowed.

Here is a slightly larger example:

dc-end
HN-start
start-kj
dc-start
dc-HN
LN-dc
HN-end
kj-sa
kj-HN
kj-dc

The 19 paths through it are as follows:

start,HN,dc,HN,end
start,HN,dc,HN,kj,HN,end
start,HN,dc,end
start,HN,dc,kj,HN,end
start,HN,end
start,HN,kj,HN,dc,HN,end
start,HN,kj,HN,dc,end
start,HN,kj,HN,end
start,HN,kj,dc,HN,end
start,HN,kj,dc,end
start,dc,HN,end
start,dc,HN,kj,HN,end
start,dc,end
start,dc,kj,HN,end
start,kj,HN,dc,HN,end
start,kj,HN,dc,end
start,kj,HN,end
start,kj,dc,HN,end
start,kj,dc,end

Finally, this even larger example has 226 paths through it:

fs-end
he-DX
fs-he
start-DX
pj-DX
end-zg
zg-sl
zg-pj
pj-he
RW-he
fs-DX
pj-RW
zg-RW
start-pj
he-WI
zg-he
pj-fs
start-RW

How many paths through this cave system are there that visit small caves at most once?

defmodule Day12 do
  defmodule Graph do
    def new() do
      %{}
    end

    def new(edges) do
      Enum.reduce(edges, new(), fn {a, b}, graph ->
        add_edge(graph, a, b)
      end)
    end

    def add_edge(graph, a, b) do
      graph
      |> Map.update(a, MapSet.new([b]), &MapSet.put(&1, b))
      |> Map.update(b, MapSet.new([a]), &MapSet.put(&1, a))
    end

    def adjacent?(graph, a, b) do
      MapSet.member?(Map.get(graph, a), b)
    end

    def neighbors(graph, node) do
      Map.get(graph, node)
    end
  end

  def read_input() do
    AdventOfCode.read_input_lines(12)
    |> Enum.map(fn line ->
      [start_node, end_node] = String.split(line, "-")
      {start_node, end_node}
    end)
    |> Graph.new()
  end

  def count_paths(graph, a, b) do
    paths = search_paths(graph, a, b, [], [], MapSet.new())
    Enum.count(paths)
  end

  defp search_paths(graph, a, b, paths, current_path, visited) do
    if MapSet.member?(visited, a) do
      paths
    else
      visited = if can_revisit?(a), do: visited, else: MapSet.put(visited, a)
      current_path = [a | current_path]

      if a == b do
        [current_path | paths]
      else
        Enum.reduce(Graph.neighbors(graph, a), paths, fn next, paths ->
          search_paths(graph, next, b, paths, current_path, visited)
        end)
      end
    end
  end

  defp can_revisit?(name) do
    String.upcase(name) == name
  end

  def count_paths2(graph, a, b) do
    paths = search_paths2(graph, a, b, [], [], MapSet.new(), nil)
    Enum.count(paths)
  end

  defp search_paths2(graph, a, b, paths, current_path, visited, double_visited) do
    if MapSet.member?(visited, a) and (not can_revisit_once?(a) or double_visited != nil) do
      paths
    else
      {visited, double_visited} =
        cond do
          can_revisit?(a) ->
            {visited, double_visited}

          not MapSet.member?(visited, a) ->
            {MapSet.put(visited, a), double_visited}

          true ->
            {visited, a}
        end

      current_path = [a | current_path]

      if a == b do
        [current_path | paths]
      else
        Enum.reduce(Graph.neighbors(graph, a), paths, fn next, paths ->
          search_paths2(graph, next, b, paths, current_path, visited, double_visited)
        end)
      end
    end
  end

  defp can_revisit_once?(name) do
    String.downcase(name) == name && name != "start" && name != "end"
  end
end
{:module, Day12, <<70, 79, 82, 49, 0, 0, 18, ...>>, {:can_revisit_once?, 1}}
Day12.read_input()
|> Day12.count_paths("start", "end")
4773

Part Two

After reviewing the available paths, you realize you might have time to visit a single small cave twice. Specifically, big caves can be visited any number of times, a single small cave can be visited at most twice, and the remaining small caves can be visited at most once. However, the caves named start and end can only be visited exactly once each: once you leave the start cave, you may not return to it, and once you reach the end cave, the path must end immediately.

Now, the 36 possible paths through the first example above are:

start,A,b,A,b,A,c,A,end
start,A,b,A,b,A,end
start,A,b,A,b,end
start,A,b,A,c,A,b,A,end
start,A,b,A,c,A,b,end
start,A,b,A,c,A,c,A,end
start,A,b,A,c,A,end
start,A,b,A,end
start,A,b,d,b,A,c,A,end
start,A,b,d,b,A,end
start,A,b,d,b,end
start,A,b,end
start,A,c,A,b,A,b,A,end
start,A,c,A,b,A,b,end
start,A,c,A,b,A,c,A,end
start,A,c,A,b,A,end
start,A,c,A,b,d,b,A,end
start,A,c,A,b,d,b,end
start,A,c,A,b,end
start,A,c,A,c,A,b,A,end
start,A,c,A,c,A,b,end
start,A,c,A,c,A,end
start,A,c,A,end
start,A,end
start,b,A,b,A,c,A,end
start,b,A,b,A,end
start,b,A,b,end
start,b,A,c,A,b,A,end
start,b,A,c,A,b,end
start,b,A,c,A,c,A,end
start,b,A,c,A,end
start,b,A,end
start,b,d,b,A,c,A,end
start,b,d,b,A,end
start,b,d,b,end
start,b,end

The slightly larger example above now has 103 paths through it, and the even larger example now has 3509 paths through it.

Given these new rules, how many paths through this cave system are there?

Day12.read_input()
|> Day12.count_paths2("start", "end")
116985

Day 13: Transparent Origami

You reach another volcanically active part of the cave. It would be nice if you could do some kind of thermal imaging so you could tell ahead of time which caves are too hot to safely enter.

Fortunately, the submarine seems to be equipped with a thermal camera! When you activate it, you are greeted with:

Congratulations on your purchase! To activate this infrared thermal imaging camera system, please enter the code found on page 1 of the manual.

Apparently, the Elves have never used this feature. To your surprise, you manage to find the manual; as you go to open it, page 1 falls out. It's a large sheet of transparent paper! The transparent paper is marked with random dots and includes instructions on how to fold it up (your puzzle input). For example:

6,10
0,14
9,10
0,3
10,4
4,11
6,0
6,12
4,1
0,13
10,12
3,4
3,0
8,4
1,10
2,14
8,10
9,0

fold along y=7
fold along x=5

The first section is a list of dots on the transparent paper. 0,0 represents the top-left coordinate. The first value, x, increases to the right. The second value, y, increases downward. So, the coordinate 3,0 is to the right of 0,0, and the coordinate 0,7 is below 0,0. The coordinates in this example form the following pattern, where # is a dot on the paper and . is an empty, unmarked position:

...#..#..#.
....#......
...........
#..........
...#....#.#
...........
...........
...........
...........
...........
.#....#.##.
....#......
......#...#
#..........
#.#........

Then, there is a list of fold instructions. Each instruction indicates a line on the transparent paper and wants you to fold the paper up (for horizontal y=... lines) or left (for vertical x=... lines). In this example, the first fold instruction is fold along y=7, which designates the line formed by all of the positions where y is 7 (marked here with -):

...#..#..#.
....#......
...........
#..........
...#....#.#
...........
...........
-----------
...........
...........
.#....#.##.
....#......
......#...#
#..........
#.#........

Because this is a horizontal line, fold the bottom half up. Some of the dots might end up overlapping after the fold is complete, but dots will never appear exactly on a fold line. The result of doing this fold looks like this:

#.##..#..#.
#...#......
......#...#
#...#......
.#.#..#.###
...........
...........

Now, only 17 dots are visible.

Notice, for example, the two dots in the bottom left corner before the transparent paper is folded; after the fold is complete, those dots appear in the top left corner (at 0,0 and 0,1). Because the paper is transparent, the dot just below them in the result (at 0,3) remains visible, as it can be seen through the transparent paper.

Also notice that some dots can end up overlapping; in this case, the dots merge together and become a single dot.

The second fold instruction is fold along x=5, which indicates this line:

#.##.|#..#.
#...#|.....
.....|#...#
#...#|.....
.#.#.|#.###
.....|.....
.....|.....

Because this is a vertical line, fold left:

#####
#...#
#...#
#...#
#####
.....
.....

The instructions made a square!

The transparent paper is pretty big, so for now, focus on just completing the first fold. After the first fold in the example above, 17 dots are visible - dots that end up overlapping after the fold is completed count as a single dot.

How many dots are visible after completing just the first fold instruction on your transparent paper?

defmodule Day13 do
  def read_input() do
    {points, ["" | folds]} =
      AdventOfCode.read_input_lines(13)
      |> Enum.split_while(&(&1 != ""))

    points =
      for line <- points, into: MapSet.new() do
        [x, y] = String.split(line, ",") |> Enum.map(&String.to_integer/1)
        {x, y}
      end

    folds =
      for "fold along " <> line <- folds do
        case line do
          "x=" <> x ->
            {:vertical, String.to_integer(x)}

          "y=" <> y ->
            {:horizontal, String.to_integer(y)}
        end
      end

    {points, folds}
  end

  def fold_many(points, folds) do
    Enum.reduce(folds, points, fn fold, points ->
      fold(points, fold)
    end)
  end

  def fold(points, fold) do
    points
    |> Enum.map(&fold_point(&1, fold))
    |> MapSet.new()
  end

  def fold_point({x, y}, {:vertical, fold_x}) do
    if x < fold_x do
      {x, y}
    else
      {2 * fold_x - x, y}
    end
  end

  def fold_point({x, y}, {:horizontal, fold_y}) do
    if y < fold_y do
      {x, y}
    else
      {x, 2 * fold_y - y}
    end
  end

  def print(points) do
    max_x = points |> Enum.map(fn {x, _} -> x end) |> Enum.max()
    max_y = points |> Enum.map(fn {_, y} -> y end) |> Enum.max()

    for y <- 0..max_y, x <- 0..max_x do
      str = if MapSet.member?(points, {x, y}), do: "#", else: " "
      IO.write(str)

      if x == max_x do
        IO.write("\n")
      end
    end

    :ok
  end
end
{:module, Day13, <<70, 79, 82, 49, 0, 0, 21, ...>>, {:print, 1}}
{points, folds} = Day13.read_input()

points
|> Day13.fold(hd(folds))
|> Enum.count()
770

Part Two

Finish folding the transparent paper according to the instructions. The manual says the code is always eight capital letters.

What code do you use to activate the infrared thermal imaging camera system?

points
|> Day13.fold_many(folds)
|> Day13.print()
#### ###  #  # #### #    ###  ###  ### 
#    #  # #  # #    #    #  # #  # #  #
###  #  # #  # ###  #    #  # ###  #  #
#    ###  #  # #    #    ###  #  # ### 
#    #    #  # #    #    #    #  # # # 
#### #     ##  #### #### #    ###  #  #
:ok

Day 14: Extended Polymerization

The incredible pressures at this depth are starting to put a strain on your submarine. The submarine has polymerization equipment that would produce suitable materials to reinforce the submarine, and the nearby volcanically-active caves should even have the necessary input elements in sufficient quantities.

The submarine manual contains instructions for finding the optimal polymer formula; specifically, it offers a polymer template and a list of pair insertion rules (your puzzle input). You just need to work out what polymer would result after repeating the pair insertion process a few times.

For example:

NNCB

CH -> B
HH -> N
CB -> H
NH -> C
HB -> C
HC -> B
HN -> C
NN -> C
BH -> H
NC -> B
NB -> B
BN -> B
BB -> N
BC -> B
CC -> N
CN -> C

The first line is the polymer template - this is the starting point of the process.

The following section defines the pair insertion rules. A rule like AB -> C means that when elements A and B are immediately adjacent, element C should be inserted between them. These insertions all happen simultaneously.

So, starting with the polymer template NNCB, the first step simultaneously considers all three pairs:

  • The first pair (NN) matches the rule NN -> C, so element C is inserted between the first N and the second N.
  • The second pair (NC) matches the rule NC -> B, so element B is inserted between the N and the C.
  • The third pair (CB) matches the rule CB -> H, so element H is inserted between the C and the B.

Note that these pairs overlap: the second element of one pair is the first element of the next pair. Also, because all pairs are considered simultaneously, inserted elements are not considered to be part of a pair until the next step.

After the first step of this process, the polymer becomes NCNBCHB.

Here are the results of a few steps using the above rules:

Template:     NNCB
After step 1: NCNBCHB
After step 2: NBCCNBBBCBHCB
After step 3: NBBBCNCCNBBNBNBBCHBHHBCHB
After step 4: NBBNBNBBCCNBCNCCNBBNBBNBBBNBBNBBCBHCBHHNHCBBCBHCB

This polymer grows quickly. After step 5, it has length 97; After step 10, it has length 3073. After step 10, B occurs 1749 times, C occurs 298 times, H occurs 161 times, and N occurs 865 times; taking the quantity of the most common element (B, 1749) and subtracting the quantity of the least common element (H, 161) produces 1749 - 161 = 1588.

Apply 10 steps of pair insertion to the polymer template and find the most and least common elements in the result. What do you get if you take the quantity of the most common element and subtract the quantity of the least common element?

defmodule Day14 do
  def read_input() do
    [polymer, "" | rules] = AdventOfCode.read_input_lines(14)

    rules =
      rules
      |> Enum.map(fn line ->
        [pair, inserted] = String.split(line, " -> ")
        {pair, inserted}
      end)
      |> Map.new()

    {polymer, rules}
  end
end
{:module, Day14, <<70, 79, 82, 49, 0, 0, 7, ...>>, {:read_input, 0}}
{polymer, rules} = Day14.read_input()
polymer
"FPNFCVSNNFSFHHOCNBOB"
defmodule Day14.Part1 do
  def expand_polymer(polymer, rules, steps \\ 1) do
    letters = String.codepoints(polymer)

    Enum.reduce(1..steps, letters, fn _, letters ->
      insert_between_pairs(letters, &Map.fetch!(rules, &1 <> &2))
    end)
    |> Enum.join()
  end

  def insert_between_pairs(letters, f) do
    to_insert = Enum.zip_with(letters, tl(letters), f) ++ [[]]

    letters
    |> Enum.zip_with(to_insert, &[&1, &2])
    |> List.flatten()
  end

  def common_element_difference(polymer) do
    {min, max} =
      polymer
      |> String.codepoints()
      |> Enum.frequencies()
      |> Map.values()
      |> Enum.min_max()

    max - min
  end
end
{:module, Day14.Part1, <<70, 79, 82, 49, 0, 0, 13, ...>>, {:common_element_difference, 1}}
polymer
|> Day14.Part1.expand_polymer(rules, 10)
|> Day14.Part1.common_element_difference()
2745

Part Two

The resulting polymer isn't nearly strong enough to reinforce the submarine. You'll need to run more steps of the pair insertion process; a total of 40 steps should do it.

In the above example, the most common element is B (occurring 2192039569602 times) and the least common element is H (occurring 3849876073 times); subtracting these produces 2188189693529.

Apply 40 steps of pair insertion to the polymer template and find the most and least common elements in the result. What do you get if you take the quantity of the most common element and subtract the quantity of the least common element?

defmodule Day14.Part2 do
  def count_pairs(polymer) do
    letters = String.codepoints(polymer)

    letters
    |> Enum.zip_with(tl(letters), &(&1 <> &2))
    |> Enum.frequencies()
  end

  def expand_polymer(polymer, rules, steps \\ 1) do
    Enum.reduce(1..steps, polymer, fn _, polymer -> expand(polymer, rules) end)
  end

  defp expand(polymer, rules) do
    Enum.reduce(polymer, polymer, fn {pair, count}, polymer ->
      [front, back] = String.codepoints(pair)
      new_letter = Map.fetch!(rules, pair)

      polymer
      |> Map.update!(pair, &(&1 - count))
      |> Map.update(front <> new_letter, count, &(&1 + count))
      |> Map.update(new_letter <> back, count, &(&1 + count))
    end)
  end

  def common_element_difference(polymer, first, last) do
    freqs =
      polymer
      |> Enum.reduce(%{}, fn {pair, count}, counts ->
        [front, back] = String.codepoints(pair)

        counts
        |> Map.update(front, count, &(&1 + count))
        |> Map.update(back, count, &(&1 + count))
      end)
      |> Map.map(fn
        {letter, count} when letter == first or letter == last ->
          div(count + 1, 2)

        {_, count} ->
          div(count, 2)
      end)

    {min, max} =
      freqs
      |> Map.values()
      |> Enum.min_max()

    max - min
  end
end
{:module, Day14.Part2, <<70, 79, 82, 49, 0, 0, 19, ...>>, {:common_element_difference, 3}}
polymer
|> Day14.Part2.count_pairs()
|> Day14.Part2.expand_polymer(rules, 40)
|> Day14.Part2.common_element_difference("F", "B")
3420801168962

Day 15: Chiton

You've almost reached the exit of the cave, but the walls are getting closer together. Your submarine can barely still fit, though; the main problem is that the walls of the cave are covered in chitons, and it would be best not to bump any of them.

The cavern is large, but has a very low ceiling, restricting your motion to two dimensions. The shape of the cavern resembles a square; a quick scan of chiton density produces a map of risk level throughout the cave (your puzzle input). For example:

1163751742
1381373672
2136511328
3694931569
7463417111
1319128137
1359912421
3125421639
1293138521
2311944581

You start in the top left position, your destination is the bottom right position, and you cannot move diagonally. The number at each position is its risk level; to determine the total risk of an entire path, add up the risk levels of each position you enter (that is, don't count the risk level of your starting position unless you enter it; leaving it adds no risk to your total).

Your goal is to find a path with the lowest total risk. In this example, a path with the lowest total risk is highlighted here:

1163751742
1381373672
2136511328
3694931569
7463417111
1319128137
1359912421
3125421639
1293138521
2311944581

The total risk of this path is 40 (the starting position is never entered, so its risk is not counted).

What is the lowest total risk of any path from the top left to the bottom right?

defmodule Day15 do
  def read_input() do
    AdventOfCode.read_input_lines(15)
    |> Enum.with_index()
    |> Enum.flat_map(fn {line, y} ->
      line
      |> String.codepoints()
      |> Enum.map(&String.to_integer/1)
      |> Enum.with_index(fn value, x -> {{x, y}, value} end)
    end)
    |> Map.new()
  end
end
{:module, Day15, <<70, 79, 82, 49, 0, 0, 8, ...>>, {:read_input, 0}}
nodes = Day15.read_input()
%{
  {76, 13} => 8,
  {37, 47} => 9,
  {65, 63} => 7,
  {38, 2} => 9,
  {1, 26} => 7,
  {83, 76} => 3,
  {32, 15} => 2,
  {89, 14} => 8,
  {35, 30} => 2,
  {37, 53} => 2,
  {4, 5} => 8,
  {8, 50} => 8,
  {78, 98} => 4,
  {95, 56} => 9,
  {74, 12} => 3,
  {11, 39} => 9,
  {65, 43} => 3,
  {22, 38} => 6,
  {14, 86} => 5,
  {20, 41} => 2,
  {29, 25} => 7,
  {86, 10} => 2,
  {83, 36} => 7,
  {29, 26} => 2,
  {47, 27} => 5,
  {4, 81} => 9,
  {31, 42} => 2,
  {9, 34} => 2,
  {90, 0} => 9,
  {67, 98} => 9,
  {13, 85} => 7,
  {63, 81} => 7,
  {82, 60} => 3,
  {47, 38} => 7,
  {15, 92} => 8,
  {58, 58} => 4,
  {20, 3} => 8,
  {61, 95} => 5,
  {23, 67} => 7,
  {78, 75} => 5,
  {79, 17} => 8,
  {75, 0} => 9,
  {16, 73} => 6,
  {76, 2} => 7,
  {58, 84} => 6,
  {58, 33} => 9,
  {47, 44} => 4,
  {54, 31} => 6,
  {13, ...} => 8,
  {...} => 2,
  ...
}
defmodule Day15.Part1 do
  def neighbors(nodes, {x, y}) do
    [{x - 1, y}, {x + 1, y}, {x, y - 1}, {x, y + 1}]
    |> Enum.flat_map(fn point ->
      case Map.get(nodes, point) do
        nil -> []
        value -> [{point, value}]
      end
    end)
  end

  def lowest_risk_path(nodes, src, dest) do
    distances = for n <- Map.keys(nodes), into: %{}, do: {n, if(n == src, do: 0, else: :infinity)}
    lowest_risk_path(nodes, dest, Heap.push(Heap.min(), {0, src}), distances)
  end

  def lowest_risk_path(nodes, dest, remaining, distances) do
    {{_, u}, remaining} = Heap.split(remaining)

    if u == dest do
      Map.fetch!(distances, dest)
    else
      {distances, remaining} =
        neighbors(nodes, u)
        |> Enum.reduce({distances, remaining}, fn {v, cost}, {distances, remaining} ->
          alt = add(Map.fetch!(distances, u), cost)

          if alt < Map.fetch!(distances, v) do
            {Map.put(distances, v, alt), Heap.push(remaining, {alt, v})}
          else
            {distances, remaining}
          end
        end)

      lowest_risk_path(nodes, dest, remaining, distances)
    end
  end

  def bottom_right_corner(nodes) do
    nodes
    |> Map.keys()
    |> Enum.max_by(fn {x, y} -> x + y end)
  end

  defp add(_x, :infinity), do: :infinity
  defp add(:infinity, _y), do: :infinity
  defp add(x, y), do: x + y
end
{:module, Day15.Part1, <<70, 79, 82, 49, 0, 0, 16, ...>>, {:add, 2}}
corner = Day15.Part1.bottom_right_corner(nodes)
{99, 99}
Day15.Part1.lowest_risk_path(nodes, {0, 0}, corner)
589

Part Two

Now that you know how to find low-risk paths in the cave, you can try to find your way out.

The entire cave is actually five times larger in both dimensions than you thought; the area you originally scanned is just one tile in a 5x5 tile area that forms the full map. Your original map tile repeats to the right and downward; each time the tile repeats to the right or downward, all of its risk levels are 1 higher than the tile immediately up or left of it. However, risk levels above 9 wrap back around to 1. So, if your original map had some position with a risk level of 8, then that same position on each of the 25 total tiles would be as follows:

8 9 1 2 3
9 1 2 3 4
1 2 3 4 5
2 3 4 5 6
3 4 5 6 7

Each single digit above corresponds to the example position with a value of 8 on the top-left tile. Because the full map is actually five times larger in both dimensions, that position appears a total of 25 times, once in each duplicated tile, with the values shown above.

Here is the full five-times-as-large version of the first example above, with the original map in the top left corner highlighted:

11637517422274862853338597396444961841755517295286
13813736722492484783351359589446246169155735727126
21365113283247622439435873354154698446526571955763
36949315694715142671582625378269373648937148475914
74634171118574528222968563933317967414442817852555
13191281372421239248353234135946434524615754563572
13599124212461123532357223464346833457545794456865
31254216394236532741534764385264587549637569865174
12931385212314249632342535174345364628545647573965
23119445813422155692453326671356443778246755488935
22748628533385973964449618417555172952866628316397
24924847833513595894462461691557357271266846838237
32476224394358733541546984465265719557637682166874
47151426715826253782693736489371484759148259586125
85745282229685639333179674144428178525553928963666
24212392483532341359464345246157545635726865674683
24611235323572234643468334575457944568656815567976
42365327415347643852645875496375698651748671976285
23142496323425351743453646285456475739656758684176
34221556924533266713564437782467554889357866599146
33859739644496184175551729528666283163977739427418
35135958944624616915573572712668468382377957949348
43587335415469844652657195576376821668748793277985
58262537826937364893714847591482595861259361697236
96856393331796741444281785255539289636664139174777
35323413594643452461575456357268656746837976785794
35722346434683345754579445686568155679767926678187
53476438526458754963756986517486719762859782187396
34253517434536462854564757396567586841767869795287
45332667135644377824675548893578665991468977611257
44961841755517295286662831639777394274188841538529
46246169155735727126684683823779579493488168151459
54698446526571955763768216687487932779859814388196
69373648937148475914825958612593616972361472718347
17967414442817852555392896366641391747775241285888
46434524615754563572686567468379767857948187896815
46833457545794456865681556797679266781878137789298
64587549637569865174867197628597821873961893298417
45364628545647573965675868417678697952878971816398
56443778246755488935786659914689776112579188722368
55172952866628316397773942741888415385299952649631
57357271266846838237795794934881681514599279262561
65719557637682166874879327798598143881961925499217
71484759148259586125936169723614727183472583829458
28178525553928963666413917477752412858886352396999
57545635726865674683797678579481878968159298917926
57944568656815567976792667818781377892989248891319
75698651748671976285978218739618932984172914319528
56475739656758684176786979528789718163989182927419
67554889357866599146897761125791887223681299833479

Equipped with the full map, you can now find a path from the top left corner to the bottom right corner with the lowest total risk:

11637517422274862853338597396444961841755517295286
13813736722492484783351359589446246169155735727126
21365113283247622439435873354154698446526571955763
36949315694715142671582625378269373648937148475914
74634171118574528222968563933317967414442817852555
13191281372421239248353234135946434524615754563572
13599124212461123532357223464346833457545794456865
31254216394236532741534764385264587549637569865174
12931385212314249632342535174345364628545647573965
23119445813422155692453326671356443778246755488935
22748628533385973964449618417555172952866628316397
24924847833513595894462461691557357271266846838237
32476224394358733541546984465265719557637682166874
47151426715826253782693736489371484759148259586125
85745282229685639333179674144428178525553928963666
24212392483532341359464345246157545635726865674683
24611235323572234643468334575457944568656815567976
42365327415347643852645875496375698651748671976285
23142496323425351743453646285456475739656758684176
34221556924533266713564437782467554889357866599146
33859739644496184175551729528666283163977739427418
35135958944624616915573572712668468382377957949348
43587335415469844652657195576376821668748793277985
58262537826937364893714847591482595861259361697236
96856393331796741444281785255539289636664139174777
35323413594643452461575456357268656746837976785794
35722346434683345754579445686568155679767926678187
53476438526458754963756986517486719762859782187396
34253517434536462854564757396567586841767869795287
45332667135644377824675548893578665991468977611257
44961841755517295286662831639777394274188841538529
46246169155735727126684683823779579493488168151459
54698446526571955763768216687487932779859814388196
69373648937148475914825958612593616972361472718347
17967414442817852555392896366641391747775241285888
46434524615754563572686567468379767857948187896815
46833457545794456865681556797679266781878137789298
64587549637569865174867197628597821873961893298417
45364628545647573965675868417678697952878971816398
56443778246755488935786659914689776112579188722368
55172952866628316397773942741888415385299952649631
57357271266846838237795794934881681514599279262561
65719557637682166874879327798598143881961925499217
71484759148259586125936169723614727183472583829458
28178525553928963666413917477752412858886352396999
57545635726865674683797678579481878968159298917926
57944568656815567976792667818781377892989248891319
75698651748671976285978218739618932984172914319528
56475739656758684176786979528789718163989182927419
67554889357866599146897761125791887223681299833479

The total risk of this path is 315 (the starting position is still never entered, so its risk is not counted).

Using the full map, what is the lowest total risk of any path from the top left to the bottom right?

defmodule Day15.Part2 do
  def expand_graph(nodes) do
    {corner_x, corner_y} = Day15.Part1.bottom_right_corner(nodes)
    w = corner_x + 1
    h = corner_y + 1

    points =
      for x <- 0..(w * 5 - 1), y <- 0..(h * 5 - 1) do
        src = {rem(x, w), rem(y, h)}
        offset = div(x, w) + div(y, h)
        cost = rem(Map.fetch!(nodes, src) + offset - 1, 9) + 1
        {{x, y}, cost}
      end

    Map.new(points)
  end
end
{:module, Day15.Part2, <<70, 79, 82, 49, 0, 0, 9, ...>>, {:expand_graph, 1}}
expanded = Day15.Part2.expand_graph(nodes)
corner2 = Day15.Part1.bottom_right_corner(expanded)
{499, 499}
Day15.Part1.lowest_risk_path(expanded, {0, 0}, corner2)
2885

Day 16: Packet Decoder

As you leave the cave and reach open waters, you receive a transmission from the Elves back on the ship.

The transmission was sent using the Buoyancy Interchange Transmission System (BITS), a method of packing numeric expressions into a binary sequence. Your submarine's computer has saved the transmission in hexadecimal (your puzzle input).

The first step of decoding the message is to convert the hexadecimal representation into binary. Each character of hexadecimal corresponds to four bits of binary data:

0 = 0000
1 = 0001
2 = 0010
3 = 0011
4 = 0100
5 = 0101
6 = 0110
7 = 0111
8 = 1000
9 = 1001
A = 1010
B = 1011
C = 1100
D = 1101
E = 1110
F = 1111

The BITS transmission contains a single packet at its outermost layer which itself contains many other packets. The hexadecimal representation of this packet might encode a few extra 0 bits at the end; these are not part of the transmission and should be ignored.

Every packet begins with a standard header: the first three bits encode the packet version, and the next three bits encode the packet type ID. These two values are numbers; all numbers encoded in any packet are represented as binary with the most significant bit first. For example, a version encoded as the binary sequence 100 represents the number 4.

Packets with type ID 4 represent a literal value. Literal value packets encode a single binary number. To do this, the binary number is padded with leading zeroes until its length is a multiple of four bits, and then it is broken into groups of four bits. Each group is prefixed by a 1 bit except the last group, which is prefixed by a 0 bit. These groups of five bits immediately follow the packet header. For example, the hexadecimal string D2FE28 becomes:

110100101111111000101000
VVVTTTAAAAABBBBBCCCCC

Below each bit is a label indicating its purpose:

  • The three bits labeled V (110) are the packet version, 6.
  • The three bits labeled T (100) are the packet type ID, 4, which means the packet is a literal value.
  • The five bits labeled A (10111) start with a 1 (not the last group, keep reading) and contain the first four bits of the number, 0111.
  • The five bits labeled B (11110) start with a 1 (not the last group, keep reading) and contain four more bits of the number, 1110.
  • The five bits labeled C (00101) start with a 0 (last group, end of packet) and contain the last four bits of the number, 0101.
  • The three unlabeled 0 bits at the end are extra due to the hexadecimal representation and should be ignored.

So, this packet represents a literal value with binary representation 011111100101, which is 2021 in decimal.

Every other type of packet (any packet with a type ID other than 4) represent an operator that performs some calculation on one or more sub-packets contained within. Right now, the specific operations aren't important; focus on parsing the hierarchy of sub-packets.

An operator packet contains one or more packets. To indicate which subsequent binary data represents its sub-packets, an operator packet can use one of two modes indicated by the bit immediately after the packet header; this is called the length type ID:

  • If the length type ID is 0, then the next 15 bits are a number that represents the total length in bits of the sub-packets contained by this packet.
  • If the length type ID is 1, then the next 11 bits are a number that represents the number of sub-packets immediately contained by this packet.

Finally, after the length type ID bit and the 15-bit or 11-bit field, the sub-packets appear.

For example, here is an operator packet (hexadecimal string 38006F45291200) with length type ID 0 that contains two sub-packets:

00111000000000000110111101000101001010010001001000000000
VVVTTTILLLLLLLLLLLLLLLAAAAAAAAAAABBBBBBBBBBBBBBBB
  • The three bits labeled V (001) are the packet version, 1.
  • The three bits labeled T (110) are the packet type ID, 6, which means the packet is an operator.
  • The bit labeled I (0) is the length type ID, which indicates that the length is a 15-bit number representing the number of bits in the sub-packets.
  • The 15 bits labeled L (000000000011011) contain the length of the sub-packets in bits, 27.
  • The 11 bits labeled A contain the first sub-packet, a literal value representing the number 10.
  • The 16 bits labeled B contain the second sub-packet, a literal value representing the number 20.

After reading 11 and 16 bits of sub-packet data, the total length indicated in L (27) is reached, and so parsing of this packet stops.

As another example, here is an operator packet (hexadecimal string EE00D40C823060) with length type ID 1 that contains three sub-packets:

11101110000000001101010000001100100000100011000001100000
VVVTTTILLLLLLLLLLLAAAAAAAAAAABBBBBBBBBBBCCCCCCCCCCC
  • The three bits labeled V (111) are the packet version, 7.
  • The three bits labeled T (011) are the packet type ID, 3, which means the packet is an operator.
  • The bit labeled I (1) is the length type ID, which indicates that the length is a 11-bit number representing the number of sub-packets.
  • The 11 bits labeled L (00000000011) contain the number of sub-packets, 3.
  • The 11 bits labeled A contain the first sub-packet, a literal value representing the number 1.
  • The 11 bits labeled B contain the second sub-packet, a literal value representing the number 2.
  • The 11 bits labeled C contain the third sub-packet, a literal value representing the number 3.

After reading 3 complete sub-packets, the number of sub-packets indicated in L (3) is reached, and so parsing of this packet stops.

For now, parse the hierarchy of the packets throughout the transmission and add up all of the version numbers.

Here are a few more examples of hexadecimal-encoded transmissions:

  • 8A004A801A8002F478 represents an operator packet (version 4) which contains an operator packet (version 1) which contains an operator packet (version 5) which contains a literal value (version 6); this packet has a version sum of 16.
  • 620080001611562C8802118E34 represents an operator packet (version 3) which contains two sub-packets; each sub-packet is an operator packet that contains two literal values. This packet has a version sum of 12.
  • C0015000016115A2E0802F182340 has the same structure as the previous example, but the outermost packet uses a different length type ID. This packet has a version sum of 23.
  • A0016C880162017C3686B18A3D4780 is an operator packet that contains an operator packet that contains an operator packet that contains five literal values; it has a version sum of 31.

Decode the structure of your hexadecimal-encoded BITS transmission; what do you get if you add up the version numbers in all packets?

defmodule Day16 do
  def read_input() do
    AdventOfCode.read_input(16) |> Base.decode16!()
  end
end
{:module, Day16, <<70, 79, 82, 49, 0, 0, 5, ...>>, {:read_input, 0}}
input = Day16.read_input()
<<226, 13, 65, 128, 43, 41, 132, 189, 0, 84, 0, 16, 248, 45, 9, 227, 88, 128, 53, 13, 97, 164, 29,
  48, 4, 229, 97, 30, 88, 95, 64, 21, 158, 215, 173, 124, 144, 207, 107, 214, 190, 73, 200, 2, 222,
  176, 5, 37, 39, 44, ...>>
defmodule Day16.Part1 do
  defmodule Packet do
    defstruct [:version, :type_id, :payload]
  end

  def sum_versions(%Packet{version: version, type_id: 4}) do
    version
  end

  def sum_versions(%Packet{version: version, payload: subpackets}) do
    version + Enum.sum(for p <- subpackets, do: sum_versions(p))
  end

  def decode_packet(<<version::size(3), 4::size(3), rest::bits>>) do
    {value, rest} = decode_literal_value(rest)
    size = bit_size(value)
    <<value::integer-size(size)>> = value
    {%Packet{version: version, type_id: 4, payload: value}, rest}
  end

  def decode_packet(<<version::size(3), type_id::size(3), 0::size(1), len::size(15), rest::bits>>) do
    <<subpackets::bits-size(len), rest::bits>> = rest
    subpackets = decode_all_subpackets(subpackets)
    {%Packet{version: version, type_id: type_id, payload: subpackets}, rest}
  end

  def decode_packet(<<version::size(3), type_id::size(3), 1::size(1), n::size(11), rest::bits>>) do
    {packets, rest} =
      Enum.reduce(1..n, {[], rest}, fn _, {packets, rest} ->
        {packet, rest} = decode_packet(rest)
        {[packet | packets], rest}
      end)

    {%Packet{version: version, type_id: type_id, payload: Enum.reverse(packets)}, rest}
  end

  defp decode_literal_value(<<1::size(1), value::bits-size(4), rest::bits>>) do
    {remaining_value, rest} = decode_literal_value(rest)
    {<<value::bits, remaining_value::bits>>, rest}
  end

  defp decode_literal_value(<<0::size(1), value::bits-size(4), rest::bits>>) do
    {value, rest}
  end

  defp decode_all_subpackets(<<>>) do
    []
  end

  defp decode_all_subpackets(subpackets) do
    {packet, rest} = decode_packet(subpackets)
    [packet | decode_all_subpackets(rest)]
  end
end
{:module, Day16.Part1, <<70, 79, 82, 49, 0, 0, 14, ...>>, {:decode_all_subpackets, 1}}
"D2FE28"
|> Base.decode16!()
|> Day16.Part1.decode_packet()
{%Day16.Part1.Packet{payload: 2021, type_id: 4, version: 6}, <<0::size(3)>>}
"38006F45291200"
|> Base.decode16!()
|> Day16.Part1.decode_packet()
{%Day16.Part1.Packet{
   payload: [
     %Day16.Part1.Packet{payload: 10, type_id: 4, version: 6},
     %Day16.Part1.Packet{payload: 20, type_id: 4, version: 2}
   ],
   type_id: 6,
   version: 1
 }, <<0::size(7)>>}
"EE00D40C823060"
|> Base.decode16!()
|> Day16.Part1.decode_packet()
{%Day16.Part1.Packet{
   payload: [
     %Day16.Part1.Packet{payload: 1, type_id: 4, version: 2},
     %Day16.Part1.Packet{payload: 2, type_id: 4, version: 4},
     %Day16.Part1.Packet{payload: 3, type_id: 4, version: 1}
   ],
   type_id: 3,
   version: 7
 }, <<0::size(5)>>}
input
|> Day16.Part1.decode_packet()
|> elem(0)
|> Day16.Part1.sum_versions()
984

Part Two

Now that you have the structure of your transmission decoded, you can calculate the value of the expression it represents.

Literal values (type ID 4) represent a single number as described above. The remaining type IDs are more interesting:

  • Packets with type ID 0 are sum packets - their value is the sum of the values of their sub-packets. If they only have a single sub-packet, their value is the value of the sub-packet.
  • Packets with type ID 1 are product packets - their value is the result of multiplying together the values of their sub-packets. If they only have a single sub-packet, their value is the value of the sub-packet.
  • Packets with type ID 2 are minimum packets - their value is the minimum of the values of their sub-packets.
  • Packets with type ID 3 are maximum packets - their value is the maximum of the values of their sub-packets.
  • Packets with type ID 5 are greater than packets - their value is 1 if the value of the first sub-packet is greater than the value of the second sub-packet; otherwise, their value is 0. These packets always have exactly two sub-packets.
  • Packets with type ID 6 are less than packets - their value is 1 if the value of the first sub-packet is less than the value of the second sub-packet; otherwise, their value is 0. These packets always have exactly two sub-packets.
  • Packets with type ID 7 are equal to packets - their value is 1 if the value of the first sub-packet is equal to the value of the second sub-packet; otherwise, their value is 0. These packets always have exactly two sub-packets.

Using these rules, you can now work out the value of the outermost packet in your BITS transmission.

For example:

  • C200B40A82 finds the sum of 1 and 2, resulting in the value 3.
  • 04005AC33890 finds the product of 6 and 9, resulting in the value 54.
  • 880086C3E88112 finds the minimum of 7, 8, and 9, resulting in the value 7.
  • CE00C43D881120 finds the maximum of 7, 8, and 9, resulting in the value 9.
  • D8005AC2A8F0 produces 1, because 5 is less than 15.
  • F600BC2D8F produces 0, because 5 is not greater than 15.
  • 9C005AC2F8F0 produces 0, because 5 is not equal to 15.
  • 9C0141080250320F1802104A08 produces 1, because 1 + 3 = 2 * 2.

What do you get if you evaluate the expression represented by your hexadecimal-encoded BITS transmission?

defmodule Day16.Part2 do
  alias Day16.Part1.Packet

  def packet_value(%Packet{type_id: 0, payload: subpackets}) do
    subpackets
    |> Enum.map(&packet_value/1)
    |> Enum.sum()
  end

  def packet_value(%Packet{type_id: 1, payload: subpackets}) do
    subpackets
    |> Enum.map(&packet_value/1)
    |> Enum.product()
  end

  def packet_value(%Packet{type_id: 2, payload: subpackets}) do
    subpackets
    |> Enum.map(&packet_value/1)
    |> Enum.min()
  end

  def packet_value(%Packet{type_id: 3, payload: subpackets}) do
    subpackets
    |> Enum.map(&packet_value/1)
    |> Enum.max()
  end

  def packet_value(%Packet{type_id: 4, payload: value}), do: value

  def packet_value(%Packet{type_id: 5, payload: [first, second]}) do
    if packet_value(first) > packet_value(second), do: 1, else: 0
  end

  def packet_value(%Packet{type_id: 6, payload: [first, second]}) do
    if packet_value(first) < packet_value(second), do: 1, else: 0
  end

  def packet_value(%Packet{type_id: 7, payload: [first, second]}) do
    if packet_value(first) == packet_value(second), do: 1, else: 0
  end
end
{:module, Day16.Part2, <<70, 79, 82, 49, 0, 0, 9, ...>>, {:packet_value, 1}}
"C200B40A82"
|> Base.decode16!()
|> Day16.Part1.decode_packet()
|> elem(0)
|> Day16.Part2.packet_value()
3
input
|> Day16.Part1.decode_packet()
|> elem(0)
|> Day16.Part2.packet_value()
1015320896946

Day 17: Trick Shot

You finally decode the Elves' message. HI, the message says. You continue searching for the sleigh keys.

Ahead of you is what appears to be a large ocean trench. Could the keys have fallen into it? You'd better send a probe to investigate.

The probe launcher on your submarine can fire the probe with any integer velocity in the x (forward) and y (upward, or downward if negative) directions. For example, an initial x,y velocity like 0,10 would fire the probe straight up, while an initial velocity like 10,-1 would fire the probe forward at a slight downward angle.

The probe's x,y position starts at 0,0. Then, it will follow some trajectory by moving in steps. On each step, these changes occur in the following order:

  • The probe's x position increases by its x velocity.
  • The probe's y position increases by its y velocity.
  • Due to drag, the probe's x velocity changes by 1 toward the value 0; that is, it decreases by 1 if it is greater than 0, increases by 1 if it is less than 0, or does not change if it is already 0.
  • Due to gravity, the probe's y velocity decreases by 1.

For the probe to successfully make it into the trench, the probe must be on some trajectory that causes it to be within a target area after any step. The submarine computer has already calculated this target area (your puzzle input). For example:

target area: x=20..30, y=-10..-5

This target area means that you need to find initial x,y velocity values such that after any step, the probe's x position is at least 20 and at most 30, and the probe's y position is at least -10 and at most -5.

Given this target area, one initial velocity that causes the probe to be within the target area after any step is 7,2:

.............#....#............
.......#..............#........
...............................
S........................#.....
...............................
...............................
...........................#...
...............................
....................TTTTTTTTTTT
....................TTTTTTTTTTT
....................TTTTTTTT#TT
....................TTTTTTTTTTT
....................TTTTTTTTTTT
....................TTTTTTTTTTT

In this diagram, S is the probe's initial position, 0,0. The x coordinate increases to the right, and the y coordinate increases upward. In the bottom right, positions that are within the target area are shown as T. After each step (until the target area is reached), the position of the probe is marked with #. (The bottom-right # is both a position the probe reaches and a position in the target area.)

Another initial velocity that causes the probe to be within the target area after any step is 6,3:

...............#..#............
...........#........#..........
...............................
......#..............#.........
...............................
...............................
S....................#.........
...............................
...............................
...............................
.....................#.........
....................TTTTTTTTTTT
....................TTTTTTTTTTT
....................TTTTTTTTTTT
....................TTTTTTTTTTT
....................T#TTTTTTTTT
....................TTTTTTTTTTT

Another one is 9,0:

S........#.....................
.................#.............
...............................
........................#......
...............................
....................TTTTTTTTTTT
....................TTTTTTTTTT#
....................TTTTTTTTTTT
....................TTTTTTTTTTT
....................TTTTTTTTTTT
....................TTTTTTTTTTT

One initial velocity that doesn't cause the probe to be within the target area after any step is 17,-4:

S..............................................................
...............................................................
...............................................................
...............................................................
.................#.............................................
....................TTTTTTTTTTT................................
....................TTTTTTTTTTT................................
....................TTTTTTTTTTT................................
....................TTTTTTTTTTT................................
....................TTTTTTTTTTT..#.............................
....................TTTTTTTTTTT................................
...............................................................
...............................................................
...............................................................
...............................................................
................................................#..............
...............................................................
...............................................................
...............................................................
...............................................................
...............................................................
...............................................................
..............................................................#

The probe appears to pass through the target area, but is never within it after any step. Instead, it continues down and to the right - only the first few steps are shown.

If you're going to fire a highly scientific probe out of a super cool probe launcher, you might as well do it with style. How high can you make the probe go while still reaching the target area?

In the above example, using an initial velocity of 6,9 is the best you can do, causing the probe to reach a maximum y position of 45. (Any higher initial y velocity causes the probe to overshoot the target area entirely.)

Find the initial velocity that causes the probe to reach the highest y position and still eventually be within the target area after any step. What is the highest y position it reaches on this trajectory?

defmodule Day17 do
  def read_input() do
    "target area: " <> rest = AdventOfCode.read_input(17)
    ["x=" <> x_range, "y=" <> y_range] = String.split(rest, ", ")
    [x_min, x_max] = x_range |> String.split("..") |> Enum.map(&String.to_integer/1)
    [y_min, y_max] = y_range |> String.split("..") |> Enum.map(&String.to_integer/1)
    {x_min..x_max, y_min..y_max}
  end
end
{:module, Day17, <<70, 79, 82, 49, 0, 0, 8, ...>>, {:read_input, 0}}
input = Day17.read_input()
{207..263, -115..-63}
defmodule Day17.Part1 do
  def contains?({x_range, y_range}, {x, y}) do
    Enum.any?(x_range, &(&1 == x)) and Enum.any?(y_range, &(&1 == y))
  end

  def simulate(
        target,
        {x_vel, y_vel} = vel,
        {x, y} = point \\ {0, 0},
        {_, y_max} = highest_point \\ {0, 0}
      ) do
    cond do
      contains?(target, point) ->
        highest_point

      cannot_reach_target?(target, vel, point) ->
        nil

      true ->
        {x, y} = {x + x_vel, y + y_vel}

        x_vel =
          cond do
            x_vel > 0 ->
              x_vel - 1

            x_vel < 0 ->
              x_vel + 1

            true ->
              0
          end

        y_vel = y_vel - 1

        highest_point = if y > y_max, do: {x, y}, else: highest_point

        simulate(target, {x_vel, y_vel}, {x, y}, highest_point)
    end
  end

  def cannot_reach_target?({x_range, y_range}, {x_vel, y_vel}, {x, y}) do
    cond do
      x_vel > 0 and x > x_range.last ->
        true

      y_vel < 0 and y < y_range.first ->
        true

      true ->
        false
    end
  end
end
{:module, Day17.Part1, <<70, 79, 82, 49, 0, 0, 14, ...>>, {:cannot_reach_target?, 3}}
x_vel_input = Kino.Input.number("x Velocity", default: 21) |> Kino.render()
y_vel_input = Kino.Input.number("y Velocity", default: 114)
x_vel = Kino.Input.read(x_vel_input)
y_vel = Kino.Input.read(y_vel_input)
result = Day17.Part1.simulate(input, {x_vel, y_vel})
hit_target = result != nil

output =
  Kino.Markdown.new("""
  **Hits target?** #{hit_target}  
  **Highest point:** #{inspect(result)}
  """)

Part Two

Maybe a fancy trick shot isn't the best idea; after all, you only have one probe, so you had better not miss.

To get the best idea of what your options are for launching the probe, you need to find every initial velocity that causes the probe to eventually be within the target area after any step.

In the above example, there are 112 different initial velocity values that meet these criteria:

23,-10  25,-9   27,-5   29,-6   22,-6   21,-7   9,0     27,-7   24,-5
25,-7   26,-6   25,-5   6,8     11,-2   20,-5   29,-10  6,3     28,-7
8,0     30,-6   29,-8   20,-10  6,7     6,4     6,1     14,-4   21,-6
26,-10  7,-1    7,7     8,-1    21,-9   6,2     20,-7   30,-10  14,-3
20,-8   13,-2   7,3     28,-8   29,-9   15,-3   22,-5   26,-8   25,-8
25,-6   15,-4   9,-2    15,-2   12,-2   28,-9   12,-3   24,-6   23,-7
25,-10  7,8     11,-3   26,-7   7,1     23,-9   6,0     22,-10  27,-6
8,1     22,-8   13,-4   7,6     28,-6   11,-4   12,-4   26,-9   7,4
24,-10  23,-8   30,-8   7,0     9,-1    10,-1   26,-5   22,-9   6,5
7,5     23,-6   28,-10  10,-2   11,-1   20,-9   14,-2   29,-7   13,-3
23,-5   24,-8   27,-9   30,-7   28,-5   21,-10  7,9     6,6     21,-5
27,-10  7,2     30,-9   21,-8   22,-7   24,-9   20,-6   6,9     29,-5
8,-2    27,-8   30,-5   24,-7

How many distinct initial velocity values cause the probe to be within the target area after any step?

defmodule Day17.Part2 do
  alias Day17.Part1

  @x_range 1..263
  @y_range -200..250

  def count_valid_velocities(target) do
    for x_vel <- @x_range, y_vel <- @y_range do
      Part1.simulate(target, {x_vel, y_vel})
    end
    |> Enum.filter(&(not is_nil(&1)))
    |> Enum.count()
  end
end
{:module, Day17.Part2, <<70, 79, 82, 49, 0, 0, 8, ...>>, {:count_valid_velocities, 1}}
Day17.Part2.count_valid_velocities(input)
4973

Day 18: Snailfish

You descend into the ocean trench and encounter some snailfish. They say they saw the sleigh keys! They'll even tell you which direction the keys went if you help one of the smaller snailfish with his math homework.

Snailfish numbers aren't like regular numbers. Instead, every snailfish number is a pair - an ordered list of two elements. Each element of the pair can be either a regular number or another pair.

Pairs are written as [x,y], where x and y are the elements within the pair. Here are some example snailfish numbers, one snailfish number per line:

[1,2]
[[1,2],3]
[9,[8,7]]
[[1,9],[8,5]]
[[[[1,2],[3,4]],[[5,6],[7,8]]],9]
[[[9,[3,8]],[[0,9],6]],[[[3,7],[4,9]],3]]
[[[[1,3],[5,3]],[[1,3],[8,7]]],[[[4,9],[6,9]],[[8,2],[7,3]]]]

This snailfish homework is about addition. To add two snailfish numbers, form a pair from the left and right parameters of the addition operator. For example, [1,2] + [[3,4],5] becomes [[1,2],[[3,4],5]].

There's only one problem: snailfish numbers must always be reduced, and the process of adding two snailfish numbers can result in snailfish numbers that need to be reduced.

To reduce a snailfish number, you must repeatedly do the first action in this list that applies to the snailfish number:

  • If any pair is nested inside four pairs, the leftmost such pair explodes.
  • If any regular number is 10 or greater, the leftmost such regular number splits.

Once no action in the above list applies, the snailfish number is reduced.

During reduction, at most one action applies, after which the process returns to the top of the list of actions. For example, if split produces a pair that meets the explode criteria, that pair explodes before other splits occur.

To explode a pair, the pair's left value is added to the first regular number to the left of the exploding pair (if any), and the pair's right value is added to the first regular number to the right of the exploding pair (if any). Exploding pairs will always consist of two regular numbers. Then, the entire exploding pair is replaced with the regular number 0.

Here are some examples of a single explode action:

  • [[[[[9,8],1],2],3],4] becomes [[[[0,9],2],3],4] (the 9 has no regular number to its left, so it is not added to any regular number).
  • [7,[6,[5,[4,[3,2]]]]] becomes [7,[6,[5,[7,0]]]] (the 2 has no regular number to its right, and so it is not added to any regular number).
  • [[6,[5,[4,[3,2]]]],1] becomes [[6,[5,[7,0]]],3].
  • [[3,[2,[1,[7,3]]]],[6,[5,[4,[3,2]]]]] becomes [[3,[2,[8,0]]],[9,[5,[4,[3,2]]]]] (the pair [3,2] is unaffected because the pair [7,3] is further to the left; [3,2] would explode on the next action).
  • [[3,[2,[8,0]]],[9,[5,[4,[3,2]]]]] becomes [[3,[2,[8,0]]],[9,[5,[7,0]]]].

To split a regular number, replace it with a pair; the left element of the pair should be the regular number divided by two and rounded down, while the right element of the pair should be the regular number divided by two and rounded up. For example, 10 becomes [5,5], 11 becomes [5,6], 12 becomes [6,6], and so on.

Here is the process of finding the reduced result of [[[[4,3],4],4],[7,[[8,4],9]]] + [1,1]:

after addition: [[[[[4,3],4],4],[7,[[8,4],9]]],[1,1]]
after explode:  [[[[0,7],4],[7,[[8,4],9]]],[1,1]]
after explode:  [[[[0,7],4],[15,[0,13]]],[1,1]]
after split:    [[[[0,7],4],[[7,8],[0,13]]],[1,1]]
after split:    [[[[0,7],4],[[7,8],[0,[6,7]]]],[1,1]]
after explode:  [[[[0,7],4],[[7,8],[6,0]]],[8,1]]

Once no reduce actions apply, the snailfish number that remains is the actual result of the addition operation: [[[[0,7],4],[[7,8],[6,0]]],[8,1]].

The homework assignment involves adding up a list of snailfish numbers (your puzzle input). The snailfish numbers are each listed on a separate line. Add the first snailfish number and the second, then add that result and the third, then add that result and the fourth, and so on until all numbers in the list have been used once.

For example, the final sum of this list is [[[[1,1],[2,2]],[3,3]],[4,4]]:

[1,1]
[2,2]
[3,3]
[4,4]

The final sum of this list is [[[[3,0],[5,3]],[4,4]],[5,5]]:

[1,1]
[2,2]
[3,3]
[4,4]
[5,5]

The final sum of this list is [[[[5,0],[7,4]],[5,5]],[6,6]]:

[1,1]
[2,2]
[3,3]
[4,4]
[5,5]
[6,6]

Here's a slightly larger example:

[[[0,[4,5]],[0,0]],[[[4,5],[2,6]],[9,5]]]
[7,[[[3,7],[4,3]],[[6,3],[8,8]]]]
[[2,[[0,8],[3,4]]],[[[6,7],1],[7,[1,6]]]]
[[[[2,4],7],[6,[0,5]]],[[[6,8],[2,8]],[[2,1],[4,5]]]]
[7,[5,[[3,8],[1,4]]]]
[[2,[2,2]],[8,[8,1]]]
[2,9]
[1,[[[9,3],9],[[9,0],[0,7]]]]
[[[5,[7,4]],7],1]
[[[[4,2],2],6],[8,7]]

The final sum [[[[8,7],[7,7]],[[8,6],[7,7]]],[[[0,7],[6,6]],[8,7]]] is found after adding up the above snailfish numbers:

  [[[0,[4,5]],[0,0]],[[[4,5],[2,6]],[9,5]]]
+ [7,[[[3,7],[4,3]],[[6,3],[8,8]]]]
= [[[[4,0],[5,4]],[[7,7],[6,0]]],[[8,[7,7]],[[7,9],[5,0]]]]

  [[[[4,0],[5,4]],[[7,7],[6,0]]],[[8,[7,7]],[[7,9],[5,0]]]]
+ [[2,[[0,8],[3,4]]],[[[6,7],1],[7,[1,6]]]]
= [[[[6,7],[6,7]],[[7,7],[0,7]]],[[[8,7],[7,7]],[[8,8],[8,0]]]]

  [[[[6,7],[6,7]],[[7,7],[0,7]]],[[[8,7],[7,7]],[[8,8],[8,0]]]]
+ [[[[2,4],7],[6,[0,5]]],[[[6,8],[2,8]],[[2,1],[4,5]]]]
= [[[[7,0],[7,7]],[[7,7],[7,8]]],[[[7,7],[8,8]],[[7,7],[8,7]]]]

  [[[[7,0],[7,7]],[[7,7],[7,8]]],[[[7,7],[8,8]],[[7,7],[8,7]]]]
+ [7,[5,[[3,8],[1,4]]]]
= [[[[7,7],[7,8]],[[9,5],[8,7]]],[[[6,8],[0,8]],[[9,9],[9,0]]]]

  [[[[7,7],[7,8]],[[9,5],[8,7]]],[[[6,8],[0,8]],[[9,9],[9,0]]]]
+ [[2,[2,2]],[8,[8,1]]]
= [[[[6,6],[6,6]],[[6,0],[6,7]]],[[[7,7],[8,9]],[8,[8,1]]]]

  [[[[6,6],[6,6]],[[6,0],[6,7]]],[[[7,7],[8,9]],[8,[8,1]]]]
+ [2,9]
= [[[[6,6],[7,7]],[[0,7],[7,7]]],[[[5,5],[5,6]],9]]

  [[[[6,6],[7,7]],[[0,7],[7,7]]],[[[5,5],[5,6]],9]]
+ [1,[[[9,3],9],[[9,0],[0,7]]]]
= [[[[7,8],[6,7]],[[6,8],[0,8]]],[[[7,7],[5,0]],[[5,5],[5,6]]]]

  [[[[7,8],[6,7]],[[6,8],[0,8]]],[[[7,7],[5,0]],[[5,5],[5,6]]]]
+ [[[5,[7,4]],7],1]
= [[[[7,7],[7,7]],[[8,7],[8,7]]],[[[7,0],[7,7]],9]]

  [[[[7,7],[7,7]],[[8,7],[8,7]]],[[[7,0],[7,7]],9]]
+ [[[[4,2],2],6],[8,7]]
= [[[[8,7],[7,7]],[[8,6],[7,7]]],[[[0,7],[6,6]],[8,7]]]

To check whether it's the right answer, the snailfish teacher only checks the magnitude of the final sum. The magnitude of a pair is 3 times the magnitude of its left element plus 2 times the magnitude of its right element. The magnitude of a regular number is just that number.

For example, the magnitude of [9,1] is 39 + 21 = 29; the magnitude of [1,9] is 31 + 29 = 21. Magnitude calculations are recursive: the magnitude of [[9,1],[1,9]] is 329 + 221 = 129.

Here are a few more magnitude examples:

  • [[1,2],[[3,4],5]] becomes 143.
  • [[[[0,7],4],[[7,8],[6,0]]],[8,1]] becomes 1384.
  • [[[[1,1],[2,2]],[3,3]],[4,4]] becomes 445.
  • [[[[3,0],[5,3]],[4,4]],[5,5]] becomes 791.
  • [[[[5,0],[7,4]],[5,5]],[6,6]] becomes 1137.
  • [[[[8,7],[7,7]],[[8,6],[7,7]]],[[[0,7],[6,6]],[8,7]]] becomes 3488.

So, given this example homework assignment:

[[[0,[5,8]],[[1,7],[9,6]]],[[4,[1,2]],[[1,4],2]]]
[[[5,[2,8]],4],[5,[[9,9],0]]]
[6,[[[6,2],[5,6]],[[7,6],[4,7]]]]
[[[6,[0,7]],[0,9]],[4,[9,[9,0]]]]
[[[7,[6,4]],[3,[1,3]]],[[[5,5],1],9]]
[[6,[[7,3],[3,2]]],[[[3,8],[5,7]],4]]
[[[[5,4],[7,7]],8],[[8,3],8]]
[[9,3],[[9,9],[6,[4,9]]]]
[[2,[[7,7],7]],[[5,8],[[9,3],[0,2]]]]
[[[[5,2],5],[8,[3,7]]],[[5,[7,5]],[4,4]]]

The final sum is:

[[[[6,6],[7,6]],[[7,7],[7,0]]],[[[7,7],[7,7]],[[7,8],[9,9]]]]

The magnitude of this final sum is 4140.

Add up all of the snailfish numbers from the homework assignment in the order they appear. What is the magnitude of the final sum?

defmodule Day18 do
  def read_input() do
    for line <- AdventOfCode.read_input_lines(18) do
      Jason.decode!(line)
    end
  end
end
{:module, Day18, <<70, 79, 82, 49, 0, 0, 6, ...>>, {:read_input, 0}}
input = Day18.read_input()
[
  [6, [[6, 3], [[1, 4], [8, 4]]]],
  [5, [[[0, 8], [1, 0]], 8]],
  [[[6, '\a\a'], [2, [6, 4]]], [2, 6]],
  [[[[7, 4], [2, 7]], [4, [1, 5]]], [[[0, 5], 5], [[2, 1], [8, 2]]]],
  [[[[5, 9], [7, 2]], [0, '\t\t']], [[[5, 3], '\a\t'], [3, [9, 1]]]],
  [5, [[3, 0], [[8, 2], 5]]],
  [[2, [[0, 8], 7]], [4, '\a\a']],
  [[[[3, 4], 6], [5, [4, 2]]], [[9, [9, 5]], 2]],
  [[7, [0, 5]], [[1, 3], [7, [4, 0]]]],
  [[[6, 2], 3], [[[2, 0], 9], [5, [7, 2]]]],
  [[4, [8, 6]], 8],
  [['\t\b', [[7, 3], [4, 6]]], [5, 3]],
  [[[0, [8, 4]], 8], [[5, [4, 7]], [5, 9]]],
  [[[[0, 8], [3, 7]], [5, 1]], [[5, 6], 2]],
  [[[[5, 8], 0], [[3, 0], 3]], [[6, 5], [[8, 0], [3, 9]]]],
  [[[8, [5, 6]], [6, 4]], [8, 0]],
  [7, [[5, [3, 8]], 3]],
  [[[4, [2, 0]], 2], [[[8, 1], [5, 8]], 5]],
  [9, [[[6, 6], [8, 1]], ['\a\t', 9]]],
  [[[6, 0], [[7, 2], 9]], [[[7, 3], [1, 1]], 0]],
  [[['\a\b', [0, 3]], [5, 9]], [[2, [4, 3]], 7]],
  [[[3, 1], [3, [6, 3]]], [[6, '\b\t'], 7]],
  [[[[1, 1], [0, 5]], [8, 1]], [0, [8, [1, 4]]]],
  [[[6, [8, 6]], '\a\b'], [[7, 3], [3, [5, 8]]]],
  [[[2, 5], [[6, 8], [4, 5]]], [[2, [8, 2]], [2, [3, 2]]]],
  [[[[9, 2], 0], 5], 0],
  [[7, [[3, 7], [0, 9]]], [6, [1, [6, 9]]]],
  [[[[4, 5], 5], 5], 4],
  [[[6, [6, 9]], [8, 3]], 9],
  [[[[2, 7], [8, 6]], 0], [2, [4, 9]]],
  [[[4, '\t\b'], [7, 6]], [7, [[2, 7], [2, 7]]]],
  [[[0, 8], [4, [5, 9]]], [[4, [1, 0]], [6, 8]]],
  [[[2, 4], 9], [['\a\t', 5], [0, 5]]],
  [[[3, [8, 6]], 6], [[8, [6, 7]], [[6, 1], [2, 1]]]],
  [[[0, [0, 5]], [[0, 5], 4]], 9],
  [[[[0, 0], 7], 8], [[8, [4, 6]], 9]],
  [[[1, [1, 1]], [3, [2, 5]]], [6, 6]],
  [[[[3, 7], [6, 1]], [5, 4]], [[0, [2, 6]], [0, 1]]],
  [[1, 1], [3, 4]],
  [9, [[4, '\a\b'], [3, 4]]],
  [[[[5, 3], [5, 9]], 9], [[[2, 4], [2, 7]], [[6, 3], [1, 8]]]],
  [[[2, [2, 2]], ['\b\a', 9]], [[[4, 6], [5, 3]], [[2, 6], 9]]],
  [[[3, 8], [[5, 7], 7]], [[[0, 9], 3], 1]],
  [[[6, [1, 9]], [2, 1]], [[[7, 0], [2, ...]], 8]],
  [[[8, '\t\t'], 1], [[4, 1], [[...], ...]]],
  [[[2, [...]], [[...], ...]], [[3, ...], [...]]],
  [[[[...], ...], 3], [[...], ...]],
  [[[...], ...], [...]],
  [9, ...],
  [...],
  ...
]
defmodule Day18.Part1 do
  def add(a, b) do
    reduce([a, b])
  end

  def magnitude([a, b]) do
    3 * magnitude(a) + 2 * magnitude(b)
  end

  def magnitude(n) when is_integer(n) do
    n
  end

  defmodule State do
    defstruct level: 0, explode_left: nil, explode_right: nil, done: false
  end

  def reduce(num) do
    case try_explode(num) do
      {num, %State{done: false, explode_right: nil}} ->
        case try_split(num) do
          {num, %State{done: false}} ->
            num

          {num, _} ->
            reduce(num)
        end

      {num, _} ->
        reduce(num)
    end
  end

  def try_explode(num, state \\ %State{})

  def try_explode(num, %State{done: true} = state) do
    {num, state}
  end

  def try_explode([left, right], %State{level: 4, explode_right: nil} = state) do
    {0, %{state | explode_left: left, explode_right: right}}
  end

  def try_explode(num, %State{explode_right: exp_right} = state)
      when is_integer(num) and not is_nil(exp_right) do
    {num + exp_right, %{state | explode_right: nil, done: true}}
  end

  def try_explode([left, right], %State{level: level} = state) do
    {left, left_state} = try_explode(left, %{state | level: level + 1})
    {right, right_state} = try_explode(right, left_state)

    if left_state.explode_left == nil and right_state.explode_left != nil do
      {[explode_left(left, right_state.explode_left), right],
       %{right_state | level: level, explode_left: nil}}
    else
      {[left, right], %{right_state | level: level}}
    end
  end

  def try_explode(num, state) do
    {num, state}
  end

  defp explode_left([left, right], to_add) do
    [left, explode_left(right, to_add)]
  end

  defp explode_left(num, to_add) when is_integer(num) do
    num + to_add
  end

  def try_split(num, state \\ %State{})

  def try_split(num, %State{done: true} = state) do
    {num, state}
  end

  def try_split(num, state) when is_integer(num) and num > 9 do
    {[floor(num / 2), ceil(num / 2)], %{state | done: true}}
  end

  def try_split([left, right], state) do
    {left, state} = try_split(left, state)
    {right, state} = try_split(right, state)

    {[left, right], state}
  end

  def try_split(num, state) do
    {num, state}
  end
end
{:module, Day18.Part1, <<70, 79, 82, 49, 0, 0, 19, ...>>, {:try_split, 2}}
[[[[[9, 8], 1], 2], 3], 4]
|> Day18.Part1.reduce()
[[[[0, 9], 2], 3], 4]
[7, [6, [5, [4, [3, 2]]]]]
|> Day18.Part1.reduce()
[7, [6, [5, [7, 0]]]]
[[3, [2, [1, [7, 3]]]], [6, [5, [4, [3, 2]]]]]
|> Day18.Part1.reduce()
[[3, [2, [8, 0]]], [9, [5, [7, 0]]]]
Day18.Part1.add([[[[4, 3], 4], 4], [7, [[8, 4], 9]]], [1, 1])
# [[[[0,7],4],[[7,8],[6,0]]],[8,1]]
[[[[0, 7], 4], ['\a\b', [6, 0]]], [8, 1]]
nums = [
  [[[0, [4, 5]], [0, 0]], [[[4, 5], [2, 6]], [9, 5]]],
  [7, [[[3, 7], [4, 3]], [[6, 3], [8, 8]]]],
  [[2, [[0, 8], [3, 4]]], [[[6, 7], 1], [7, [1, 6]]]],
  [[[[2, 4], 7], [6, [0, 5]]], [[[6, 8], [2, 8]], [[2, 1], [4, 5]]]],
  [7, [5, [[3, 8], [1, 4]]]],
  [[2, [2, 2]], [8, [8, 1]]],
  [2, 9],
  [1, [[[9, 3], 9], [[9, 0], [0, 7]]]],
  [[[5, [7, 4]], 7], 1],
  [[[[4, 2], 2], 6], [8, 7]]
]

nums
|> Enum.reduce(fn b, a -> Day18.Part1.add(a, b) end)
[[['\b\a', '\a\a'], [[8, 6], '\a\a']], [[[0, 7], [6, 6]], '\b\a']]
[
  [[[0, [5, 8]], [[1, 7], [9, 6]]], [[4, [1, 2]], [[1, 4], 2]]],
  [[[5, [2, 8]], 4], [5, [[9, 9], 0]]],
  [6, [[[6, 2], [5, 6]], [[7, 6], [4, 7]]]],
  [[[6, [0, 7]], [0, 9]], [4, [9, [9, 0]]]],
  [[[7, [6, 4]], [3, [1, 3]]], [[[5, 5], 1], 9]],
  [[6, [[7, 3], [3, 2]]], [[[3, 8], [5, 7]], 4]],
  [[[[5, 4], [7, 7]], 8], [[8, 3], 8]],
  [[9, 3], [[9, 9], [6, [4, 9]]]],
  [[2, [[7, 7], 7]], [[5, 8], [[9, 3], [0, 2]]]],
  [[[[5, 2], 5], [8, [3, 7]]], [[5, [7, 5]], [4, 4]]]
]
|> Enum.reduce(fn b, a -> Day18.Part1.add(a, b) end)
|> Day18.Part1.magnitude()
4140
input
|> Enum.reduce(fn b, a -> Day18.Part1.add(a, b) end)
|> Day18.Part1.magnitude()
3892

Part Two

You notice a second question on the back of the homework assignment:

What is the largest magnitude you can get from adding only two of the snailfish numbers?

Note that snailfish addition is not commutative - that is, x + y and y + x can produce different results.

Again considering the last example homework assignment above:

[[[0,[5,8]],[[1,7],[9,6]]],[[4,[1,2]],[[1,4],2]]]
[[[5,[2,8]],4],[5,[[9,9],0]]]
[6,[[[6,2],[5,6]],[[7,6],[4,7]]]]
[[[6,[0,7]],[0,9]],[4,[9,[9,0]]]]
[[[7,[6,4]],[3,[1,3]]],[[[5,5],1],9]]
[[6,[[7,3],[3,2]]],[[[3,8],[5,7]],4]]
[[[[5,4],[7,7]],8],[[8,3],8]]
[[9,3],[[9,9],[6,[4,9]]]]
[[2,[[7,7],7]],[[5,8],[[9,3],[0,2]]]]
[[[[5,2],5],[8,[3,7]]],[[5,[7,5]],[4,4]]]

The largest magnitude of the sum of any two snailfish numbers in this list is 3993. This is the magnitude of [[2,[[7,7],7]],[[5,8],[[9,3],[0,2]]]] + [[[0,[5,8]],[[1,7],[9,6]]],[[4,[1,2]],[[1,4],2]]], which reduces to [[[[7,8],[6,6]],[[6,0],[7,7]]],[[[7,8],[8,8]],[[7,9],[0,6]]]].

What is the largest magnitude of any sum of two different snailfish numbers from the homework assignment?

defmodule Day18.Part2 do
  alias Day18.Part1

  def largest_added_magnitude(nums) do
    sums =
      for a <- nums, b <- nums, a != b do
        Part1.add(a, b)
      end

    sums
    |> Enum.map(&Part1.magnitude/1)
    |> Enum.max()
  end
end
{:module, Day18.Part2, <<70, 79, 82, 49, 0, 0, 7, ...>>, {:largest_added_magnitude, 1}}
Day18.Part2.largest_added_magnitude(input)
4909
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment