Powered by AppSignal & Oban Pro
Would you like to see your link here? Contact us

Advent of Code - Day 11

livebooks/day11.livemd

Advent of Code - Day 11

Mix.install(
  [
    {:flow, "~> 1.2"}
  ]
)

Part 1

They’re arranged in a perfectly straight line, and each stone has a number engraved on it.

The stones each simultaneously change according to the first applicable rule in this list:

  • If the stone is engraved with the number 0, it is replaced by a stone engraved with the number 1.
  • If the stone is engraved with a number that has an even number of digits, it is replaced by two stones. The left half of the digits are engraved on the new left stone, and the right half of the digits are engraved on the new right stone. (The new numbers don’t keep extra leading zeroes: 1000 would become stones 10 and 0.)
  • If none of the other rules apply, the stone is replaced by a new stone; the old stone’s number multiplied by 2024 is engraved on the new stone.

No matter how the stones change, their order is preserved, and they stay on their perfectly straight line.

raw_sample = "0 1 10 99 999"

  • The first stone, 0, becomes a stone marked 1.
  • The second stone, 1, is multiplied by 2024 to become 2024.
  • The third stone, 10, is split into a stone marked 1 followed by a stone marked 0.
  • The fourth stone, 99, is split into two stones marked 9.
  • The fifth stone, 999, is replaced by a stone marked 2021976.

So, after blinking once, your five stones would become an arrangement of seven stones engraved with the numbers 1 2024 1 0 9 9 2021976.

Initial arrangement: 125 17

After 1 blink: 253000 1 7

After 2 blinks: 253 0 2024 14168

After 3 blinks: 512072 1 20 24 28676032

After 4 blinks: 512 72 2024 2 0 2 4 2867 6032

After 5 blinks: 1036288 7 2 20 24 4048 1 4048 8096 28 67 60 32

After 6 blinks: 2097446912 14168 4048 2 0 2 4 40 48 2024 40 48 80 96 2 8 6 7 6 0 3 2

In this example, after blinking six times, you would have 22 stones. After blinking 25 times, you would have 55312 stones!

Consider the arrangement of stones in front of you. How many stones will you have after blinking 25 times?

defmodule Part1 do
  def parse_input(input) do
    input
    |> String.trim("\n")
    |> String.split(" ", trim: true)
  end

  def mutate(stone) do
    stone_num = stone |> String.to_integer()
    digits = stone |> String.graphemes()

    cond do
      stone_num == 0 ->
        ["1"]

      digits |> length() |> rem(2) == 0 ->
        digit_count = digits |> length()
        {left, right} = digits |> Enum.split(div(digit_count, 2))

        left_stone = left |> Enum.join()
        right_stone = right |> Enum.join() |> String.to_integer() |> Integer.to_string()

        [left_stone, right_stone]

      true ->
        [(stone_num * 2024) |> Integer.to_string()]
    end
  end

  def solve(stones, times) do
    1..times
    |> Enum.reduce(stones, fn _, stones ->
      stones
      |> Enum.flat_map(fn stone ->
        mutate(stone)
      end)
    end)
  end
end
Part1.mutate("1234") |> dbg()
Part1.mutate("1200") |> dbg()
Part1.mutate("120")
Part1.solve(["0", "1", "1"], 1)
Part1.parse_input(raw_sample)
|> Part1.solve(1)
Part1.parse_input("125 17")
|> Part1.solve(25)
|> length()
File.read!(
  "/Users/errantsky/elixir-projects/phoenix-projects/advent_of_code_2024/inputs/day11.txt"
)
|> Part1.parse_input()
|> Part1.solve(25)
|> length()

Part 2

Need to make the blink number part of the recursion hint: map of counts

defmodule Part2 do
  def parse_input(input) do
    input
    |> String.trim("\n")
    |> String.split(" ", trim: true)
  end

  def mutate(stone) do
    stone_num = stone |> String.to_integer()
    digits = stone |> String.graphemes()

    cond do
      stone_num == 0 ->
        ["1"]

      digits |> length() |> rem(2) == 0 ->
        digit_count = digits |> length()
        {left, right} = digits |> Enum.split(div(digit_count, 2))

        left_stone = left |> Enum.join()
        right_stone = right |> Enum.join() |> String.to_integer() |> Integer.to_string()

        [left_stone, right_stone]

      true ->
        [(stone_num * 2024) |> Integer.to_string()]
    end
  end

  def solve(stones, times) do
    1..times
    |> Enum.reduce(stones, fn _, stones ->
      stones
      |> Enum.flat_map(fn stone ->
        mutate(stone)
      end)
    end)
  end
end
File.read!(
  "/Users/errantsky/elixir-projects/phoenix-projects/advent_of_code_2024/inputs/day11.txt"
)
|> Part2.parse_input()
|> Part2.solve(25)
|> length()
File.read!(
  "/Users/errantsky/elixir-projects/phoenix-projects/advent_of_code_2024/inputs/day11.txt"
)
|> Part2.parse_input()
|> Part2.solve(75)
|> length()