2019-12-19 12:56:55 +01:00
|
|
|
from grid import Grid
|
|
|
|
from intcode import Intcode
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
2019-12-19 17:32:24 +01:00
|
|
|
codes = Intcode.parse_file("day19.txt")
|
|
|
|
mgr = DroneMgr(codes)
|
|
|
|
|
|
|
|
# Part 1
|
2019-12-19 12:56:55 +01:00
|
|
|
area = Grid()
|
|
|
|
for y in range(50):
|
|
|
|
for x in range(50):
|
|
|
|
mgr.run(inputs=[x, y])
|
2019-12-19 17:32:24 +01:00
|
|
|
area.setv(y, x, mgr.clear())
|
2019-12-19 12:56:55 +01:00
|
|
|
area.dumb_print()
|
|
|
|
num_affected = sum(v for _, _, v in area.values_gen())
|
|
|
|
print("Num affected:", num_affected)
|
|
|
|
|
2019-12-19 17:32:24 +01:00
|
|
|
# Part 2
|
|
|
|
d = 100 - 1
|
|
|
|
found = None
|
|
|
|
x, y = 0, 4 # skip beam being too narrow for grid
|
|
|
|
while not found:
|
|
|
|
y += 1
|
|
|
|
while True:
|
|
|
|
mgr.run(inputs=[x, y])
|
|
|
|
if mgr.clear() == 0:
|
|
|
|
x += 1
|
|
|
|
continue
|
|
|
|
mgr.run(inputs=[x + d, y - d])
|
|
|
|
if mgr.clear() == 1:
|
|
|
|
found = (x, y - d)
|
|
|
|
break
|
|
|
|
print("Found 100x100 ship with nearest corner at", found)
|
|
|
|
print("Answer:", found[0] * 10000 + found[1])
|
|
|
|
|
2019-12-19 12:56:55 +01:00
|
|
|
|
|
|
|
class DroneMgr(Intcode):
|
|
|
|
|
2019-12-19 17:32:24 +01:00
|
|
|
def __init__(self, program, *args, **kwargs):
|
|
|
|
super().__init__(program, *args, **kwargs)
|
|
|
|
self.program = program.copy()
|
|
|
|
self.output = None
|
|
|
|
|
|
|
|
def clear(self):
|
|
|
|
output = self.output
|
|
|
|
self.reset()
|
2019-12-19 12:56:55 +01:00
|
|
|
self.output = None
|
2019-12-19 17:32:24 +01:00
|
|
|
return output
|
2019-12-19 12:56:55 +01:00
|
|
|
|
|
|
|
def output_data(self, data):
|
|
|
|
self.output = data
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|