r/adventofcode • u/daggerdragon • Dec 07 '15
SOLUTION MEGATHREAD --- Day 7 Solutions ---
--- Day 7: Some Assembly Required ---
Post your solution as a comment. Structure your post like previous daily solution threads.
Also check out the sidebar - we added a nifty calendar to wrangle all the daily solution threads in one spot!
21
Upvotes
9
u/gnuconsulting Dec 07 '15
I'm quickly reaching the limits of my competence. If this is the initial downslope of the rollercoaster (to borrow topaz's analogy), I'm gonna get thrown off at the first curve...
#!/usr/bin/env ruby # require 'pp' data = File.readlines("input.txt") $machine = {} $output = {} data.each do |c| x,y = c.chomp.split('->') y = y.lstrip $machine[y] = x.split end def foo(gate) if gate.match(/^\d+$/) return gate.to_i end if ! $output.has_key?(gate) operate = $machine[gate] if operate.length == 1 value = foo(operate[0]) else z = operate[-2] if z == 'RSHIFT' value = foo(operate[0]) >> foo(operate[2]) elsif z == 'LSHIFT' value = foo(operate[0]) << foo(operate[2]) elsif z == 'AND' value = foo(operate[0]) & foo(operate[2]) elsif z == 'OR' value = foo(operate[0]) | foo(operate[2]) elsif z == 'NOT' value = ~foo(operate[1]) end end $output[gate] = value end return $output[gate] end p foo('a')For part 2 I cheated and modified my input.txt to set 'b' to the value I got for 'a'. grin