advent-of-code/2019-python/output/day_09.py

86 lines
1.9 KiB
Python

from output import answer
from output.intcode_computer import execute, parse
n = 9
title = "Sensor Boost"
@answer(1, "[intcode 0.3.1] BOOST keycode: {}")
def part_1(o):
return o[0]
@answer(2, "[intcode 0.3.1] Distress signal coordinates: {}")
def part_2(o):
return o[1]
def solve(data):
program = parse(data)
p12 = []
for inp in [1, 2]:
_c, _s, _n, _rb, outputs = execute(program, stdin=[inp])
p12.append(outputs.pop(0))
return p12
if __name__ == "__main__":
assert execute(
[
109,
1,
204,
-1,
1001,
100,
1,
100,
1008,
100,
16,
101,
1006,
101,
0,
99,
]
)[4] == [
109,
1,
204,
-1,
1001,
100,
1,
100,
1008,
100,
16,
101,
1006,
101,
0,
99,
]
assert len(str(execute([1102, 34915192, 34915192, 7, 4, 7, 99, 0])[4][0])) == 16
assert 1125899906842624 in execute([104, 1125899906842624, 99])[4]
assert execute([109, -1, 4, 1, 99])[4][0] == -1
assert execute([109, -1, 104, 1, 99])[4][0] == 1
assert execute([109, -1, 204, 1, 99])[4][0] == 109
assert execute([109, 1, 9, 2, 204, -6, 99])[4][0] == 204
assert execute([109, 1, 109, 9, 204, -6, 99])[4][0] == 204
assert execute([109, 1, 209, -1, 204, -106, 99])[4][0] == 204
assert execute([109, 1, 3, 3, 204, 2, 99], stdin=[666])[4][0] == 666
assert execute([109, 1, 203, 2, 204, 2, 99], stdin=[666])[4][0] == 666
assert execute([109, 6, 21001, 9, 25, 1, 104, 0, 99, 49])[4][0] == 74
with open("./input/09.txt", "r") as f:
inp = f.read().strip()
inp = solve(inp)
a = part_1(inp)
b = part_2(inp)
assert a == 2351176124
assert b == 73110