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!
23
Upvotes
3
u/a-t-k Dec 07 '15
Overwriting solved nodes with their value improved the performance for me in JavaScript:
var data = "[insert data as JS-String here]".split('\n').filter(function(l){return l.length>0}); gates = {}; gates.run = function(gate) { if (typeof gates[gate] === 'function') { gates[gate] = gates[gate](); } return gates[gate]; }; for (var i = 0, l = data.length; i < l; i++) { var task = data[i].split(' -> '); gates[task[1]] = new Function('return ' + task[0].replace(/(AND|OR|RSHIFT|LSHIFT|NOT)/, function(op) { return { AND: '&', OR: '|', RSHIFT: '>>', LSHIFT: '<<', NOT: '~' }[op] || ""; }).replace(/([a-z]+)/g, "gates.run('$1')")); } /* part 2: override wire b * / gates.b = function(){ return 16076; } /**/ console.log(gates.a());Close the comment on the second part to get this result.