Metadata
-
Date
-
Tagged
-
Part of series
- Advent of Code 2022 Day 1
- Advent of Code 2022 Day 2
- Advent of Code 2022 Day 3
- Advent of Code 2022 Day 4
- Advent of Code 2022 Day 5
- Advent of Code 2022 Day 6
- Advent of Code 2022 Day 7
- Advent of Code 2022 Day 8
- Advent of Code 2022 Day 9
- Advent of Code 2022 Day 10
- Advent of Code 2022 Day 11
- Advent of Code 2022 Day 12
- Advent of Code 2022 Day 13
- Advent of Code 2022 Day 14
- Advent of Code 2022 Day 15
- Advent of Code 2022 Day 16
- Advent of Code 2022 Day 17
- Advent of Code 2022 Day 18
- Advent of Code 2022 Day 19
- Advent of Code 2022 Day 20
- Advent of Code 2022 Day 21
- Advent of Code 2022 Day 22
- Advent of Code 2022 Day 23
- Advent of Code 2022 Day 24
- Advent of Code 2022 Day 25
-
Older post
-
Newer post
Advent of Code 2022 Day 25
Day 25: Full of Hot Air
https://adventofcode.com/2022/day/25
The expedition reached the last part, a trip to the north pole via hot air balloon!
It is so cold on the mountain that the fuel needs to be pre-heated. A machine that does this needs to know the total amount to process before it will work.
Today’s input is the fuel requirements for each balloon, written in a weird number format.
An example input looks like this:
So the amount the machine (nicknamed “Bob”) has to process is the sum of all those numbers.
The numbers are in a format called SNAFU.
It’s similar to decimal. Where decimal is based on powers of 10, SNAFU is based on powers of 5.
- In decimal, starting from the right, you have a ones place, a tens place, a hundreds place, …
- In SNAFU, starting from the right, you have a ones place, a fives place, a twenty-fives place, …
The digits are similar too, with two weird ones, -
, and =
.
SNAFU | decimal |
---|---|
0 | 0 |
1 | 1 |
2 | 2 |
- | -1 |
= | -2 |
Some examples converting decimal to SNAFU:
- 10 in decimal is two fives and no ones, in SNAFU it is written 20.
- 8 in decimal iss two fives minus two ones, in SNAFU it is written 2=.
And one in the other direction:
For the SNAFU number 2=-01
2
in the 625s place=
in the 125s place-
in the 25s place0
in the 5s place1
in the 1s place
(2 * 625) + (-2 * 125) + (-1 * 25) + (0 * 5) + (1 * 1). That’s 1250 + -250 + -25 + 0 + 1. 976
Part 1
The question asks what the SNAFU number you have to supply to “Bob“‘s console is.
Helpers
A function that converts SNAFU string to a decimal number.
With a running total that starts at 0: Look at a single SNAFU digit at a time, converting it to decimal. To get the new running total: multiply the current running total by 5 and add the converted digit.
A function that converts a decimal number to a SNAFU string.
Recursion!
It calculates the last SNAFU digit and adds it the the end of all the other SNAFU digits. It does this by using that base 5/power of 5 property. Keeping into account SNAFU digits start at -2.
Using those helpers, the final code:
- Converts all input SNAFU numbers to decimal.
- Sums all those decimal numbers.
- Converts that decimal sum back to SNAFU.
Final code
Part 2
An Elf hands you a single star. Combined with the 49 you gathered on the expedition you make a smoothie and deliver it to the reindeer!