Day Two: Fulminating Farmland
Mix.install([
{:kino, "~> 0.19.0"}
])
Problem
https://adventofcode.com/2026/day/2
defmodule Problem do
defmacro __using__(_opts \\ []) do
quote do
import Kernel, except: [inspect: 1, inspect: 2]
import unquote(__MODULE__)
end
end
def inspect(term, opts \\ []) do
Kernel.inspect(term, Keyword.merge([
pretty: true,
limit: :infinity,
printable_limit: :infinity,
structs: false,
charlists: :as_lists,
custom_options: [sort_maps: true]
], opts))
end
def parse(string) when is_binary(string) do
string |> String.trim() |> String.graphemes() |> Enum.map(&String.to_atom/1)
end
def travel(path, track \\ [:r, :o, :y, :g, :b, :v], starting_at \\ :r) do
track_length = length(track)
starting_index = Enum.find_index(track, & &1 == starting_at)
for travel_to <- path, reduce: [{starting_index, 0}] do
[{current_index, _steps_taken} | _] = steps ->
next_index = Enum.find_index(track, & &1 == travel_to)
distance = if next_index <= current_index do
track_length - current_index + next_index
else
next_index - current_index
end
[{next_index, distance} | steps]
end
|> :lists.reverse
end
def optimize(path, track \\ [:r, :o, :y, :g, :b, :v], starting_at \\ :r) do
[head | tail] = track
reversed_track = tail ++ [head]
reversed_track = :lists.reverse(reversed_track)
{_reversed_track, move_priorities} = Enum.reduce(track, {reversed_track, %{}}, fn move, {reversed_track, move_priorities} ->
move_priorities = Map.put(move_priorities, move, reversed_track)
{head, tail} = List.pop_at(reversed_track, -1)
next_track = [head | tail]
{next_track, move_priorities}
end)
moves = Enum.frequencies(path)
[^starting_at | path] = [:move]
|> Stream.cycle()
|> Enum.reduce_while({moves, [starting_at]}, fn :move, {moves, [position | path]} ->
if moves |> Map.values |> Enum.all?(& &1 == 0) do
{:halt, [position | path]}
else
{moves, next_move} = Enum.reduce_while(Map.get(move_priorities, position), moves, fn move, moves ->
if Map.get(moves, move, 0) > 0 do
{:halt, {Map.update(moves, move, 0, & &1 - 1), move}}
else
{:cont, moves}
end
end)
{:cont, {moves, [next_move | [position | path]]}}
end
end)
|> :lists.reverse()
path
end
end
Part One
You ride the train to its first stop: a large farm surrounded by fields. The Elves explain that you're not actually here for anything the farm grows; apparently, this farm also collects thunder from passing storms. This explains the surprisingly rumbly crates being loaded onto the train.
In an attempt to distance yourself from the noise, you take a walk and explore some of the farm. After a few minutes, you come across some Elves tinkering with an interesting-looking robot.
From a brief conversation, you learn that the robot's job is to follow a long series of colorful waypoints, automatically watering, fertilizing, and weeding as it travels. The Elves are trying to figure out how far the robot's current program will take it.
The waypoints appear in a repeating pattern of six colors: red (r), orange (o), yellow (y), green (g), blue (b), and violet (v). The robot starts at a red waypoint. So, the first few waypoints look like this:
roygbvroygbvroygbvroygbvroygbvroygbvroygbvroygbv...
The robot has a program (your puzzle input) which tells it which color of waypoint to go to next. The robot always goes to the very next waypoint of the indicated color. So, if the robot were at a red waypoint, an instruction of o would cause it to travel a distance of 1 (to the waypoint directly in front of it), while an instruction of r would cause it to travel a distance of 6 before it found the next red waypoint.
For example, using ^ to mark the robot's current position, suppose the robot had a program of obbvb:
roygbvroygbvroygbvroygbvroygbvroygbvroygbvroygbv
^ initial robot position
roygbvroygbvroygbvroygbvroygbvroygbvroygbvroygbv
^ robot travels distance 1 to next orange waypoint
roygbvroygbvroygbvroygbvroygbvroygbvroygbvroygbv
^ robot travels distance 3 to next blue waypoint
roygbvroygbvroygbvroygbvroygbvroygbvroygbvroygbv
^ robot travels distance 6 to next blue waypoint
roygbvroygbvroygbvroygbvroygbvroygbvroygbvroygbv
^ robot travels distance 1 to next violet waypoint
roygbvroygbvroygbvroygbvroygbvroygbvroygbvroygbv
^ robot travels distance 5 to next blue waypoint
After these instructions, the robot has traveled a total distance of 16.
Here are a few more examples:
A robot with program roygbv would travel a total distance of 11.
A robot with program vbgyor would travel a total distance of 30.
A robot with program vvvvvv would travel a total distance of 35.
However, the robot's actual program is much longer. Starting at a red waypoint, what distance would the program cause the robot to travel?
Sample Input One
sample_input_one = "obbvb"
Solution One
defmodule Problem.One do
use Problem
def solve(parsed) do
parsed |> travel |> Enum.map(&elem(&1, 1)) |> Enum.sum()
end
end
Actual Input
actual_input = "gvvggogbvogbbrrvgvrbgbbggrvorvoobovggoogorgvbvbrorgbvorogggbvvogogogbobvgrvbrbbgbbgorvvogbbogbgbbrgooobbgrgbvrggvvbovorbrvggrrvgbrogbbvbovbbrbrrggbvbvgorbvobbobbbrbrgvbggvgbobbgrvrbbrrobbgobobgrroooogbgbbgbgbvobogrorovogrbbvgggrgbggbrrrbvgrrgoggrgbrbgrbgrbvgbogorogvobovoggrvrroobbrorgrggogorbroggvrvrrrgbooogbrbbrbvrrgggbboovgrrbggvgrbboboorvggborboobvvgbgbgrbgrrbroobboobvrrgbgggrogborgrrgogoobrrrovgbbvobooggvggrgobogbbrrbbvobovggrgrovrgrrgrvboobvggovbggboorovbovgbbgoobbvbgorovgrrrvbbrovbbgbbgbbgogrbrbgbbgrogobbggbgoboogbbgggbbrbgvvrvbrrgbroorgbbgoorgbbbggbogbgovrgobbbvrgorrrggobooogbgbrbbbrobbovvbgooogbovbvbbroogbrvvbgrorbbovbrggboggrbbogvrrbbggggbrggggggboogrrovogbrbvbvbrrgogrgoobgbbooobrvrrvggovvggborogvbbroggbbbgrbogrrbobggbggrbovrbvbrrgrrrgobrgbgbobororbgbgvbbbvrrrorgobobovbogrobbrbgogvbobgovrgbgggvggrggbroogvvrvggobobrovrogrorvrborrgoggbrggbrggbgrgbbgbbggoggroogrbvbooorrbobvgbbogovgorvvgggvobovrbrgobvrrobbgbrrbrrrobbgrvrgorbbrbvbvobbogrbgbgrooobvbobgbgboggbrgggbbvbggoogrgrovogrggvbrrrrobrovgboorggrrrbvorgborvbgvvroggbrrrr"
Part Two
Unfortunately, you've confirmed the Elves' suspicions: this program simply won't take the robot far enough.
The Elves would just give the robot a longer program, but some of the keys on their keyboard are broken, so they can't type the letters they need. The only thing they can do is rearrange the colors in the existing program.
However, they do know a trick for making the robot travel long distances. Whenever the robot is about to move, it should always get the instruction that would cause it to move the furthest. So, if the robot is at an orange waypoint, it would be best if the robot's next instruction were o (for distance 6); if there aren't any o instructions left, the next-best instruction would be r (for distance 5), and so on.
So, your job is to rearrange the colors in the robot's program so that each color causes the robot to move as far as possible. For example, suppose the robot had an initial program of rybryb:
- The robot starts at a red waypoint.
- The first instruction in the rearranged program could be
r(distance 6),y(distance 2), orb(distance 4), so the first instruction should ber. - The next instruction could again be
r(distance 6),y(distance 2), orb(distance 4), so the next instruction should ber. - Next, there are no more
rinstructions left to use; the options arey(distance 2) orb(distance 4). So, the next instruction should beb. - Now, the options are
b(distance 6) ory(distance 4), so the next instruction should again beb. - Finally, the only instructions left are all
y, so the last two instructions will both bey(distance 4, then distance 6).
So, the program rybryb could be rearranged into rrbbyy, giving a total distance of 32.
Here are a few more examples:
- The program
vrvbvgcould be rearranged intorvvvbg, a total distance of 33. - The program
groovyyoyocould be rearranged for a total distance of 55. - The program
roygbvroygbvroygbvcould be rearranged for a total distance of 103.
Use the Elves' trick to rearrange the instructions in the program. Starting at a red waypoint, what distance would the rearranged program cause the robot to travel?
Sample Input Two
sample_input_two = "rybryb"
Solution Two
defmodule Problem.Two do
use Problem
def solve(parsed) do
parsed |> optimize |> travel |> Enum.map(&elem(&1, 1)) |> Enum.sum()
end
end
Answers
form =
Kino.Control.form(
[
problem: Kino.Input.select("Problem", [
one: "Problem One",
two: "Problem Two"
]),
input: Kino.Input.select("Input", [
sample_input: "Sample Input",
actual_input: "Actual Input"
])
],
submit: "Solve"
) |> Kino.render
frame = Kino.Frame.new() |> Kino.render()
Kino.listen(form, fn event ->
use Problem
{problem, input} = case {event.data.problem, event.data.input} do
{:one, :sample_input} -> {Problem.One, sample_input_one}
{:one, :actual_input} -> {Problem.One, actual_input}
{:two, :sample_input} -> {Problem.Two, sample_input_two}
{:two, :actual_input} -> {Problem.Two, actual_input}
end
result = input
|> Problem.parse()
|> problem.solve()
|> inspect()
|> Kino.Text.new(terminal: false)
Kino.Frame.clear(frame)
Kino.Frame.append(frame, result)
end)