Advertisement
Guest User

AoC Day 8 Python 3

a guest
Dec 8th, 2020
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.24 KB | None | 0 0
  1. # day8
  2. import re
  3.  
  4. input = "d8_data.txt"
  5. test = "d8_test.txt"
  6.  
  7. def read_file(filename):
  8.     with open(filename) as f:
  9.         return f.read().splitlines()
  10.  
  11.  
  12. data = read_file(input)
  13.  
  14. # Each Operation gets stored in a dict, which are stored in an array
  15. def get_instructions(data):
  16.     trouble = []
  17.     for line in data:
  18.         instructions = re.match(r"(.+) (.+)$", line)
  19.         if instructions:
  20.             operation = instructions.group(1)
  21.             parameter = instructions.group(2)
  22.             trouble.append({'Operation':operation,'Parameter':int(parameter),'Acc before':0,'Acc after':0,'Visited':False})
  23.     return trouble
  24.  
  25. # Running through the troubled data
  26. def find_loop(trouble,index):
  27.     i=0
  28.     prev=0
  29.     bfr=0
  30.     order = []
  31.  
  32.     # updating the current operation set
  33.     def update_item(jmp,i,bfr,par):
  34.         trouble[i]['Visited'] = True
  35.         trouble[i]['Acc before'] = bfr
  36.         trouble[i]['Acc after'] = bfr
  37.         if not jmp:
  38.             trouble[i]['Acc after'] = par + bfr
  39.    
  40.     while (True):
  41.         if (i>=len(trouble)):
  42.             break
  43.         opr = trouble[i].get('Operation')
  44.         par = trouble[i].get('Parameter')
  45.         bfr = trouble[prev].get('Acc after')
  46.         prev = i
  47.         #order.append(str(i)+' | '+str(bfr)+' | '+opr+' '+str(par))
  48.  
  49.         # stop on seen item        
  50.         if (trouble[i].get('Visited')):
  51.             #print('Order:')
  52.             #for calc in order: print(calc)
  53.             return str(bfr)
  54.  
  55.         if (opr=='nop'):
  56.             update_item(False,i,bfr,0)
  57.             i += 1
  58.             continue
  59.  
  60.         if (opr=='acc'):
  61.             update_item(False,i,bfr,par)
  62.             bfr += par
  63.             i += 1
  64.             continue
  65.        
  66.         if (opr=='jmp'):
  67.             update_item(True,i,bfr,par)
  68.             i = i+par
  69.             continue
  70.  
  71.     return "Real answer: "+str(bfr)+" Index:"+str(index)
  72.    
  73.  
  74. # find the operation to change
  75. def repair_trouble(trb):
  76.     trouble = trb
  77.    
  78.     # toggle encounterd jmp and nop
  79.     def toggle(operation):
  80.         opr = operation.get('Operation')
  81.         if opr == 'jmp':
  82.             operation['Operation']='nop'
  83.             return operation
  84.         elif opr == 'nop':
  85.             operation['Operation']='jmp'
  86.             return operation
  87.  
  88.     #check each operation and toggle if needed. Then get result.
  89.     for operation in trouble:
  90.         if not (operation.get('Operation')=='acc'):
  91.             operation = toggle(operation)
  92.             res = (find_loop(trouble,trouble.index(operation)))
  93.             if re.match(r"(.+) (.+)$",res):
  94.                 print(res)
  95.                 break
  96.             operation = toggle(operation)
  97.             #print(troubled_data)
  98.         else:
  99.             res = (find_loop(trouble,trouble.index(operation)))
  100.             #print(troubled_data)
  101.             if re.match(r"(.+) (.+)$",res):
  102.                 print(res)
  103.                 break
  104.         #needed to reset to initial state
  105.         for visit in trouble:
  106.             visit['Visited']=False
  107.             visit['Acc after']=0
  108.             visit['Acc before']=0
  109.        
  110.  
  111.  
  112. troubled_data = get_instructions(data)
  113. #print(troubled_data)
  114. print(repair_trouble(troubled_data))
  115.  
  116. #print(find_loop(troubled_data,0))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement