-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6fd5001
commit f27b3b7
Showing
5 changed files
with
348 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
## --- Day 25: Full of Hot Air --- | ||
|
||
As the expedition finally reaches the extraction point, several large [hot air balloons](https://en.wikipedia.org/wiki/Hot_air_balloon) drift down to meet you. Crews quickly start unloading the equipment the balloons brought: many hot air balloon kits, some fuel tanks, and a __fuel heating machine__. | ||
|
||
The fuel heating machine is a new addition to the process. When this mountain was a volcano, the ambient temperature was more reasonable; now, it's so cold that the fuel won't work at all without being warmed up first. | ||
|
||
The Elves, seemingly in an attempt to make the new machine feel welcome, have already attached a pair of [googly eyes](https://en.wikipedia.org/wiki/Googly_eyes) and started calling it "Bob". | ||
|
||
To heat the fuel, Bob needs to know the total amount of fuel that will be processed ahead of time so it can correctly calibrate heat output and flow rate. This amount is simply the __sum__ of the fuel requirements of all of the hot air balloons, and those fuel requirements are even listed clearly on the side of each hot air balloon's burner. | ||
|
||
You assume the Elves will have no trouble adding up some numbers and are about to go back to figuring out which balloon is yours when you get a tap on the shoulder. Apparently, the fuel requirements use numbers written in a format the Elves don't recognize; predictably, they'd like your help deciphering them. | ||
|
||
You make a list of all of the fuel requirements (your puzzle input), but you don't recognize the number format either. For example: | ||
|
||
``` | ||
1=-0-2 | ||
12111 | ||
2=0= | ||
21 | ||
2=01 | ||
111 | ||
20012 | ||
112 | ||
1=-1= | ||
1-12 | ||
12 | ||
1= | ||
122 | ||
``` | ||
|
||
Fortunately, Bob is labeled with a support phone number. Not to be deterred, you call and ask for help. | ||
|
||
"That's right, just supply the fuel amount to the-- oh, for more than one burner? No problem, you just need to add together our Special Numeral-Analogue Fuel Units. Patent pending! They're way better than normal numbers for--" | ||
|
||
You mention that it's quite cold up here and ask if they can skip ahead. | ||
|
||
"Okay, our Special Numeral-Analogue Fuel Units - SNAFU for short - are sort of like normal numbers. You know how starting on the right, normal numbers have a ones place, a tens place, a hundreds place, and so on, where the digit in each place tells you how many of that value you have?" | ||
|
||
"SNAFU works the same way, except it uses powers of five instead of ten. Starting from the right, you have a ones place, a fives place, a twenty-fives place, a one-hundred-and-twenty-fives place, and so on. It's that easy!" | ||
|
||
You ask why some of the digits look like `-` or `=` instead of "digits". | ||
|
||
"You know, I never did ask the engineers why they did that. Instead of using digits four through zero, the digits are `2`, `1`, `0`, __minus__ (written `-`), and __double-minus__ (written `=`). Minus is worth -1, and double-minus is worth -2." | ||
|
||
"So, because ten (in normal numbers) is two fives and no ones, in SNAFU it is written `20`. Since eight (in normal numbers) is two fives minus two ones, it is written `2=`." | ||
|
||
"You can do it the other direction, too. Say you have the SNAFU number `2=-01`. That's `2` in the 625s place, `=` (double-minus) in the 125s place, `-` (minus) in the 25s place, `0` in the 5s place, and `1` in the 1s place. (2 times 625) plus (-2 times 125) plus (-1 times 25) plus (0 times 5) plus (1 times 1). That's 1250 plus -250 plus -25 plus 0 plus 1. __976__!" | ||
|
||
"I see here that you're connected via our premium uplink service, so I'll transmit our handy SNAFU brochure to you now. Did you need anything else?" | ||
|
||
You ask if the fuel will even work in these temperatures. | ||
|
||
"Wait, it's __how__ cold? There's no __way__ the fuel - or __any__ fuel - would work in those conditions! There are only a few places in the-- where did you say you are again?" | ||
|
||
Just then, you notice one of the Elves pour a few drops from a snowflake-shaped container into one of the fuel tanks, thank the support representative for their time, and disconnect the call. | ||
|
||
The SNAFU brochure contains a few more examples of decimal ("normal") numbers and their SNAFU counterparts: | ||
|
||
``` | ||
Decimal SNAFU | ||
1 1 | ||
2 2 | ||
3 1= | ||
4 1- | ||
5 10 | ||
6 11 | ||
7 12 | ||
8 2= | ||
9 2- | ||
10 20 | ||
15 1=0 | ||
20 1-0 | ||
2022 1=11-2 | ||
12345 1-0---0 | ||
314159265 1121-1110-1=0 | ||
``` | ||
|
||
Based on this process, the SNAFU numbers in the example above can be converted to decimal numbers as follows: | ||
|
||
``` | ||
SNAFU Decimal | ||
1=-0-2 1747 | ||
12111 906 | ||
2=0= 198 | ||
21 11 | ||
2=01 201 | ||
111 31 | ||
20012 1257 | ||
112 32 | ||
1=-1= 353 | ||
1-12 107 | ||
12 7 | ||
1= 3 | ||
122 37 | ||
``` | ||
|
||
In decimal, the sum of these numbers is `4890`. | ||
|
||
As you go to input this number on Bob's console, you discover that some buttons you expected are missing. Instead, you are met with buttons labeled `=`, `-`, `0`, `1`, and `2`. Bob needs the input value expressed as a SNAFU number, not in decimal. | ||
|
||
Reversing the process, you can determine that for the decimal number `4890`, the SNAFU number you need to supply to Bob's console is `2=-1=0`. | ||
|
||
The Elves are starting to get cold. __What SNAFU number do you supply to Bob's console?__ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
1=-2==20= | ||
110 | ||
2=0200-1==-=01=-- | ||
1010=0=-1===-1 | ||
222==0 | ||
1==11001=0-0101=11= | ||
1=2-02102101-=02=- | ||
2-=012 | ||
1--0-- | ||
1=2102000=1=- | ||
1-20=2--==02--222 | ||
1001=2 | ||
12-=-=--=-=2-==== | ||
121--0-=0=12= | ||
1=0 | ||
11=10=- | ||
1--10-022- | ||
22-02=0-2-0222-2=1 | ||
11==1-0=2 | ||
1-12-2011=11-21 | ||
101001-220 | ||
2012022122-1-=220 | ||
1--=11=2-012 | ||
111-1=1-2=1= | ||
2=1-1-=1 | ||
21=-=020=0200-=1 | ||
1-=02 | ||
10-- | ||
221=121-22-2=-=-1 | ||
101 | ||
1-=2 | ||
101=220-212-- | ||
1-102=02000 | ||
1=-=1-=2 | ||
1-0102-121 | ||
2-=211= | ||
1-=2=2=122-112110 | ||
1=2=-=2=-2=--1- | ||
20222=0=21 | ||
2 | ||
10-0=2-00-020101 | ||
2-200=-01-2-220-111 | ||
2-0 | ||
1=121000 | ||
2--12 | ||
1=2==21-0010=002-0 | ||
1=-1022 | ||
10=100-001=22=11==- | ||
1000-00--10==-2= | ||
21022121-0=- | ||
120-222=-2=2--01-1 | ||
1=0=2- | ||
1=-00-1= | ||
1-121-1-=-11--12 | ||
21001-222==0-22= | ||
12-1-=---01-1-=- | ||
2==22-00-11 | ||
11200=2=0 | ||
1=--1010=10- | ||
12-1= | ||
1=-1--001 | ||
1=02==22=120 | ||
2- | ||
2-=0021-=000=1 | ||
1-1-= | ||
1=- | ||
120=00==1- | ||
2-=0-220=-12 | ||
2==1- | ||
1-0000-2-===1-20=2 | ||
2=-0=-=0-0= | ||
1=122-=2-11=0=-1=0 | ||
2-2-1 | ||
1=0-==102102== | ||
2=-1 | ||
1=1- | ||
1012 | ||
2002-22-011-1 | ||
2200-----101 | ||
12 | ||
1--==2210 | ||
1001= | ||
2-==- | ||
11111 | ||
2=1=012- | ||
1-==01-=0 | ||
1--20221 | ||
212=22122 | ||
11-00211=12- | ||
1=2-11=102---20 | ||
1-==20-1--010 | ||
1--01102=0-12-0--0-0 | ||
20-22 | ||
20-10-0222 | ||
100--2=2-1-0220 | ||
2=---02100-1210 | ||
1=-=0=012 | ||
102100-1-0=000- | ||
1-221=1 | ||
2-- | ||
10-=1-0012 | ||
2-20-0221=-11-2=-2 | ||
111-1--2-0--122 | ||
1==- | ||
1==1--022=1= | ||
2=011-20=120211 | ||
2--010=200 | ||
1-21=02010- | ||
2==12=00=1=- | ||
1===2---=1-=-0-1 | ||
21=-021-=1=1210 | ||
1==0-1-0=-0-- | ||
11-21220 | ||
11=-=-=21=0-=1 | ||
22111= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import input from "../../utils/deno/input.ts"; | ||
import { solvePart1 } from "./solutions.ts"; | ||
|
||
console.log(solvePart1(input)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { Input } from "../../utils/deno/input.ts"; | ||
import { sum } from "../../utils/deno/arrays.ts"; | ||
|
||
export function toNumber(x: string): number { | ||
return x | ||
.split("") | ||
.map((x) => { | ||
if (x === "-") { | ||
return -1; | ||
} else if (x === "=") { | ||
return -2; | ||
} else { | ||
return Number(x); | ||
} | ||
}) | ||
.reverse() | ||
.reduce((result, x, i) => result + Math.pow(5, i) * x, 0); | ||
} | ||
|
||
export function toSNAFU(x: number): string { | ||
const base5 = x.toString(5).split("").map(Number).reverse(); | ||
let result = ""; | ||
// console.log(result.split("").reverse().join("")); | ||
// console.log([...base5].reverse()); | ||
for (let i = 0; i < base5.length; i++) { | ||
const quota = Math.floor(base5[i] / 3); | ||
const wanted = base5[i]; | ||
const carryOver = Math.ceil(quota / 3); | ||
if (carryOver > 0) { | ||
if (i + 1 < base5.length) { | ||
base5[i + 1] += carryOver; | ||
} else { | ||
base5.push(carryOver); | ||
} | ||
const diff = carryOver * 5 - wanted; | ||
if (diff === 1) { | ||
result += "-"; | ||
} else if (diff === 2) { | ||
result += "="; | ||
} else if (diff === 0) { | ||
result += "0"; | ||
} else { | ||
throw new Error(`go bad diff converting ${x} to SNAFU: ${diff}`); | ||
} | ||
} else { | ||
result += base5[i].toString(); | ||
} | ||
base5[i] = 0; | ||
// console.log(result.split("").reverse().join("")); | ||
// console.log([...base5].reverse()); | ||
} | ||
return result.split("").reverse().join(""); | ||
} | ||
|
||
export function solvePart1(input: Input): string { | ||
return toSNAFU(input.lines.map(toNumber).reduce(sum)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { assertEquals } from "https://deno.land/std/testing/asserts.ts"; | ||
import { Input } from "../../utils/deno/input.ts"; | ||
import { solvePart1, toNumber, toSNAFU } from "./solutions.ts"; | ||
|
||
Deno.test("part 1 - given test case", () => { | ||
const input = `1=-0-2 | ||
12111 | ||
2=0= | ||
21 | ||
2=01 | ||
111 | ||
20012 | ||
112 | ||
1=-1= | ||
1-12 | ||
12 | ||
1= | ||
122`; | ||
assertEquals(solvePart1(new Input(input)), "2=-1=0"); | ||
}); | ||
|
||
Deno.test("part 1 - conversion", () => { | ||
const input = ` | ||
1 1 | ||
2 2 | ||
3 1= | ||
4 1- | ||
5 10 | ||
6 11 | ||
7 12 | ||
8 2= | ||
9 2- | ||
10 20 | ||
15 1=0 | ||
20 1-0 | ||
2022 1=11-2 | ||
12345 1-0---0 | ||
314159265 1121-1110-1=0 | ||
1747 1=-0-2 | ||
906 12111 | ||
198 2=0= | ||
11 21 | ||
201 2=01 | ||
31 111 | ||
1257 20012 | ||
32 112 | ||
353 1=-1= | ||
107 1-12 | ||
7 12 | ||
3 1= | ||
37 122 | ||
246573259444 2=0200-1==-=01=-- | ||
1353001841 11=-=-=21=0-=1 | ||
4890 2=-1=0 | ||
7653 22111= | ||
35422591760336 2-121-=10=200==2==21`; | ||
const numbers = input | ||
.trim() | ||
.split("\n") | ||
.map((x) => { | ||
const parts = x.trim().split(" "); | ||
return [Number(parts[0]), parts[1]] as [number, string]; | ||
}); | ||
for (const [decimal, snafu] of numbers) { | ||
assertEquals(toSNAFU(decimal), snafu, `${decimal} -> ${snafu}`); | ||
assertEquals(toNumber(snafu), decimal, `${snafu} -> ${decimal}`); | ||
} | ||
}); |