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
import sys
class Op(IntEnum):
@ -32,19 +31,19 @@ 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):
return [int(i) for i in text.rstrip().split(",")]
def log(self, message, *args):
if self.debug:
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()
@ -67,7 +66,7 @@ class Intcode(object):
def mem_get(self, pos):
self._check_memory_limits(pos)
return self._memory[pos]
def mem_set(self, pos, value):
self._check_memory_limits(pos)
self._memory[pos] = value
@ -99,7 +98,7 @@ class Intcode(object):
elif mode == PMode.REL:
address = self.rel_base + self.mem_get(self.ip + index)
return address if pointer else self.mem_get(address)
def write_at_param(self, param_offset, value):
self.mem_set(self.param(param_offset, pointer=True), value)
@ -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)

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())