Day 19 part 1
This commit is contained in:
parent
0a694d06f9
commit
7ae1bf9318
31
2019/day19.py
Normal file
31
2019/day19.py
Normal 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()
|
|
@ -1,7 +1,6 @@
|
|||
""" Intcode interpreter. Day 11. """
|
||||
"""Intcode interpreter."""
|
||||
|
||||
from enum import IntEnum
|
||||
import sys
|
||||
|
||||
|
||||
class Op(IntEnum):
|
||||
|
@ -32,8 +31,8 @@ class Intcode(object):
|
|||
self.ctx_op = None
|
||||
self.ctx_modes = None
|
||||
self.halt = False
|
||||
self.inputs = None
|
||||
self.debug = debug
|
||||
self._inputs_list = None
|
||||
|
||||
@staticmethod
|
||||
def parse_input(text):
|
||||
|
@ -44,7 +43,7 @@ class Intcode(object):
|
|||
print("debug:", message, *args)
|
||||
|
||||
def run(self, inputs=None):
|
||||
self.inputs = inputs or []
|
||||
self._inputs_list = inputs or []
|
||||
handlers = self.get_handlers()
|
||||
while not self.halt:
|
||||
self.read_code()
|
||||
|
@ -147,7 +146,7 @@ class Intcode(object):
|
|||
self.halt = True
|
||||
|
||||
def input_data(self):
|
||||
return self.inputs.pop(0)
|
||||
return self._inputs_list.pop(0)
|
||||
|
||||
def output_data(self, data):
|
||||
print(">", data)
|
||||
|
|
Loading…
Reference in a new issue