This was a fairly simple problem based around parsing a file full of numbers and applying some basic math, involving iteration and/or recursion for Part II. This is a good way to ease into Advent of Code and to try out a new language.
- Ruby (tests embedded)
- Elixir (test) - Uses tail call optimization
- Go (test)
- Python
- Raku
- Crystal Main Lib (test)
- Haskell Main Lib (test)
input_large.txt
is a fan made input containing 1,000,000 lines of numbers 46
digits long.
- Elixir - After implementing Flow,
solves in 4.7 seconds on my quad core laptop.
make big
to run. Was 19 seconds without concurrency. - Go - Runs in 7 seconds using BigNum and channels. Adding channels to Part 1 actually slowed it down, but I might not be implementing them here in the best way.
Overall, Elixir made solving the large input very easy. It supports BigNums
out of the box - all ints have large support by default - I was using them all
along, which I didn't expect. The Flow
library provided one way to
parallelize the work easily, but I also tried a manual implementation which
was just as fast.
- Elixir Manual - Manual chunking/parallelize: 5 seconds.
- Elixir Flow - Using Flow library: 5 seconds.
- Elixir Naive - Spawning one million processes at once. Slow: 35 seconds. It's actually faster to run in one process, but hey, I wanted to spawn a million.
On the other hand, my Go program needed to be converted to use big.Int, which
was not fun. All of the basic operations need their own function calls, and
they expect you to pass pointers to BigInts around, not the actual values.
When it came to adding concurrency, I first tried passing pointers across the
channels but the program paniced and crashed. So I changed to values.. it
ended up working, but it was a bit of a *
and &
soup, which I didn't like.
I made some visualizations of the problem using react-three-fiber. They're quite crude, with relative box area representing the amount of fuel used for each number, but it's my first time using react-three-fiber to draw in 3d, so I'm satisfied.
2019 Day 01 on AdventOfCode.com
The Elves quickly load you into a spacecraft and prepare to launch.
At the first Go / No Go poll, every Elf is Go until the Fuel Counter-Upper. They haven't determined the amount of fuel required yet.
Fuel required to launch a given module is based on its mass. Specifically, to find the fuel required for a module, take its mass, divide by three, round down, and subtract 2.
For example:
- For a mass of
12
, divide by 3 and round down to get4
, then subtract 2 to get2
. - For a mass of
14
, dividing by 3 and rounding down still yields4
, so the fuel required is also2
. - For a mass of
1969
, the fuel required is654
. - For a mass of
100756
, the fuel required is33583
.
The Fuel Counter-Upper needs to know the total fuel requirement. To find it, individually calculate the fuel needed for the mass of each module (your puzzle input), then add together all the fuel values.
What is the sum of the fuel requirements for all of the modules on your spacecraft?
During the second Go / No Go poll, the Elf in charge of the Rocket Equation Double-Checker stops the launch sequence. Apparently, you forgot to include additional fuel for the fuel you just added.
Fuel itself requires fuel just like a module - take its mass, divide by three, round down, and subtract 2. However, that fuel also requires fuel, and that fuel requires fuel, and so on. Any mass that would require negative fuel should instead be treated as if it requires zero fuel; the remaining mass, if any, is instead handled by wishing really hard, which has no mass and is outside the scope of this calculation.
So, for each module mass, calculate its fuel and add it to the total. Then, treat the fuel amount you just calculated as the input mass and repeat the process, continuing until a fuel requirement is zero or negative. For example:
- A module of mass
14
requires2
fuel. This fuel requires no further fuel (2 divided by 3 and rounded down is0
, which would call for a negative fuel), so the total fuel required is still just2
. - At first, a module of mass
1969
requires654
fuel. Then, this fuel requires216
more fuel (654 / 3 - 2
).216
then requires70
more fuel, which requires21
fuel, which requires5
fuel, which requires no further fuel. So, the total fuel required for a module of mass1969
is654 + 216 + 70 + 21 + 5 = 966
. - The fuel required by a module of mass
100756
and its fuel is:33583 - 11192 + 3728 + 1240 + 411 + 135 + 43 + 12 + 2 = 50346
.
What is the sum of the fuel requirements for all of the modules on your spacecraft when also taking into account the mass of the added fuel? (Calculate the fuel requirements for each module separately, then add them all up at the end.)
Created: Mon 02 Dec 2019 05:12:11 PM CST
Last Modified: Mon 02 Dec 2019 05:12:32 PM CST