Created
December 3, 2019 08:21
-
-
Save olanystrom/5ff18a63f96adec9b09e8564be50593c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/env python3 | |
# | |
# https://adventofcode.com/2019/day/2 | |
# | |
# Ola Nyström <[email protected]> | |
from pprint import pprint | |
realcode = "1,0,0,3,1,1,2,3,1,3,4,3,1,5,0,3,2,6,1,19,1,19,5,23,2,10,23,27,2,27,13,31,1,10,31,35,1,35,9,39,2,39,13,43,1,43,5,47,1,47,6,51,2,6,51,55,1,5,55,59,2,9,59,63,2,6,63,67,1,13,67,71,1,9,71,75,2,13,75,79,1,79,10,83,2,83,9,87,1,5,87,91,2,91,6,95,2,13,95,99,1,99,5,103,1,103,2,107,1,107,10,0,99,2,0,14,0".split(',') | |
realcode[1] = "12" | |
realcode[2] = "2" | |
realcode = ",".join(realcode) | |
def intcode(code): | |
intcode = code.split(',') | |
intcode = [int(a) for a in intcode] | |
running = True | |
pos = 0 | |
while running: | |
op = intcode[pos] | |
if(op == 1): | |
src1 = intcode[pos+1] | |
src2 = intcode[pos+2] | |
dst = intcode[pos+3] | |
intcode[dst] = intcode[src1] + intcode[src2] | |
pos += 4 | |
elif(op == 2): | |
src1 = intcode[pos+1] | |
src2 = intcode[pos+2] | |
dst = intcode[pos+3] | |
intcode[dst] = intcode[src1] * intcode[src2] | |
pos += 4 | |
elif(op == 99): | |
running = False | |
else: | |
raise UserWarning | |
return ",".join([str(a) for a in intcode]) | |
pprint(intcode(realcode)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment