Day 19 part 2
This commit is contained in:
parent
7ae1bf9318
commit
9b286f12ad
|
@ -1,28 +1,53 @@
|
||||||
from grid import Grid
|
from grid import Grid
|
||||||
from intcode import Intcode
|
from intcode import Intcode
|
||||||
from tools import parse_intcode
|
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
codes = parse_intcode("day19.txt")
|
codes = Intcode.parse_file("day19.txt")
|
||||||
|
mgr = DroneMgr(codes)
|
||||||
|
|
||||||
|
# Part 1
|
||||||
area = Grid()
|
area = Grid()
|
||||||
for y in range(50):
|
for y in range(50):
|
||||||
for x in range(50):
|
for x in range(50):
|
||||||
mgr = DroneMgr(codes)
|
|
||||||
mgr.run(inputs=[x, y])
|
mgr.run(inputs=[x, y])
|
||||||
area.setv(x, y, mgr.output)
|
area.setv(y, x, mgr.clear())
|
||||||
area.dumb_print()
|
area.dumb_print()
|
||||||
num_affected = sum(v for _, _, v in area.values_gen())
|
num_affected = sum(v for _, _, v in area.values_gen())
|
||||||
print("Num affected:", num_affected)
|
print("Num affected:", num_affected)
|
||||||
|
|
||||||
|
# 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])
|
||||||
|
|
||||||
|
|
||||||
class DroneMgr(Intcode):
|
class DroneMgr(Intcode):
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, program, *args, **kwargs):
|
||||||
super().__init__(*args, **kwargs)
|
super().__init__(program, *args, **kwargs)
|
||||||
self.inputs = [0, 0]
|
self.program = program.copy()
|
||||||
self.output = None
|
self.output = None
|
||||||
|
|
||||||
|
def clear(self):
|
||||||
|
output = self.output
|
||||||
|
self.reset()
|
||||||
|
self.output = None
|
||||||
|
return output
|
||||||
|
|
||||||
def output_data(self, data):
|
def output_data(self, data):
|
||||||
self.output = data
|
self.output = data
|
||||||
|
|
||||||
|
|
|
@ -25,6 +25,7 @@ class PMode(IntEnum):
|
||||||
class Intcode(object):
|
class Intcode(object):
|
||||||
|
|
||||||
def __init__(self, program, debug=False):
|
def __init__(self, program, debug=False):
|
||||||
|
self._program = program.copy()
|
||||||
self._memory = program.copy()
|
self._memory = program.copy()
|
||||||
self.ip = 0
|
self.ip = 0
|
||||||
self.rel_base = 0
|
self.rel_base = 0
|
||||||
|
@ -33,9 +34,20 @@ class Intcode(object):
|
||||||
self.halt = False
|
self.halt = False
|
||||||
self.debug = debug
|
self.debug = debug
|
||||||
self._inputs_list = None
|
self._inputs_list = None
|
||||||
|
|
||||||
|
def reset(self):
|
||||||
|
self._memory = self._program.copy()
|
||||||
|
self.ip = 0
|
||||||
|
self.rel_base = 0
|
||||||
|
self.halt = False
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def parse_input(text):
|
def parse_file(filename):
|
||||||
|
with open(filename, "rt") as input_file:
|
||||||
|
return Intcode.parse_text(input_file.read().rstrip())
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def parse_text(text):
|
||||||
return [int(i) for i in text.rstrip().split(",")]
|
return [int(i) for i in text.rstrip().split(",")]
|
||||||
|
|
||||||
def log(self, message, *args):
|
def log(self, message, *args):
|
||||||
|
|
Loading…
Reference in a new issue