Advertisement
Guest User

Untitled

a guest
Dec 7th, 2019
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.01 KB | None | 0 0
  1. # 99 : Termination, 0 parameter
  2. # 1 : Addition, 3 parameter
  3. # 2 : Multiplication, 3 parameter
  4. # 3 : Input, 1 parameter
  5. # 4 : Output, 1 parameter
  6. # 5 : Jump-if-true, 2 parameter
  7. # 6 : Jump-if-false, 2 parameter
  8. # 7 : less-than, 3 parameter
  9. # 8 : equals, 3 parameter
  10.  
  11. with open("AoC Day 5.txt", "r") as f:
  12. contents = [int(c) for c in f.read().split(',')]
  13. f.close()
  14.  
  15. def intcode(vals):
  16. contents = vals.copy()
  17.  
  18. x = 0
  19. while contents[x] != 99:
  20. instruction = str(contents[x])
  21.  
  22. opcode = '0'*(5-len(str(contents[x]))) + str(contents[x])
  23. A, B, C, DE = int(opcode[0]), int(opcode[1]), int(opcode[2]), opcode[-2:]
  24.  
  25. if DE in ('01', '02', '07', '08'):
  26. A = contents[x+3]#contents[contents[x+3]] if A==0 else contents[x+3]
  27. B = contents[contents[x+2]] if B==0 else contents[x+2]
  28. if DE in ('05', '06'):
  29. B = contents[contents[x+2]] if B==0 else contents[x+2]
  30.  
  31. C = contents[contents[x+1]] if C==0 else contents[x+1]
  32.  
  33. if DE in '01':
  34. contents[A] = B + C
  35. step_forward = 4
  36.  
  37. if DE in '02':
  38. contents[A] = B * C
  39. step_forward = 4
  40.  
  41. if DE == '03':
  42. contents[C] = int(input('input: '))
  43. step_forward = 2
  44.  
  45. if DE == '04':
  46. print(C)
  47. step_forward = 2
  48.  
  49. if DE == '05':
  50. if C != 0:
  51. x = contents[B]
  52. step_forward = 0
  53. else:
  54. step_forward = 3
  55.  
  56. if DE == '06':
  57. if C == 0:
  58. x = contents[B]
  59. step_forward = 0
  60. else:
  61. step_forward = 3
  62.  
  63. if DE == '07':
  64. contents[A] = C < B
  65. step_forward = 4
  66.  
  67. if DE == '08':
  68. contents[A] = C == B
  69. step_forward = 4
  70.  
  71. x += step_forward
  72.  
  73. # part 1
  74. print("part 1")
  75. intcode(contents)
  76.  
  77. # part 2
  78. print("\npart 2")
  79. intcode(contents)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement