SHARE
TWEET

aoc 2019-11 python

Funguu Dec 11th, 2019 (edited) 85 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import intcode
  2. from collections import defaultdict
  3.  
  4. def main():
  5.     with open("input/day11.txt") as file:
  6.         memory = [int(val) for val in file.read().split(",")]
  7.     memory += [0] * 1000
  8.    
  9.     paint = defaultdict(lambda : 0)
  10.     calculatePaint(memory.copy(), paint)
  11.     part1 = len(paint)
  12.  
  13.     paint = defaultdict(lambda : 0)
  14.     paint[0] = 1
  15.     calculatePaint(memory.copy(), paint)
  16.    
  17.     print("part 1:", part1)
  18.     print("part 2:")
  19.     for y in range(-1, 7):
  20.         for x in range(0, 41):
  21.             if paint[x + y * 1j] == 1:
  22.                 print("#", end = '')
  23.             else:
  24.                 print(" ", end = '')
  25.         print("")
  26.  
  27. def calculatePaint(memory, paint):
  28.     pos = 0
  29.     facing = -1j
  30.  
  31.     ip = 0
  32.     finished = False
  33.     relativeBase = 0
  34.     while finished == False:
  35.         output = []
  36.         ip, finished, relativeBase = intcode.runProgram(memory, [paint[pos]], output, ip, relativeBase)
  37.         paint[pos] = output[0]
  38.         facing *= (1j if output[1] == 1 else -1j)
  39.         pos += facing
  40.  
  41. main()
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48. ==== intcode.py ====
  49. def runProgram(memory, input = [], output = [], ip = 0, relativeBase = 0):
  50.     while memory[ip] % 100 != 99:
  51.         ip, finished, relativeBase = process(memory, ip, input, output, relativeBase)
  52.         if finished == False:
  53.             break
  54.     return ip, finished, relativeBase
  55.  
  56. def process(memory, ip, input, output, relativeBase = 0):
  57.     instruction = memory[ip] % 100
  58.    
  59.     if instruction == 1: # Add
  60.         parameters = getParameters(memory, ip, relativeBase, 3)
  61.         memory[parameters[-1]] = parameters[0] + parameters[1]
  62.         ip += 4
  63.  
  64.     elif instruction == 2: # Multiply
  65.         parameters = getParameters(memory, ip, relativeBase, 3)
  66.         memory[parameters[-1]] = parameters[0] * parameters[1]
  67.         ip += 4
  68.  
  69.     elif instruction == 3: # Input
  70.         if len(input) == 0:
  71.             return ip, False, relativeBase
  72.         outPos = getOutPos(memory, ip, relativeBase, 1)
  73.         memory[outPos] = input.pop(0)
  74.         ip += 2
  75.    
  76.     elif instruction == 4: # Output
  77.         parameters = getParameters(memory, ip, relativeBase, 1)
  78.         output.append(parameters[0])
  79.         ip += 2
  80.  
  81.     elif instruction == 5: # Jump if true
  82.         parameters = getParameters(memory, ip, relativeBase, 2)
  83.         if parameters[0] != 0:
  84.             ip = parameters[1]
  85.         else:
  86.             ip += 3
  87.  
  88.     elif instruction == 6: # Jump if false
  89.         parameters = getParameters(memory, ip, relativeBase, 2)
  90.         if parameters[0] == 0:
  91.             ip = parameters[1]
  92.         else:
  93.             ip += 3
  94.  
  95.     elif instruction == 7: # Less than
  96.         parameters = getParameters(memory, ip, relativeBase, 3)
  97.         if parameters[0] < parameters[1]:
  98.             memory[parameters[-1]] = 1
  99.         else:
  100.             memory[parameters[-1]] = 0
  101.         ip += 4
  102.    
  103.     elif instruction == 8: # Equals
  104.         parameters = getParameters(memory, ip, relativeBase, 3)
  105.         if parameters[0] == parameters[1]:
  106.             memory[parameters[-1]] = 1
  107.         else:
  108.             memory[parameters[-1]] = 0
  109.         ip += 4
  110.  
  111.     elif instruction == 9: # Adjust relative base
  112.         parameters = getParameters(memory, ip, relativeBase, 1)
  113.         relativeBase += parameters[0]
  114.         ip += 2
  115.  
  116.     return ip, True, relativeBase
  117.  
  118. def getParameters(memory, ip, relativeBase, nrOfParameters):
  119.     modes = list(reversed(str(int(memory[ip] / 100))))
  120.     ret = []
  121.     for i in range(nrOfParameters):
  122.         value = memory[ip + 1 + i]
  123.         if i < len(modes):
  124.             if modes[i] == "0":
  125.                 ret.append(memory[value])
  126.             elif modes[i] == "1":
  127.                 ret.append(value)
  128.             elif modes[i] == "2":
  129.                 ret.append(memory[relativeBase + value])
  130.         else:
  131.             ret.append(memory[value])
  132.     ret.append(getOutPos(memory, ip, relativeBase, nrOfParameters))
  133.     return ret
  134.  
  135. def getOutPos(memory, ip, relativeBase, nrOfParameters):
  136.     modes = list(reversed(str(int(memory[ip] / 100))))
  137.     i = nrOfParameters - 1
  138.     value = memory[ip + nrOfParameters]
  139.     if i < len(modes):
  140.         if modes[i] == "0":
  141.             return value
  142.         elif modes[i] == "2":
  143.             return relativeBase + value
  144.     else:
  145.         return value
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top