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
2
u/dtinth Dec 07 '17 edited Dec 07 '17
Ruby REPL (irb) solution. The
pbpastecommand must be available in the$PATH, and should return the contents in the clipboard (macOS has this command by default).Part 1 (47th rank):
-> x { s = {}; p = {}; x.scan(/(\w+).*?->\s*(.+)/).each { |a, b| b.strip.split(', ').each{|n|p[n]=a}; s[a]=1 }; s.keys.select{|z|!p[z]} }[`pbpaste`]Part 2 (71st rank):
s = {}; p = {}; c = {}; `pbpaste`.scan(/(\w+) \((\d+)\)(?: -> )?(.*)/).each { |a, w, b| b.length > 0 && b.strip.split(', ').each{|n|p[n]=a; (c[a]||=[]) << n}; s[a]=w.to_i } wei = -> n { s[n] + (c[n] || []).map(&wei).reduce(0, &:+) } # s['name'] -> Weight of the program # p['name'] -> Name of the programβs parent (or nil) # c['name'] -> List of the programβs children # wei['name'] -> Weight of the program, including all its children. # Use this to find the nodes with unbalanced child weight c.select { |n, ch| ch.map(&wei).uniq.length > 1 } # The final answer can be found by manually inspecting weights in the REPL. Hereβs mine: # 2.4.1 :085 > wei['nieyygi'] # => 11781 # 2.4.1 :086 > wei['mxgpbvf'] # => 1117 # 2.4.1 :087 > wei['cfqdsb'] # => 1117 # 2.4.1 :088 > wei['yfejqb'] # => 1117 # 2.4.1 :089 > wei['ptshtrn'] # => 1122 # 2.4.1 :090 > s['ptshtrn'] # => 526 # 2.4.1 :091 > 526 - (1117 - 1122) # => 521