Advertisement
Guest User

AoC Day 5 Debugging

a guest
Dec 7th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.38 KB | None | 0 0
  1. #Getting puzzle input
  2. print('Insert your puzzle input:')
  3. inp = input()
  4.  
  5. #Generating lists
  6. data = list(map(int, inp.split(',')))
  7.  
  8.  
  9. def parameters(op, command, counter):
  10.     intr = list(str(command))
  11.  
  12.     parm = ([0]*(5 - len(str(command)))) + intr
  13.  
  14.     val1 = (op[counter + 1] if int(parm[2]) == 0 else (counter + 1))
  15.     val2 = (op[counter + 2] if int(parm[1]) == 0 else (counter + 2))
  16.     val3 = (op[counter + 3] if int(parm[0]) == 0 else (counter + 3))
  17.     return val1, val2, val3
  18.  
  19.  
  20. #Main calc
  21. def run1(d):
  22.     counter = 0
  23.     input = 1
  24.     while counter < len(d):
  25.         intr = d[counter]
  26.         opcode = int(str(intr)[-1])
  27.         val1, val2, val3 = parameters(d, intr, counter)
  28.         if opcode == 1:
  29.             d[val3] = d[val1] + d[val2]
  30.             counter += 4
  31.         elif opcode == 2:
  32.             d[val3] = d[val1] * d[val2]
  33.             counter += 4
  34.         elif opcode == 3:
  35.             d[val1] = input
  36.             counter += 2
  37.         elif opcode == 99 or opcode == 4:
  38.             if d[val1] != 0 and d[counter + 2] == 99:
  39.                 return d[val1]
  40.             elif d[val1] != 0 and d[counter + 2] != 99:
  41.                 return f'Test failed with output {d[val1]}'
  42.                
  43.    
  44. #Running part 1
  45. def gen1(d):
  46.     result = run1(d)
  47.     return result
  48.  
  49. #Running parts
  50. p1 = gen1(data)
  51.  
  52. #Printing results
  53. print(f'Part 1 Result is {p1}')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement