Advent of Code 2022 - Day 21: Monkey Math Solution
Complex numberPart 1
Straightforward recursion to calculate value
Parsed data
1{2 root: [ 'pppw', '+', 'sjmn' ],3 dbpl: 5,4 cczh: [ 'sllz', '+', 'lgvd' ],5 zczc: 2,6 ptdq: [ 'humn', '-', 'dvpt' ],7 dvpt: 3,8 lfqf: 4,9 humn: 5,10 ljgn: 2,11 sjmn: [ 'drzm', '*', 'dbpl' ],12 sllz: 4,13 pppw: [ 'cczh', '/', 'lfqf' ],14 lgvd: [ 'ljgn', '*', 'ptdq' ],15 drzm: [ 'hmdt', '-', 'zczc' ],16 hmdt: 3217}
Implementation
1const fs = require("fs")23const getFuncFromOperator = operator =>4 ({5 "+": (a, b) => a + b,6 "-": (a, b) => a - b,7 "*": (a, b) => a * b,8 "/": (a, b) => a / b,9 }[operator])1011const readData = () => {12 const data = fs13 .readFileSync("./input", "utf-8")14 .split(/\r?\n/)15 .filter(Boolean)16 .map(line => line.split(": "))17 .map(([key, value]) => [18 key,19 Number.isNaN(Number(value)) ? value.split(" ") : Number(value),20 ])2122 return Object.fromEntries(data)23}2425const main = () => {26 const data = readData()2728 const recursion = name => {29 const value = data[name]3031 if (Number.isInteger(value)) return value3233 const [operand1, operator, operand2] = value34 const func = getFuncFromOperator(operator)3536 return func(recursion(operand1), recursion(operand2))37 }3839 const res = recursion("root")4041 console.log(res)42}4344main()
Part 2
Convert to complex number, unknown humn
could be store in imaginary part
Parsed data
1{2 root: [ 'pppw', '+', 'sjmn' ],3 dbpl: { real: 5, imag: 0 },4 cczh: [ 'sllz', '+', 'lgvd' ],5 zczc: { real: 2, imag: 0 },6 ptdq: [ 'humn', '-', 'dvpt' ],7 dvpt: { real: 3, imag: 0 },8 lfqf: { real: 4, imag: 0 },9 humn: { real: 0, imag: 1 },10 ljgn: { real: 2, imag: 0 },11 sjmn: [ 'drzm', '*', 'dbpl' ],12 sllz: { real: 4, imag: 0 },13 pppw: [ 'cczh', '/', 'lfqf' ],14 lgvd: [ 'ljgn', '*', 'ptdq' ],15 drzm: [ 'hmdt', '-', 'zczc' ],16 hmdt: { real: 32, imag: 0 }17}
Recursively calculate lefh-hand side (LHS), right-hand side (RHS) of root
and we will get 2 complex numbers
Calculate imaginary from
1LHS = RHS
to get humn
value
Operations for complex number could be refered from Wikipedia or any mathemetic blogs
Implementation
1const fs = require("fs")23const getFuncFromOperator = operator =>4 ({5 "+": ({ real: x, imag: y }, { real: u, imag: v }) => ({6 real: x + u,7 imag: y + v,8 }),9 "-": ({ real: x, imag: y }, { real: u, imag: v }) => ({10 real: x - u,11 imag: y - v,12 }),13 "*": ({ real: x, imag: y }, { real: u, imag: v }) => ({14 real: x * u - y * v,15 imag: x * v + y * u,16 }),17 "/": ({ real: x, imag: y }, { real: u, imag: v }) => ({18 real: (x * u + y * v) / (u ** 2 + v ** 2),19 imag: (y * u - x * v) / (u ** 2 + v ** 2),20 }),21 }[operator])2223const readData = () => {24 const data = fs25 .readFileSync("./input", "utf-8")26 .split(/\r?\n/)27 .filter(Boolean)28 .map(line => line.split(": "))29 .map(([key, value]) => [30 key,31 Number.isNaN(Number(value))32 ? value.split(" ")33 : { real: Number(value), imag: 0 },34 ])3536 return Object.fromEntries(data)37}3839const main = () => {40 const data = readData()4142 const recursion = name => {43 const value = data[name]4445 if (!Array.isArray(value)) return value4647 const func = getFuncFromOperator(value[1])4849 value[0] = recursion(value[0])50 value[2] = recursion(value[2])5152 return func(value[0], value[2])53 }5455 data["humn"] = { real: 0, imag: 1 }56 recursion("root")5758 let [lhs, _, rhs] = data["root"]5960 if (rhs.imag) {61 ;[lhs, rhs] = [rhs, lhs]62 }6364 const res = Math.floor((rhs.real - lhs.real) / lhs.imag)6566 console.log(res)67}6869main()
References
Comments
Loading comments...
Tags
adventofcode
recursion
math
Apply and earn a $2,500 bonus once you're hired on your first job!
Clients from the Fortune 500 to Silicon Valley startups
Choose your own rate, get paid on time
From hourly, part-time, to full-time positions
Flexible remote working environment
A lot of open JavaScript jobs!!
Fact corner: Referred talent are 5x more likely to pass the Toptal screening process than the average applicant.
Still hesitate? Read HoningJS author's guide on dealing with Toptal interview process.
Next Post
Read the problem description carefully, the rest is straigthforward implementation
Previous Post
Floyd-Warshal and bitmask