Day 19 part 1

This commit is contained in:
Adrien Abraham 2019-12-19 12:56:55 +01:00
parent 0a694d06f9
commit 7ae1bf9318
3 changed files with 46 additions and 10 deletions

31
2019/day19.py Normal file
View file

@ -0,0 +1,31 @@
from grid import Grid
from intcode import Intcode
from tools import parse_intcode
def main():
codes = parse_intcode("day19.txt")
area = Grid()
for y in range(50):
for x in range(50):
mgr = DroneMgr(codes)
mgr.run(inputs=[x, y])
area.setv(x, y, mgr.output)
area.dumb_print()
num_affected = sum(v for _, _, v in area.values_gen())
print("Num affected:", num_affected)
class DroneMgr(Intcode):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.inputs = [0, 0]
self.output = None
def output_data(self, data):
self.output = data
if __name__ == "__main__":
main()

View file

@ -1,7 +1,6 @@
""" Intcode interpreter. Day 11. """ """Intcode interpreter."""
from enum import IntEnum from enum import IntEnum
import sys
class Op(IntEnum): class Op(IntEnum):
@ -32,8 +31,8 @@ class Intcode(object):
self.ctx_op = None self.ctx_op = None
self.ctx_modes = None self.ctx_modes = None
self.halt = False self.halt = False
self.inputs = None
self.debug = debug self.debug = debug
self._inputs_list = None
@staticmethod @staticmethod
def parse_input(text): def parse_input(text):
@ -44,7 +43,7 @@ class Intcode(object):
print("debug:", message, *args) print("debug:", message, *args)
def run(self, inputs=None): def run(self, inputs=None):
self.inputs = inputs or [] self._inputs_list = inputs or []
handlers = self.get_handlers() handlers = self.get_handlers()
while not self.halt: while not self.halt:
self.read_code() self.read_code()
@ -147,7 +146,7 @@ class Intcode(object):
self.halt = True self.halt = True
def input_data(self): def input_data(self):
return self.inputs.pop(0) return self._inputs_list.pop(0)
def output_data(self, data): def output_data(self, data):
print(">", data) print(">", data)

6
tools.py Normal file
View file

@ -0,0 +1,6 @@
from intcode import Intcode
def parse_intcode(filename):
with open(filename, "rt") as input_file:
return Intcode.parse_input(input_file.read().rstrip())