diff --git a/2019/day19.py b/2019/day19.py new file mode 100644 index 0000000..a31daa2 --- /dev/null +++ b/2019/day19.py @@ -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() diff --git a/2019/intcode.py b/2019/intcode.py index 0afc2fe..344fe1e 100644 --- a/2019/intcode.py +++ b/2019/intcode.py @@ -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) diff --git a/tools.py b/tools.py new file mode 100644 index 0000000..a877584 --- /dev/null +++ b/tools.py @@ -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())