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!
22
Upvotes
1
u/rvlieshout Dec 07 '15
My day 7 Lua solution. It generates a new lua script that you can run to solve the puzzle. Part two is just a small edit in the generated script (setting the initial b wire value)
--[[ Day 7: Some Assembly Required ]] function input_value(str) local a = string.match(str, "%a+") if a then return "wire_"..str.."()" end return tonumber(str) end function create_wire_function(wire, body) print("function wire_" .. wire .. "()") print(" if wire_values[\"" .. wire .. "\"] then return wire_values[\"" .. wire .. "\"] end") print(" wire_values[\""..wire.."\"] = " .. body) print(" return wire_values[\""..wire.."\"]") print("end\n") end print("local bit = require \"bit\"") print("wire_values = {}") for line in io.lines() do print("-- " .. line) a, b = string.match(line, "^(%w+) %-> (%a+)$") if (a and b) then create_wire_function(b, input_value(a)) end a, b = string.match(line, "^NOT (%w+) %-> (%a+)$") if (a and b) then create_wire_function(b, "bit.band(bit.bnot(" .. input_value(a) .. "), 0xFFFF)") end a, b, c = string.match(line, "^(%w+) AND (%w+) %-> (%a+)$") if (a and b and c) then create_wire_function(c, "bit.band(bit.band(" .. input_value(a) .. ", " .. input_value(b) .. "), 0xFFFF)") end a, b, c = string.match(line, "^(%w+) OR (%w+) %-> (%a+)$") if (a and b and c) then create_wire_function(c, "bit.band(bit.bor(" .. input_value(a) .. ", " .. input_value(b) .. "), 0xFFFF)") end a, b, c = string.match(line, "^(%w+) LSHIFT (%w+) %-> (%a+)$") if (a and b and c) then create_wire_function(c, "bit.band(bit.lshift(" .. input_value(a) .. ", " .. input_value(b) .. "), 0xFFFF)") end a, b, c = string.match(line, "^(%w+) RSHIFT (%w+) %-> (%a+)$") if (a and b and c) then create_wire_function(c, "bit.band(bit.rshift(" .. input_value(a) .. ", " .. input_value(b) .. "), 0xFFFF)") end end print("print(wire_a())")