Advertisement
Guest User

Untitled

a guest
Dec 11th, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.34 KB | None | 0 0
  1. def intcode(startingValue, relativeBase, panelColor, x):
  2.  
  3. outputArray = []
  4. inputInstruction = panelColor #send 0 or 1 to input instruction
  5.  
  6. i = startingValue
  7. while i < len(x):
  8. op = int(x[i]%100)
  9. modes = int(x[i]/100)
  10. parameter1 = modes%10
  11. parameter2 = (modes//10)%10
  12. parameter3 = (modes//100)
  13.  
  14.  
  15. if (op == 1): #add
  16. #check len of x[i]
  17. #now do the operation
  18. first = x[i+1]
  19. second = x[i+2]
  20. if (parameter1 == 0):
  21. first = x[x[i+1]]
  22. if (parameter1 == 2):
  23. first = x[x[i+1]+relativeBase]
  24.  
  25. if (parameter2 == 0):
  26. second = x[x[i+2]]
  27. if (parameter2 == 2):
  28. second = x[x[i+2]+relativeBase]
  29.  
  30.  
  31. result = first + second
  32.  
  33.  
  34. if (parameter3 == 0):
  35. x[x[i+3]] = result
  36. if (parameter3 == 1):
  37. x[i+3] = result
  38. if (parameter3 == 2):
  39. x[x[i+3] + relativeBase] = result
  40.  
  41. i+=4
  42.  
  43. #multiply
  44. if (op == 2):
  45. #check len of x[i]
  46. first = x[i+1]
  47. second = x[i+2]
  48. if (parameter1 == 0):
  49. first = x[x[i+1]]
  50. if (parameter1 == 2):
  51. first = x[x[i+1]+relativeBase]
  52.  
  53. if (parameter2 == 0):
  54. second = x[x[i+2]]
  55. if (parameter2 == 2):
  56. second = x[x[i+2]+relativeBase]
  57.  
  58. result = first * second
  59.  
  60.  
  61.  
  62. if (parameter3 == 0):
  63. x[x[i+3]] = result
  64. if (parameter3 == 1):
  65. x[i+3] = result
  66. if (parameter3 == 2):
  67. x[x[i+3] + relativeBase] = result
  68.  
  69. x[x[i+3]] = result
  70. i+=4
  71.  
  72. #read
  73. if (op == 3):
  74.  
  75. #asks for input
  76. if (parameter1 == 1):
  77. x[i+1] = inputInstruction
  78.  
  79. if (parameter1 == 2):
  80. x[x[i+1] + relativeBase] = inputInstruction
  81.  
  82. if (parameter1 == 0):
  83. x[x[i+1]] = inputInstruction
  84.  
  85.  
  86. i+=2
  87.  
  88. #output
  89. if (op == 4):
  90.  
  91.  
  92.  
  93. #now do the operation
  94. output = x[i+1]
  95.  
  96. if (parameter1 == 2):
  97. output = x[x[i+1] + relativeBase]
  98.  
  99. if (parameter1 == 0):
  100. output = x[x[i+1]]
  101.  
  102. outputArray.append(output)
  103. #return output array after 2 outputs have occurred
  104. #send back the current state of intcode and the location to start at?
  105.  
  106. if (len(outputArray) == 2):
  107. return (outputArray + [i+2] + [relativeBase], x)
  108.  
  109.  
  110. i+=2
  111.  
  112.  
  113.  
  114.  
  115. if (op == 5):
  116. #check len of x[i]
  117. # immediate mode / position mode
  118. first = x[i+1]
  119. second = x[i+2]
  120. if (parameter1 == 0):
  121. first = x[x[i+1]]
  122. if (parameter1 == 2):
  123. first = x[x[i+1]+relativeBase]
  124.  
  125. if (parameter2 == 0):
  126. second = x[x[i+2]]
  127. if (parameter2 == 2):
  128. second = x[x[i+2]+relativeBase]
  129.  
  130. if (first != 0 ):
  131. i = second
  132. else:
  133. i+=3
  134.  
  135. if (op == 6):
  136. #check len of x[i]
  137. # immediate mode / position mode
  138. first = x[i+1]
  139. second = x[i+2]
  140. if (parameter1 == 0):
  141. first = x[x[i+1]]
  142. if (parameter1 == 2):
  143. first = x[x[i+1]+relativeBase]
  144.  
  145. if (parameter2 == 0):
  146. second = x[x[i+2]]
  147. if (parameter2 == 2):
  148. second = x[x[i+2]+relativeBase]
  149.  
  150. if (first == 0 ):
  151. i = second
  152. else:
  153. i+=3
  154.  
  155. if (op == 7):
  156. #check len of x[i]
  157. # immediate mode / position mode
  158.  
  159. first = x[i+1]
  160. second = x[i+2]
  161. if (parameter1 == 0):
  162. first = x[x[i+1]]
  163. if (parameter1 == 2):
  164. first = x[x[i+1]+relativeBase]
  165.  
  166. if (parameter2 == 0):
  167. second = x[x[i+2]]
  168. if (parameter2 == 2):
  169. second = x[x[i+2]+relativeBase]
  170.  
  171. if (first < second ):
  172. if (parameter3 == 0):
  173. x[x[i+3]] = 1
  174. if (parameter3 == 1):
  175. x[i+3] = 1
  176. if (parameter3 == 2):
  177. x[x[i+3] + relativeBase] = 1
  178.  
  179. else:
  180. if (parameter3 == 0):
  181. x[x[i+3]] = 0
  182. if (parameter3 == 1):
  183. x[i+3] = 0
  184. if (parameter3 == 2):
  185. x[x[i+3] + relativeBase] = 0
  186.  
  187. i+=4
  188.  
  189. if (op == 8):
  190. # check len of x[i]
  191. # immediate mode / position mode
  192. first = x[i+1]
  193. second = x[i+2]
  194. if (parameter1 == 0):
  195. first = x[x[i+1]]
  196. if (parameter1 == 2):
  197. first = x[x[i+1]+relativeBase]
  198.  
  199. if (parameter2 == 0):
  200. second = x[x[i+2]]
  201. if (parameter2 == 2):
  202. second = x[x[i+2]+relativeBase]
  203.  
  204. if (first == second):
  205. if (parameter3 == 0):
  206. x[x[i+3]] = 1
  207. if (parameter3 == 1):
  208. x[i+3] = 1
  209. if (parameter3 == 2):
  210. x[x[i+3] + relativeBase] = 1
  211. else:
  212. if (parameter3 == 0):
  213. x[x[i+3]] = 0
  214. if (parameter3 == 1):
  215. x[i+3] = 0
  216. if (parameter3 == 2):
  217. x[x[i+3] + relativeBase] = 0
  218.  
  219. i+=4
  220.  
  221. if (op == 9):
  222. # check len of x[i]
  223. # immediate mode / position mode
  224. first = x[i+1]
  225. if (parameter1 == 0):
  226. first = x[x[i+1]]
  227. if (parameter1 == 2):
  228. first = x[x[i+1]+relativeBase]
  229.  
  230. relativeBase += first
  231.  
  232. i+=2
  233.  
  234. if (op == 99):
  235. return ([999999,999999,999999], x)
  236.  
  237.  
  238. def advent11():
  239. f = open('C:\\Users\\Lucas\\Desktop\\advent\\adventday11.txt', 'r')
  240. inputToIntcode = f.readline().split(',')
  241. f.close()
  242. for i in range(len(inputToIntcode)):
  243. inputToIntcode[i] = int(inputToIntcode[i])
  244. inputToIntcode.extend([0] * 1000)
  245. robotPanelColor = 0
  246. intCodePointer = 0
  247. panelsColoredCount = 0
  248. boolTest = True
  249. relativeBase = 0
  250.  
  251. w, h = 1000, 1000;
  252. matrix1 = [[(0,0) for x in range(w)] for y in range(h)]
  253. x = 500
  254. y = 500
  255. direction = '^'
  256. while (boolTest):
  257. firstArg, inputToIntcode = intcode(intCodePointer, relativeBase, robotPanelColor, inputToIntcode)
  258. intCodePointer = firstArg[2]
  259. relativeBase = firstArg[3]
  260. colorToPaintPanel = firstArg[0]
  261. directionToMoveRobot = int(firstArg[1])
  262. print (directionToMoveRobot)
  263. if (directionToMoveRobot == 0):
  264. #move robot left
  265. if (direction == '^'):
  266. direction = '<'
  267. matrix1[x][y] = colorToPaintPanel
  268. x = x - 1
  269. elif (direction == '<'):
  270. direction = 'v'
  271. matrix1[x][y] = colorToPaintPanel
  272. y = y - 1
  273. elif (direction == 'v'):
  274. direction = '>'
  275. matrix1[x][y] = colorToPaintPanel
  276. x = x + 1
  277. elif (direction == '>'):
  278. direction = '^'
  279. matrix1[x][y] = colorToPaintPanel
  280. y = y + 1
  281.  
  282. if (directionToMoveRobot == 1):
  283. #move robot right
  284. if (direction == '^'):
  285. direction = '>'
  286. matrix1[x][y] = colorToPaintPanel
  287. x = x + 1
  288. elif (direction == '>'):
  289. direction = 'v'
  290. matrix1[x][y] = colorToPaintPanel
  291. y = y - 1
  292. elif (direction == 'v'):
  293. direction = '<'
  294. matrix1[x][y] = colorToPaintPanel
  295. x = x - 1
  296. elif (direction == '<'):
  297. direction = '^'
  298. matrix1[x][y] = colorToPaintPanel
  299. y = y + 1
  300.  
  301. robotPanelColor = matrix1[x][y]
  302.  
  303. if (firstArg[0] == 999999):
  304. #stop
  305. boolTest = False
  306.  
  307.  
  308.  
  309.  
  310.  
  311. # move robot then pass in new coords
  312.  
  313.  
  314. print(advent11())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement