Advent of Code - Day 7: The Treachery of Whales
Mix.install([:kino])
Day 7: The Treachery of Whales
- AOC Link
- tylerj GitHub
- José Valim GitHub
- José Valim Twitch Stream (N/A)
Part One: Prompt
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?
To begin, get your puzzle input.
Part One: Solution
input = Kino.Input.textarea("Please paste your input file:")
list =
input
|> Kino.Input.read()
|> String.split(",", trim: true)
|> Enum.map(&String.to_integer/1)
[1101, 1, 29, 67, 1102, 0, 1, 65, 1008, 65, 35, 66, 1005, 66, 28, 1, 67, 65, 20, 4, 0, 1001, 65, 1,
65, 1106, 0, 8, 99, 35, 67, 101, 99, 105, 32, 110, 39, 101, 115, 116, 32, 112, 97, 115, 32, 117,
110, 101, 32, 105, ...]
# Let's find the location of the median,
# and determine if the list has an even or odd number of entries
len = length(list)
median_index = {div(len, 2), rem(len, 2)}
{500, 0}
# Sort so we can find the median(s)
sorted = Enum.sort(list)
[0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 3, 3, 3, 3, 4, 5, 5, 5, 6, 7, 7, 8, 9, 10, 10,
10, 10, 11, 11, 11, 12, 12, 13, 14, 15, 15, 16, 16, 16, 16, 16, 16, 17, ...]
medians =
case median_index do
# If even number of entries, we need to try both median values
{x, 0} -> Enum.slice(sorted, x - 1, 2)
# If odd number of entries, we only have 1 median value
{x, 1} -> Enum.slice(sorted, x, 1)
end
[358, 358]
defmodule CrabWalker do
def find_shortest_total_burn(positions, list, incr \\ 0) do
positions
# Only calculate once per unique position
|> Enum.uniq()
# For each position, calculate the total burn
|> Enum.map(&total_burn_for_pos(&1, list, incr))
# Take the smallest total burn as the winner
|> Enum.min()
end
# given a position, calculate and sum the burns for each item in the list
defp total_burn_for_pos(pos, list, incr) do
Enum.reduce(list, 0, fn x, total_burn ->
total_burn + calc_burn(pos, x, incr)
end)
end
# linear burn rate
defp calc_burn(x, y, 0) do
abs(x - y)
end
# incremental burn rate
defp calc_burn(x, y, 1) do
for x <- 1..abs(x - y), reduce: 0 do
acc -> acc + x
end
end
end
{:module, CrabWalker, <<70, 79, 82, 49, 0, 0, 11, ...>>, {:calc_burn, 3}}
CrabWalker.find_shortest_total_burn(medians, list)
344138
How much fuel must they spend to align to that position?
Your puzzle answer was 344138.
The first half of this puzzle is complete! It provides one gold star: *
Part Two: Prompt
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?
Part Two: Solution
I modified the code from Part 1 to introduce a CrabWalker module to help perform these calculations.
Check that code for details on the functions, with comments, that were used to solve Part Two.
# Let's find the mean
mean = Enum.sum(list) / length(list)
482.596
# Check the whole integers around the mean for the shortest burn
# Pass in incremental value of 1, to indicate that the burn is calculated
# at an increasing rate
CrabWalker.find_shortest_total_burn(
floor(mean)..ceil(mean),
list,
1
)
94862125
How much fuel must they spend to align to that position?
Your puzzle answer was 94862124.
Both parts of this puzzle are complete! They provide two gold stars: **
At this point, you should return to your Advent calendar and try another puzzle.
If you still want to see it, you can get your puzzle input.