This commit is contained in:
eleith 2023-01-14 00:23:27 -08:00
parent 0955109a95
commit 8ff78c25c2
4 changed files with 2202 additions and 0 deletions

58
21/README.md Normal file
View File

@ -0,0 +1,58 @@
--- Day 21: Monkey Math ---
The monkeys are back! You're worried they're going to try to steal your stuff again, but it seems like they're just holding their ground and making various monkey noises at you.
Eventually, one of the elephants realizes you don't speak monkey and comes over to interpret. As it turns out, they overheard you talking about trying to find the grove; they can show you a shortcut if you answer their riddle.
Each monkey is given a job: either to yell a specific number or to yell the result of a math operation. All of the number-yelling monkeys know their number from the start; however, the math operation monkeys need to wait for two other monkeys to yell a number, and those two other monkeys might also be waiting on other monkeys.
Your job is to work out the number the monkey named root will yell before the monkeys figure it out themselves.
For example:
root: pppw + sjmn
dbpl: 5
cczh: sllz + lgvd
zczc: 2
ptdq: humn - dvpt
dvpt: 3
lfqf: 4
humn: 5
ljgn: 2
sjmn: drzm _ dbpl
sllz: 4
pppw: cczh / lfqf
lgvd: ljgn _ ptdq
drzm: hmdt - zczc
hmdt: 32
Each line contains the name of a monkey, a colon, and then the job of that monkey:
A lone number means the monkey's job is simply to yell that number.
A job like aaaa + bbbb means the monkey waits for monkeys aaaa and bbbb to yell each of their numbers; the monkey then yells the sum of those two numbers.
aaaa - bbbb means the monkey yells aaaa's number minus bbbb's number.
Job aaaa * bbbb will yell aaaa's number multiplied by bbbb's number.
Job aaaa / bbbb will yell aaaa's number divided by bbbb's number.
So, in the above example, monkey drzm has to wait for monkeys hmdt and zczc to yell their numbers. Fortunately, both hmdt and zczc have jobs that involve simply yelling a single number, so they do this immediately: 32 and 2. Monkey drzm can then yell its number by finding 32 minus 2: 30.
Then, monkey sjmn has one of its numbers (30, from monkey drzm), and already has its other number, 5, from dbpl. This allows it to yell its own number by finding 30 multiplied by 5: 150.
This process continues until root yells a number: 152.
However, your actual situation involves considerably more monkeys. What number will the monkey named root yell?
Your puzzle answer was 282285213953670.
--- Part Two ---
Due to some kind of monkey-elephant-human mistranslation, you seem to have misunderstood a few key details about the riddle.
First, you got the wrong job for the monkey named root; specifically, you got the wrong math operation. The correct operation for monkey root should be =, which means that it still listens for two numbers (from the same two monkeys as before), but now checks that the two numbers match.
Second, you got the wrong monkey for the job starting with humn:. It isn't a monkey - it's you. Actually, you got the job wrong, too: you need to figure out what number you need to yell so that root's equality check passes. (The number that appears after humn: in your input is now irrelevant.)
In the above example, the number you need to yell to pass root's equality test is 301. (This causes root to get the same number, 150, from both of its monkeys.)
What number do you yell to pass root's equality test?
Your puzzle answer was 3699945358564.

1917
21/input.txt Normal file

File diff suppressed because it is too large Load Diff

15
21/sample-input.txt Normal file
View File

@ -0,0 +1,15 @@
root: pppw + sjmn
dbpl: 5
cczh: sllz + lgvd
zczc: 2
ptdq: humn - dvpt
dvpt: 3
lfqf: 4
humn: 5
ljgn: 2
sjmn: drzm * dbpl
sllz: 4
pppw: cczh / lfqf
lgvd: ljgn * ptdq
drzm: hmdt - zczc
hmdt: 32

212
21/src/index.ts Normal file
View File

