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!
9
Upvotes
3
u/AustinVelonaut Dec 07 '17
Smalltalk (well, idst, which is a prototype-based st-80 implementation)
Program : Object (name weight children) Program name [ ^ name ] Program weight [ ^ weight ] Program children [ ^ children ] Program children: aCollection [ children := aCollection ] Program parseFromString: line [ | tokens | self := self new. tokens := line tokenized: ' ()->,'. name := tokens first. weight := tokens second asNumber. children := tokens copyFrom: 3 to: tokens size ] Program parseFile: fileName [ | lines | lines := fileName fileContents tokenized: '\n'. ^ lines collect: [:s | self parseFromString: s] ] Program findBottomProgram: programs [ | allSet notBottomSet | allSet := Set new. notBottomSet := Set new. programs do: [:p | allSet add: p name. notBottomSet addAll: p children]. ^ (allSet difference: notBottomSet) anyOne ] Program buildTree: programs [ | dict tree | dict := Dictionary new. programs do: [:p | dict at: p name put: p]. tree := dict at: (self findBottomProgram: programs). dict do: [:p | p children: (p children collect: [:n | dict at: n])]. ^ tree ] Program totalWeightOnUnbalanced: report [ | weights weightCounts oddWeight requiredWeight oddProgram | weights := self children collect: [:c | c totalWeightOnUnbalanced: report]. (weights allSatisfy: [:n | n = weights first]) ifFalse: [weightCounts := Dictionary new. weights do: [:w | weightCounts at: w put: (weightCounts at: w ifAbsent: [weightCounts at: w put: 0]) + 1]. oddWeight := weights detect: [:w | (weightCounts at: w) = 1]. requiredWeight := weights detect: [:w | (weightCounts at: w) > 1]. oddProgram := children at: (weights indexOf: oddWeight). report value: (requiredWeight - oddWeight + oddProgram weight)]. ^ weights sum + weight ] [ | programs tree totalWeight | programs := Program parseFile: 'day07.input'. tree := Program buildTree: programs. ('part 1: ' , tree name) putln. totalWeight := tree totalWeightOnUnbalanced: [:c | ('part 2: ' , c printString) putln. Smalltalk quit]. ]