Guest User

AOC - Day 7 Solution

a guest
Dec 7th, 2015
212
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. with open('inputv2.txt') as f:
  2.     inputs = f.readlines()
  3.  
  4. Wires = {}
  5. done = list()
  6.  
  7. i = 0
  8.  
  9. for x in range(0,len(inputs)):
  10.     done.append(False);
  11.  
  12. while i < len(inputs):
  13.     if done[i] == False:
  14.  
  15.         currentTask = inputs[i].replace("\n","")
  16.         splitTask = currentTask.split(" -> ")
  17.         targetId = splitTask[1];
  18.  
  19.         if "AND" in splitTask[0]: #AND OPERATION
  20.             invwires = splitTask[0].split(" AND ")
  21.             if invwires[0] == "1" and invwires[1] in Wires:
  22.                 Wires[targetId] = 1 & Wires[invwires[1]]
  23.                 done[i] = True
  24.             elif invwires[0] in Wires and invwires[1] in Wires:
  25.                 #print "Starting AND OPERATION"
  26.                 Wires[targetId] = Wires[invwires[0]] & Wires[invwires[1]]
  27.                 done[i] = True
  28.         elif "OR" in splitTask[0]: #OR OPERATION
  29.             invwires = splitTask[0].split(" OR ")
  30.             if invwires[0] in Wires and invwires[1] in Wires:
  31.                 #print "Starting OR OPERATION"
  32.                 Wires[targetId] = Wires[invwires[0]] | Wires[invwires[1]]
  33.                 done[i] = True
  34.         elif "LSHIFT" in splitTask[0]:
  35.             lshift = splitTask[0].split(" LSHIFT ")
  36.             if lshift[0] in Wires:
  37.                 Wires[targetId] = Wires[lshift[0]] << int(lshift[1])
  38.                 done[i] = True
  39.         elif "RSHIFT" in splitTask[0]:
  40.             rshift = splitTask[0].split(" RSHIFT ")
  41.             if rshift[0] in Wires:
  42.                 Wires[targetId] = Wires[rshift[0]] >> int(rshift[1])
  43.                 done[i] = True
  44.         elif "NOT" in splitTask[0]:
  45.             notwire = splitTask[0].replace("NOT ", "")
  46.             if notwire in Wires:
  47.                 Wires[targetId] = 65535 - Wires[notwire]
  48.                 done[i] = True
  49.  
  50.         else:
  51.             try:
  52.                 Wires[targetId] = int(splitTask[0])
  53.                 done[i] = True
  54.             except ValueError:
  55.                 if splitTask[0] in Wires:
  56.                     Wires[targetId] = Wires[splitTask[0]]
  57.  
  58.     if "a" in Wires:
  59.         print "FOUND IT!!!! " + str(Wires["a"])
  60.         break
  61.  
  62.     elif i+1 == len(inputs):
  63.         executed = 0
  64.         for x in range(0,len(inputs)):
  65.             if done[x] == True:
  66.                 executed+=1
  67.         print "executed " + str(executed) + " instructions"
  68.         i = 0
  69.  
  70.     else:
  71.         i+=1
RAW Paste Data