@ -0,0 +1,212 @@
import { readFileSync } from 'fs'
type Operands = '-' | '+' | '*' | '/'
type MonkeyJobNumber = {
type: 'number'
value: number
}
type MonkeyJobVariable = {
type: 'variable'
value: string
}
type MonkeyJobMath = {
left: string
right: string
type: 'math'
operand: Operands
}
type MonkeyJob = MonkeyJobMath | MonkeyJobNumber | MonkeyJobVariable
type MonkeyJobs = {
[monkeyName: string]: MonkeyJob
}
type MonkeyEquation =
| [number, Operands, string | MonkeyEquation]
| [string | MonkeyEquation, Operands, number]
const jobMatch = /^(.+): (.+)$/
const jobTypeNumberMatch = /^\d+$/
const jobTypeMathMatch = /^(.+) ([-+*/]) (.+)$/
function parseJobs(lines: string[]): MonkeyJobs {
return lines.reduce((jobs, line) => {
const matchJob = line.match(jobMatch)
if (matchJob) {
const [, monkeyName, job] = matchJob
if (jobTypeNumberMatch.test(job)) {
jobs[monkeyName] = {
value: parseInt(job, 10),
type: 'number',
}
} else if (jobTypeMathMatch.test(job)) {
const jobMath = job.match(jobTypeMathMatch)
if (jobMath) {
const [, leftMonkeyName, operand, rightMonkeyName] = jobMath
jobs[monkeyName] = {
left: leftMonkeyName,
operand,
right: rightMonkeyName,
type: 'math',
} as MonkeyJob
} else {
throw new Error(`Invalid math job: ${job}`)
}
}
} else {
throw new Error(`Invalid job: ${line}`)
}
return jobs
}, {} as MonkeyJobs)
}
function solveJobFor(monkey: string, monkeyJobs: MonkeyJobs): number {
const monkeyJob = monkeyJobs[monkey]
if (monkeyJob) {
if (monkeyJob.type === 'number') {
return monkeyJob.value
} else if (monkeyJob.type === 'math') {
const leftValue = solveJobFor(monkeyJob.left, monkeyJobs)
const rightValue = solveJobFor(monkeyJob.right, monkeyJobs)
return math(leftValue, monkeyJob.operand, rightValue)
}
}
throw new Error(`no job for monkey: ${monkey}`)
}
function math(left: number, operand: Operands, right: number): number {
switch (operand) {
case '+':
return left + right
case '-':
return left - right
case '*':
return left * right
case '/':
return left / right
default:
throw new Error(`invalid operand: ${operand}`)
}
}
function getAlgorithmFor(
monkeyJob: MonkeyJob,
monkeyJobs: MonkeyJobs
): string | number | MonkeyEquation {
if (monkeyJob.type !== 'math') {
return monkeyJob.value
} else if (monkeyJob.type === 'math') {
const operand = monkeyJob.operand
const leftMonkey = monkeyJobs[monkeyJob.left]
const rightMonkey = monkeyJobs[monkeyJob.right]
const left = getAlgorithmFor(leftMonkey, monkeyJobs)
const right = getAlgorithmFor(rightMonkey, monkeyJobs)
if (typeof left === 'number') {
if (typeof right === 'number') {
return math(left, operand, right)
}
return [left, operand, right]
} else if (typeof right === 'number') {
return [left, operand, right]
}
}
throw new Error(`too many dependencies for: ${monkeyJob}`)
}
function solveForX(equation: MonkeyEquation, x: number): number {
const left = equation[0]
const operand = equation[1]
const right = equation[2]
let solve = 1
// left + right = x -> right = x - left -> left = x - right
// left - right = x -> right = left - x -> left = x + right
// left * right = x -> right = x / left -> left = x / right
// left / right = x -> right = left / x -> left = x * right
if (typeof left === 'number') {
switch (operand) {
case '+':
solve = x - left
break
case '-':
solve = left - x
break
case '*':
solve = x / left
break
default:
solve = left / x
}
} else if (typeof right === 'number') {
switch (operand) {
case '+':
solve = x - right
break
case '-':
solve = x + right
break
case '*':
solve = x / right
break
default:
solve = x * right
}
}
if (Array.isArray(right)) {
return solveForX(right, solve)
} else if (Array.isArray(left)) {
return solveForX(left, solve)
}
return solve
}
function solvePart1(lines: string[]): number {
const monkeyJobs = parseJobs(lines)
return solveJobFor('root', monkeyJobs)
}
function solvePart2(lines: string[]): number {
const monkeyJobs = parseJobs(lines)
const root = monkeyJobs['root']
monkeyJobs['humn'] = { type: 'variable', value: 'x' }
if (root.type === 'math') {
const rootAlgo = getAlgorithmFor({ ...root, operand: '-' }, monkeyJobs)
if (Array.isArray(rootAlgo)) {
return solveForX(rootAlgo, 0)
}
}
throw new Error('algo failed to be solved')
}
function run() {
const input = process.argv.slice(2)[0]
const file = readFileSync(input, 'utf8')
const lines = file.split('\n').slice(0, -1)
const solution1 = solvePart1(lines)
console.log(`part1: ${solution1}`)
const solution2 = solvePart2(lines)
console.log(`part2: ${solution2}`)
}
run()