Day 11: Plutonian Pebbles
Mix.install([
{:kino, "~> 0.14.2"}
])
Input
input = Kino.Input.textarea("Please, paste your input:")
defmodule Day11Shared do
def parse(input) do
input
|> Kino.Input.read()
|> String.split(~r/\s+/)
|> Enum.map(&String.to_integer/1)
end
end
Day11Shared.parse(input)
Part 1
The ancient civilization on Pluto was known for its ability to manipulate spacetime, and while The Historians explore their infinite corridors, you’ve noticed a strange set of physics-defying stones.
At first glance, they seem like normal stones: they’re arranged in a perfectly straight line, and each stone has a number engraved on it.
The strange part is that every time you blink, the stones change.
Sometimes, the number engraved on a stone changes. Other times, a stone might split in two, causing all the other stones to shift over a bit to make room in their perfectly straight line.
As you observe them for a while, you find that the stones have a consistent behavior. Every time you blink, 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 number1
. -
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 stones10
and0
.) - 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.
How will the stones evolve if you keep blinking at them? You take a note of the number engraved on each stone in the line (your puzzle input).
If you have an arrangement of five stones engraved with the numbers 0 1 10 99 999
and you blink once, the stones transform as follows:
-
The first stone,
0
, becomes a stone marked1
. -
The second stone,
1
, is multiplied by 2024 to become2024
. -
The third stone,
10
, is split into a stone marked1
followed by a stone marked0
. -
The fourth stone,
99
, is split into two stones marked9
. -
The fifth stone,
999
, is replaced by a stone marked2021976
.
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
.
Here is a longer example:
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 Day11Part1 do
@moduledoc """
We used the brute-force solution, and simply reduce over a list of stones.
By the way, it starts to noticeably slow down after 30-ish step.
"""
def process(stones) do
stones
|> blink(25)
|> Enum.count()
end
def blink(stones, max_steps), do: blink(stones, 0, max_steps)
def blink(stones, max, max), do: stones
def blink(stones, step, max_steps) do
stones
|> Enum.reduce([], fn stone, stones ->
digits = Integer.digits(stone)
diglen = length(digits)
cond do
stone == 0 ->
[1 | stones]
rem(diglen, 2) == 0 ->
{left, right} = Enum.split(digits, div(diglen, 2))
[Integer.undigits(right) | [Integer.undigits(left) | stones]]
true ->
[stone * 2024 | stones]
end
end)
|> blink(step + 1, max_steps)
end
end
input |> Day11Shared.parse() |> Day11Part1.process()
# 182081 is the right answer
Part 2
The Historians sure are taking a long time. To be fair, the infinite corridors are very large.
How many stones would you have after blinking a total of 75 times?
defmodule Day11Part2 do
@moduledoc """
This part cannot be solved in a brute-force manner – just too many items in the list...
The trick is in the data-structure: instead of a list with stones, we shall store them
as map: basically, a statistic of how many of each stone is there at any moment. Here is
an example:
1036288 7 2 20 24 4048 1 4048 8096 28 67 60 32
%{
1 => 1,
2 => 1,
7 => 1,
20 => 1,
24 => 1,
28 => 1,
32 => 1,
60 => 1,
67 => 1,
4048 => 2,
8096 => 1,
1036288 => 1
}
At the each step (blink), we apply rules to the map's keys, and then convert it to a new
statistical map.
"""
def process(stones) do
stones
|> Map.new(fn stone -> {stone, 1} end)
|> blink(75)
|> Map.values()
|> Enum.sum()
end
def blink(stones, max_steps), do: blink(stones, 0, max_steps)
def blink(stones, max_steps, max_steps), do: stones
def blink(stones, step, max_steps) do
# IO.inspect(stones, label: "step#{step}")
stones
|> Enum.flat_map(&transmute/1)
|> Enum.reduce(%{}, fn {new_stone, new_count}, acc ->
Map.update(acc, new_stone, new_count, &(&1 + new_count))
end)
|> blink(step + 1, max_steps)
end
def transmute({stone, count}) do
digits = Integer.digits(stone)
diglen = length(digits)
cond do
stone == 0 ->
[{1, count}]
rem(diglen, 2) == 0 ->
{left, right} = Enum.split(digits, div(diglen, 2))
left = Integer.undigits(left)
right = Integer.undigits(right)
[{left, count}, {right, count}]
true ->
[{stone * 2024, count}]
end
end
end
input |> Day11Shared.parse() |> Day11Part2.process()
# 216318908621637 is the right answer