snafu snacks

This commit is contained in:
eleith 2023-02-17 13:32:30 -08:00
parent edf0e31f3c
commit 77dc0eaf3b
4 changed files with 371 additions and 0 deletions

116
25/README.md Normal file
View File

@ -0,0 +1,116 @@
--- Day 25: Full of Hot Air ---
As the expedition finally reaches the extraction point, several large hot air balloons 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 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?
Your puzzle answer was 2=112--220-=-00=-=20.
--- Part Two ---
The hot air balloons quickly carry you to the North Pole. As soon as you land, most of the expedition is escorted directly to a small building attached to the reindeer stables.
The head smoothie chef has just finished warming up the industrial-grade smoothie blender as you arrive. It will take 50 stars to fill the blender. The expedition Elves turn their attention to you, and you begin emptying the fruit from your pack onto the table.
As you do, a very young Elf - one you recognize from the expedition team - approaches the table and holds up a single star fruit he found. The head smoothie chef places it in the blender.
Only 49 stars to go.
If you like, you can
.
Both parts of this puzzle are complete! They provide two gold stars: \*\*
At this point, all that is left is for you to admire your Advent calendar.

120
25/input.txt Normal file
View File

@ -0,0 +1,120 @@
21-2-0=1-
2-11220-==01==2
1-2200=1=0--
1-10--22=-=-2
21120
1=0==
10=022
222--0=10202-0=2
1=01==1
1021
20--221-222000
12-1=02--0221=-1
11-=-1===2-
1-101-=
11-21100-===0=-
1-
2-=
1=0=-0
10011001=0
2--
2120=202121
1=0
10==-
22=02102=2-
11=0-
1==0-1-1=0
21===-=-0
11220-=2-=20=201
2=001
1=20--=-
1===00
12-22012012-21=
2=--=20-111=-
221
12=0-=-=
2---102
1=1
202011021=
21=0-0=--1102-1-
1=--2102220-2==221=
1=10021-=21
11=2=202120021-=11
1-1012=122=
12-01=0=1=0-11=0--
100-20=1=-
110=0-
1=-102
2-221=2--010-01
12-01220
10--10====1-20=-00
110
1=00==22
212-1-222010=2==2=1
1=011=1=0
1-0
1-11221=000-=200221
221-00=2=-1-
122=1=2
101
1==2=11=-10
220-1--=21=2-02==
21--02--0-==1
1==0=11012
122
1000=20=0012
22-=0-011110=
2=21-1-20212--=
1-=202010
1-0=-===21-100
1-22--21
1-12011122=02=-21
1-01-2=1000-01=21-
12-1-1=1=-=01=-1=
202
1==-1-00
1201--=-=-
1=-1--==1122=2=12
201020
1=-11-0111-00-2
10==1==-1=2
120=1002-00122
1=12
2122110
1112-10=12==
1=02--0-=
2-0021-==112=10=
2212-022-==10==
1001200-1-=
2=000-=00--11
1==0122120
1=1==-=2210-
1-02===012
12-21-11-1-10
2=10==-=11=-
22---2=2012-22100
1=-0=--
1=20--2=1212===12=
1=
2-
1-=2=2-00211-2=0
1=222---02=00211-
112==10
2-0=101-10=02
11212101-
10-=
1=01=2=011=--0
21=0-10==22-201
1-20=-1=121-
120=
1-=01=-1-
11--1-11=0=221
1-=120==
20--002=
12--22
200-=
1=1--21=111=--=011-1
12011=0=--11
1-2
121==1
10220-==-

13
25/sample-input.txt Normal file
View File

@ -0,0 +1,13 @@
1=-0-2
12111
2=0=
21
2=01
111
20012
112
1=-1=
1-12
12
1=
122

122
25/src/index.ts Normal file
View File

@ -0,0 +1,122 @@
import { readFileSync } from 'fs'
const SnafuMap = {
'1': 1,
'2': 2,
'=': -2,
'-': -1,
'0': 0,
} as const
const SnafuBase = 5
type Snafus = keyof typeof SnafuMap
type SnafuNumbers = typeof SnafuMap[Snafus]
const snafuMapKeys = Object.keys(SnafuMap) as Snafus[]
const snafusOrdered = snafuMapKeys.sort((a, b) => SnafuMap[a] - SnafuMap[b])
function parseInput(lines: string[]): Snafus[][] {
return lines.map((line) => {
const strings = line.split('') as Snafus[]
if (strings.every((s) => s in SnafuMap)) {
return strings
}
throw new Error(`Invalid snafu string: ${line}`)
})
}
function getSnafuFromNumber(snafuNumber: SnafuNumbers): Snafus {
const entry = Object.entries(SnafuMap).find(([, value]) => snafuNumber === value)
if (entry) {
return entry[0] as Snafus
}
throw new Error(`Invalid snafu lookup for ${snafuNumber}`)
}
function snafuPower(place: number, snafu: Snafus): number {
return Math.pow(SnafuBase, place) * SnafuMap[snafu]
}
function snafuRange(places: number): number {
let total = 0
for (let i = 0; i < places; i++) {
total += snafuPower(i, getSnafuFromNumber(2))
}
return total
}
function getSnafuStringAt(input: number, snafus: Snafus[], at: number): Snafus[] {
const currentValue = snafuStringsToDecimal(snafus)
const copySnafus = [...snafus]
if (input - currentValue !== 0) {
const snafu = snafusOrdered.find((findSnafu) => {
const testSnafus = [...snafus]
testSnafus[at] = findSnafu
const testDecimal = snafuStringsToDecimal(testSnafus)
const testHighRange = snafuRange(snafus.length - at - 1)
return testDecimal + testHighRange >= input && testDecimal - testHighRange <= input
})
if (snafu === undefined) {
return getSnafuStringAt(input, [...copySnafus, getSnafuFromNumber(0)], at)
} else {
copySnafus[at] = snafu
}
if (at === snafus.length - 1) {
return copySnafus
} else {
return getSnafuStringAt(input, copySnafus, at + 1)
}
}
while (at < snafus.length) {
copySnafus[at] = getSnafuFromNumber(0)
at += 1
}
return snafus
}
function decimalToSnafuStrings(input: number): Snafus[] {
return getSnafuStringAt(input, [] as Snafus[], 0)
}
function snafuStringsToDecimal(digits: Snafus[]): number {
return [...digits]
.map((c) => SnafuMap[c])
.reverse()
.reduce((total, digit, i) => total + snafuPower(i, getSnafuFromNumber(digit)), 0 as number)
}
function solvePart1(lines: string[], debug = false): string {
const decimals = parseInput(lines).map(snafuStringsToDecimal)
const total = decimals.reduce((total, decimal) => total + decimal, 0)
const snafuStrings = decimalToSnafuStrings(total)
if (debug && total !== snafuStringsToDecimal(snafuStrings)) {
throw new Error('snafu')
}
return snafuStrings.join('')
}
async function run() {
const input = process.argv.slice(2)[0]
const debug = process.argv.slice(2)[1] === '--debug'
const file = readFileSync(input, 'utf8')
const lines = file.split('\n').slice(0, -1)
const solution = solvePart1(lines, debug)
console.log(`solution: ${solution}`)
}
run()