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
|
from enum import IntEnum
|
||||||
import sys
|
|
||||||
|
|
||||||
|
|
||||||
class Op(IntEnum):
|
class Op(IntEnum):
|
||||||
|
@ -32,19 +31,19 @@ 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):
|
||||||
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):
|
||||||
if self.debug:
|
if self.debug:
|
||||||
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()
|
||||||
|
@ -67,7 +66,7 @@ class Intcode(object):
|
||||||
def mem_get(self, pos):
|
def mem_get(self, pos):
|
||||||
self._check_memory_limits(pos)
|
self._check_memory_limits(pos)
|
||||||
return self._memory[pos]
|
return self._memory[pos]
|
||||||
|
|
||||||
def mem_set(self, pos, value):
|
def mem_set(self, pos, value):
|
||||||
self._check_memory_limits(pos)
|
self._check_memory_limits(pos)
|
||||||
self._memory[pos] = value
|
self._memory[pos] = value
|
||||||
|
@ -99,7 +98,7 @@ class Intcode(object):
|
||||||
elif mode == PMode.REL:
|
elif mode == PMode.REL:
|
||||||
address = self.rel_base + self.mem_get(self.ip + index)
|
address = self.rel_base + self.mem_get(self.ip + index)
|
||||||
return address if pointer else self.mem_get(address)
|
return address if pointer else self.mem_get(address)
|
||||||
|
|
||||||
def write_at_param(self, param_offset, value):
|
def write_at_param(self, param_offset, value):
|
||||||
self.mem_set(self.param(param_offset, pointer=True), value)
|
self.mem_set(self.param(param_offset, pointer=True), value)
|
||||||
|
|
||||||
|
@ -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)
|
||||||
|
|
Loading…
Reference in a new issue