SHARE
TWEET

Day 5 - Part 1

a guest Dec 5th, 2019 99 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. require "importutils"
  2.  
  3. local input_p = getInput(5):split(",", true)
  4.  
  5. local dict =
  6. {
  7.     id_1    = {fn = "add",      params = 3},
  8.     id_2    = {fn = "mult",     params = 3},
  9.     id_3    = {fn = "in",       params = 1},
  10.     id_4    = {fn = "out",      params = 1},
  11.     id_99   = {fn = "halt",     params = 0},
  12. }
  13.  
  14. local seed = 1 -- starting input
  15. local output = 0
  16.  
  17. local function operate(input, id, params)
  18.     local fn = dict[id].fn
  19.     assert(fn == "halt" or (params and #params > 0), "No parameters passed for operation : "..fn)
  20.     if fn == "out" then -- edge case
  21.         if params[1].mode == 0 then params[1].val = input[params[1].val+1] end
  22.     else
  23.         for i=1, #params-1 do -- skip last since we want to keep the write address
  24.             if params[i].mode == 0 then params[i].val = input[params[i].val+1] end
  25.         end
  26.     end
  27.     if     fn == "add"  then return params[1].val + params[2].val, params[3].val
  28.     elseif fn == "mult" then return params[1].val * params[2].val, params[3].val
  29.     elseif fn == "in"   then return seed, params[1].val
  30.     elseif fn == "out"  then return params[1].val, "out"
  31.     elseif fn == "halt" then return "halt" end
  32. end
  33.  
  34. local p = 1 -- input address pointer
  35. function runIntcodeComp(input)
  36.     assert(type(input) == "table", "Input is not a table. Must pass a parsed table as input.")
  37.     while p < #input do
  38.         local opcode, parse = input[p]
  39.         local id = string.format("id_%d", math.floor(string.sub(opcode, -2))) --shaves off leading 0 if exists
  40.         local params = {}
  41.         if #tostring(opcode) > 2 then
  42.             parse = tostring(opcode):splitByChar()
  43.         end
  44.         assert(dict[id],  "Invalid operation : "..id.." @ entry "..p)
  45.         for i=1, dict[id].params do
  46.             local t = {val=input[p+i], mode=0}
  47.             if parse and parse[#parse-(i+1)] == "1" then t.mode = 1 end
  48.             params[#params+1] = t
  49.         end
  50.         local result, pos = operate(input, id, params)
  51.         if result == "halt" then
  52.             print("Halt function has been pushed.")
  53.             break
  54.         end
  55.         if pos == "out" then
  56.             output = result
  57.             print("Output is set to : "..output)
  58.         else input[pos+1] = result end
  59.         p = p + #params + 1
  60.     end
  61. end
  62.  
  63. runIntcodeComp(input_p)
  64. print("\nOutput is : "..output)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top