Last active
December 6, 2019 02:52
-
-
Save theglauber/5d884851c5ea84cf9398a7f1f570cd7f to your computer and use it in GitHub Desktop.
Advent of code 2019 day 5
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
#!python | |
""" | |
https://adventofcode.com/2019/day/5 | |
Opcodes: | |
------- | |
:: | |
ABCDE | |
1002 | |
DE - two-digit opcode, 02 == opcode 2 | |
C - mode of 1st parameter, 0 == position mode | |
B - mode of 2nd parameter, 1 == immediate mode | |
A - mode of 3rd parameter, 0 == position mode, | |
omitted due to being a leading zero | |
Run: | |
--- | |
:: | |
please enter a number: 1 | |
Output: 0 | |
Output: 0 | |
Output: 0 | |
Output: 0 | |
Output: 0 | |
Output: 0 | |
Output: 0 | |
Output: 0 | |
Output: 0 | |
Output: 7265618 | |
Done | |
""" | |
import copy | |
program = [3, 225, 1, 225, 6, 6, 1100, 1, 238, 225, 104, 0, 1102, 59, 58, 224, 1001, 224, -3422, 224, 4, 224, 102, 8, 223, 223, 101, 3, 224, 224, 1, 224, 223, 223, 1101, 59, 30, 225, 1101, 53, 84, 224, 101, -137, 224, 224, 4, 224, 1002, 223, 8, 223, 101, 3, 224, 224, 1, 223, 224, 223, 1102, 42, 83, 225, 2, 140, 88, 224, 1001, 224, -4891, 224, 4, 224, 1002, 223, 8, 223, 1001, 224, 5, 224, 1, 223, 224, 223, 1101, 61, 67, 225, 101, 46, 62, 224, 1001, 224, -129, 224, 4, 224, 1002, 223, 8, 223, 101, 5, 224, 224, 1, 223, 224, 223, 1102, 53, 40, 225, 1001, 35, 35, 224, 1001, 224, -94, 224, 4, 224, 102, 8, 223, 223, 101, 6, 224, 224, 1, 223, 224, 223, 1101, 5, 73, 225, 1002, 191, 52, 224, 1001, 224, -1872, 224, 4, 224, 1002, 223, 8, 223, 1001, 224, 5, 224, 1, 223, 224, 223, 102, 82, 195, 224, 101, -738, 224, 224, 4, 224, 1002, 223, 8, 223, 1001, 224, 2, 224, 1, 224, 223, 223, 1101, 83, 52, 225, 1101, 36, 77, 225, 1101, 9, 10, 225, 1, 113, 187, 224, 1001, 224, -136, 224, 4, 224, 1002, 223, 8, 223, 101, 2, 224, 224, 1, 224, 223, 223, 4, 223, 99, 0, 0, 0, 677, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1105, 0, 99999, 1105, 227, 247, 1105, 1, 99999, 1005, 227, 99999, 1005, 0, 256, 1105, 1, 99999, 1106, 227, 99999, 1106, 0, 265, 1105, 1, 99999, 1006, 0, 99999, 1006, 227, 274, 1105, 1, 99999, 1105, 1, 280, 1105, 1, 99999, 1, 225, 225, 225, 1101, 294, 0, 0, 105, 1, 0, 1105, 1, 99999, 1106, 0, 300, 1105, 1, 99999, 1, 225, 225, 225, 1101, 314, 0, 0, 106, 0, 0, 1105, 1, 99999, 1007, 226, 226, 224, 1002, 223, 2, 223, 1006, 224, 329, 1001, 223, 1, 223, 1108, 226, 226, 224, 102, 2, 223, 223, 1006, 224, 344, 101, 1, 223, 223, 1007, 677, 677, 224, 102, 2, 223, 223, 1006, 224, 359, 101, 1, 223, 223, 1108, 677, 226, 224, 1002, 223, 2, 223, 1005, 224, 374, 1001, 223, 1, 223, 7, 677, 226, 224, 102, 2, 223, 223, 1005, 224, 389, 1001, 223, 1, 223, 1008, 677, 677, 224, 1002, 223, 2, 223, 1005, 224, 404, 101, 1, 223, 223, 108, 226, 226, 224, 1002, 223, 2, 223, 1006, 224, 419, 101, 1, 223, 223, 1008, 226, 677, 224, 1002, 223, 2, 223, 1006, 224, 434, 1001, 223, 1, 223, 1107, 677, 226, 224, 1002, 223, 2, 223, 1005, 224, 449, 101, 1, 223, 223, 1008, 226, 226, 224, 102, 2, 223, 223, 1005, 224, 464, 1001, 223, 1, 223, 8, 226, 226, 224, 1002, 223, 2, 223, 1006, 224, 479, 1001, 223, 1, 223, 107, 226, 677, 224, 102, 2, 223, 223, 1005, 224, 494, 1001, 223, 1, 223, 7, 226, 226, 224, 102, 2, 223, 223, 1005, 224, 509, 1001, 223, 1, 223, 107, 226, 226, 224, 102, 2, 223, 223, 1005, 224, 524, 101, 1, 223, 223, 107, 677, 677, 224, 1002, 223, 2, 223, 1006, 224, 539, 101, 1, 223, 223, 8, 677, 226, 224, 1002, 223, 2, 223, 1006, 224, 554, 101, 1, 223, 223, 1107, 677, 677, 224, 1002, 223, 2, 223, 1005, 224, 569, 101, 1, 223, 223, 108, 226, 677, 224, 1002, 223, 2, 223, 1006, 224, 584, 101, 1, 223, 223, 7, 226, 677, 224, 1002, 223, 2, 223, 1005, 224, 599, 1001, 223, 1, 223, 8, 226, 677, 224, 102, 2, 223, 223, 1006, 224, 614, 1001, 223, 1, 223, 108, 677, 677, 224, 1002, 223, 2, 223, 1006, 224, 629, 1001, 223, 1, 223, 1007, 226, 677, 224, 1002, 223, 2, 223, 1006, 224, 644, 101, 1, 223, 223, 1108, 226, 677, 224, 102, 2, 223, 223, 1005, 224, 659, 1001, 223, 1, 223, 1107, 226, 677, 224, 102, 2, 223, 223, 1006, 224, 674, 1001, 223, 1, 223, 4, 223, 99, 226] | |
def compute(program): | |
def get_input(list, pos, mode): | |
n = list[pos] | |
if mode == 0: | |
return list[n] | |
elif mode == 1: | |
return n | |
else: | |
raise Exception("Invalid mode: {}".format(mode)) | |
my_program = copy.deepcopy(program) | |
ip = 0 # instruction pointer | |
max_ip = len(my_program) | |
while ip < max_ip: | |
output = None | |
# modes: 0 = position, 1 = immediate | |
operator = my_program[ip] % 100 | |
mode1 = my_program[ip] // 100 % 10 | |
mode2 = my_program[ip] // 1000 % 10 | |
mode3 = my_program[ip] // 10000 % 10 | |
if (operator == 1) or (operator == 2): | |
# add or multiply | |
input1 = get_input(my_program, ip + 1, mode1) | |
input2 = get_input(my_program, ip + 2, mode2) | |
assert mode3 == 0, "output mode must be zero" | |
out_pos = my_program[ip + 3] | |
if operator == 1: | |
output = input1 + input2 | |
else: | |
output = input1 * input2 | |
my_program[out_pos] = output | |
ip += 4 | |
elif operator == 3: | |
# Input | |
input1 = int(input("please enter a number: ")) | |
assert mode1 == 0, "output mode must be zero" | |
out_pos = my_program[ip + 1] | |
my_program[out_pos] = input1 | |
ip += 2 | |
elif operator == 4: | |
# output | |
input1 = get_input(my_program, ip + 1, mode1) | |
print("Output: {}".format(input1)) | |
ip += 2 | |
elif operator == 99: | |
# return(my_program[0]) | |
print("Done") | |
ip = max_ip # the end | |
else: | |
raise Exception("Invalid operator: {}".format(operator)) | |
def main(): | |
compute(program) | |
if __name__ == "__main__": | |
main() |
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
#!python | |
""" | |
https://adventofcode.com/2019/day/5#part2 | |
Opcodes: | |
------- | |
:: | |
ABCDE | |
1002 | |
DE - two-digit opcode, 02 == opcode 2 | |
C - mode of 1st parameter, 0 == position mode | |
B - mode of 2nd parameter, 1 == immediate mode | |
A - mode of 3rd parameter, 0 == position mode, | |
omitted due to being a leading zero | |
Run: | |
--- | |
:: | |
please enter a number: 5 | |
Output: 7731427 | |
Done | |
""" | |
import copy | |
import operator | |
program = [3, 225, 1, 225, 6, 6, 1100, 1, 238, 225, 104, 0, 1102, 59, 58, 224, 1001, 224, -3422, 224, 4, 224, 102, 8, 223, 223, 101, 3, 224, 224, 1, 224, 223, 223, 1101, 59, 30, 225, 1101, 53, 84, 224, 101, -137, 224, 224, 4, 224, 1002, 223, 8, 223, 101, 3, 224, 224, 1, 223, 224, 223, 1102, 42, 83, 225, 2, 140, 88, 224, 1001, 224, -4891, 224, 4, 224, 1002, 223, 8, 223, 1001, 224, 5, 224, 1, 223, 224, 223, 1101, 61, 67, 225, 101, 46, 62, 224, 1001, 224, -129, 224, 4, 224, 1002, 223, 8, 223, 101, 5, 224, 224, 1, 223, 224, 223, 1102, 53, 40, 225, 1001, 35, 35, 224, 1001, 224, -94, 224, 4, 224, 102, 8, 223, 223, 101, 6, 224, 224, 1, 223, 224, 223, 1101, 5, 73, 225, 1002, 191, 52, 224, 1001, 224, -1872, 224, 4, 224, 1002, 223, 8, 223, 1001, 224, 5, 224, 1, 223, 224, 223, 102, 82, 195, 224, 101, -738, 224, 224, 4, 224, 1002, 223, 8, 223, 1001, 224, 2, 224, 1, 224, 223, 223, 1101, 83, 52, 225, 1101, 36, 77, 225, 1101, 9, 10, 225, 1, 113, 187, 224, 1001, 224, -136, 224, 4, 224, 1002, 223, 8, 223, 101, 2, 224, 224, 1, 224, 223, 223, 4, 223, 99, 0, 0, 0, 677, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1105, 0, 99999, 1105, 227, 247, 1105, 1, 99999, 1005, 227, 99999, 1005, 0, 256, 1105, 1, 99999, 1106, 227, 99999, 1106, 0, 265, 1105, 1, 99999, 1006, 0, 99999, 1006, 227, 274, 1105, 1, 99999, 1105, 1, 280, 1105, 1, 99999, 1, 225, 225, 225, 1101, 294, 0, 0, 105, 1, 0, 1105, 1, 99999, 1106, 0, 300, 1105, 1, 99999, 1, 225, 225, 225, 1101, 314, 0, 0, 106, 0, 0, 1105, 1, 99999, 1007, 226, 226, 224, 1002, 223, 2, 223, 1006, 224, 329, 1001, 223, 1, 223, 1108, 226, 226, 224, 102, 2, 223, 223, 1006, 224, 344, 101, 1, 223, 223, 1007, 677, 677, 224, 102, 2, 223, 223, 1006, 224, 359, 101, 1, 223, 223, 1108, 677, 226, 224, 1002, 223, 2, 223, 1005, 224, 374, 1001, 223, 1, 223, 7, 677, 226, 224, 102, 2, 223, 223, 1005, 224, 389, 1001, 223, 1, 223, 1008, 677, 677, 224, 1002, 223, 2, 223, 1005, 224, 404, 101, 1, 223, 223, 108, 226, 226, 224, 1002, 223, 2, 223, 1006, 224, 419, 101, 1, 223, 223, 1008, 226, 677, 224, 1002, 223, 2, 223, 1006, 224, 434, 1001, 223, 1, 223, 1107, 677, 226, 224, 1002, 223, 2, 223, 1005, 224, 449, 101, 1, 223, 223, 1008, 226, 226, 224, 102, 2, 223, 223, 1005, 224, 464, 1001, 223, 1, 223, 8, 226, 226, 224, 1002, 223, 2, 223, 1006, 224, 479, 1001, 223, 1, 223, 107, 226, 677, 224, 102, 2, 223, 223, 1005, 224, 494, 1001, 223, 1, 223, 7, 226, 226, 224, 102, 2, 223, 223, 1005, 224, 509, 1001, 223, 1, 223, 107, 226, 226, 224, 102, 2, 223, 223, 1005, 224, 524, 101, 1, 223, 223, 107, 677, 677, 224, 1002, 223, 2, 223, 1006, 224, 539, 101, 1, 223, 223, 8, 677, 226, 224, 1002, 223, 2, 223, 1006, 224, 554, 101, 1, 223, 223, 1107, 677, 677, 224, 1002, 223, 2, 223, 1005, 224, 569, 101, 1, 223, 223, 108, 226, 677, 224, 1002, 223, 2, 223, 1006, 224, 584, 101, 1, 223, 223, 7, 226, 677, 224, 1002, 223, 2, 223, 1005, 224, 599, 1001, 223, 1, 223, 8, 226, 677, 224, 102, 2, 223, 223, 1006, 224, 614, 1001, 223, 1, 223, 108, 677, 677, 224, 1002, 223, 2, 223, 1006, 224, 629, 1001, 223, 1, 223, 1007, 226, 677, 224, 1002, 223, 2, 223, 1006, 224, 644, 101, 1, 223, 223, 1108, 226, 677, 224, 102, 2, 223, 223, 1005, 224, 659, 1001, 223, 1, 223, 1107, 226, 677, 224, 102, 2, 223, 223, 1006, 224, 674, 1001, 223, 1, 223, 4, 223, 99, 226] | |
def compute(program): | |
def get_input(list, pos, mode): | |
n = list[pos] | |
if mode == 0: | |
return list[n] | |
elif mode == 1: | |
return n | |
else: | |
raise Exception("Invalid mode: {}".format(mode)) | |
my_program = copy.deepcopy(program) | |
ip = 0 # instruction pointer | |
max_ip = len(my_program) | |
while ip < max_ip: | |
output = None | |
# modes: 0 = position, 1 = immediate | |
opcode = my_program[ip] % 100 | |
mode1 = my_program[ip] // 100 % 10 | |
mode2 = my_program[ip] // 1000 % 10 | |
mode3 = my_program[ip] // 10000 % 10 | |
if (opcode == 1) or (opcode == 2): | |
# add or multiply | |
input1 = get_input(my_program, ip + 1, mode1) | |
input2 = get_input(my_program, ip + 2, mode2) | |
assert mode3 == 0, "output mode must be zero" | |
out_pos = my_program[ip + 3] | |
functor = operator.add if opcode == 1 else operator.mul | |
output = functor(input1, input2) | |
my_program[out_pos] = output | |
ip += 4 | |
elif opcode == 3: | |
# input | |
input1 = int(input("please enter a number: ")) | |
assert mode1 == 0, "output mode must be zero" | |
out_pos = my_program[ip + 1] | |
my_program[out_pos] = input1 | |
ip += 2 | |
elif opcode == 4: | |
# output | |
input1 = get_input(my_program, ip + 1, mode1) | |
print("Output: {}".format(input1)) | |
ip += 2 | |
elif (opcode == 5) or (opcode == 6): | |
# jump if true, jump if false | |
input1 = get_input(my_program, ip + 1, mode1) | |
input2 = get_input(my_program, ip + 2, mode2) | |
if (opcode == 5) and (input1 != 0): | |
ip = input2 | |
elif (opcode == 6) and (input1 == 0): | |
ip = input2 | |
else: | |
ip += 3 | |
elif (opcode == 7) or (opcode == 8): | |
# less-then or equal | |
input1 = get_input(my_program, ip + 1, mode1) | |
input2 = get_input(my_program, ip + 2, mode2) | |
comparator = operator.lt if opcode == 7 else operator.eq | |
assert mode3 == 0, "output mode must be zero" | |
# int(True) == 1, int(False) == 0 | |
out_pos = my_program[ip + 3] | |
my_program[out_pos] = int(comparator(input1, input2)) | |
ip += 4 | |
elif opcode == 99: | |
# halt | |
print("Done") | |
ip = max_ip # the end | |
else: | |
raise Exception("Invalid opcode: {}".format(opcode)) | |
def main(): | |
compute(program) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment