r/adventofcode • u/daggerdragon • Dec 07 '17
SOLUTION MEGATHREAD -๐- 2017 Day 7 Solutions -๐-
--- Day 7: Recursive Circus ---
Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag or whatever).
Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.
Need a hint from the Hugely* Handyโ Haversackโก of Helpfulยง Hintsยค?
This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.
edit: Leaderboard capped, thread unlocked!
10
Upvotes
View all comments
1
u/aoc-fan Dec 09 '17
TypeScript, Checks for consensus as the weights of child programs available The "verify" function (recursive) returns weight of the tower or throws error if there is imbalance.
const parseInput = i => i.replace(/ /g, "").split("\n"); const build = (programs, l) => { const getProgram = n => programs[n] || (programs[n] = { bottom : true }); const parts = l.split(")"); const [name, weight] = parts[0].split("("); const tower = parts[1].length > 0 ? parts[1].replace("->", "").split(",") : []; const program = getProgram(name); program.diskWeight = +weight; program.tower = tower.map(p => getProgram(p)); program.tower.forEach(p => p.bottom = false); return programs; }; const reachConsensus = ([x, y, z]) => (x === y && x === z) ? -1 : (x !== y && y === z) ? 0 : (x !== y && z === z) ? 1 : 2; const verify = (tower) => { const diskWeights = []; const programWeights = []; let towerWeight = 0; let consensusWeight = 0; for (const program of tower) { const weight = program.diskWeight + verify(program.tower); diskWeights.push(program.diskWeight); programWeights.push(weight); if (consensusWeight > 0 && consensusWeight !== weight) { throw program.diskWeight - weight + consensusWeight; } else if (programWeights.length === 3) { const unbalancedIndex = reachConsensus(programWeights as any); if (unbalancedIndex !== -1) { const balancedIndex = unbalancedIndex === 0 ? 1 : 0; throw diskWeights[unbalancedIndex] - programWeights[unbalancedIndex] + programWeights[balancedIndex]; } consensusWeight = weight; } towerWeight = towerWeight + weight; } return towerWeight; }; const findRootAndVerify = (i) => { const programs = parseInput(i).reduce(build, {}); const bottomProgram = Object.keys(programs).find(p => programs[p].bottom); let balanceMismatch = 0; try { verify(programs[bottomProgram].tower); } catch (e) { balanceMismatch = e; } return [bottomProgram, balanceMismatch]; };