Beispiel #1
0
 def __init__(self, memory):
     self.brain = IntCodeVM(memory, self, self)
     self.white = set()
     self.black = set()
     self.location = (0, 0)
     self.direction = (0, -1)
     self.put_state = 0
Beispiel #2
0
 def scan(self, pos):
     qin = Queue()
     qout = Queue()
     qin.put(pos[0])
     qin.put(pos[1])
     vm = IntCodeVM(self.memory, qin, qout)
     vm.run()
     return qout.get()
Beispiel #3
0
    def __init__(self, program, play_for_free=False, debug=False):
        if play_for_free:
            program = self._remove_check_for_quarters(program)
        self.debug = debug
        self.vm = IntCodeVM(program)
        self.grid = Grid()

        self.joystick_direction = self.JOYSTICK_NEUTRAL
        self.score = None
Beispiel #4
0
class Robot:
    LEFT = 0
    RIGHT = 1

    DIRECTIONS = {
        'up': (0, 1),
        'right': (1, 0),
        'down': (0, -1),
        'left': (-1, 0)
    }

    def __init__(self, program, panel):
        self.vm = IntCodeVM(program, [])
        self.position = (0, 0)
        self.direction = self.DIRECTIONS['up']
        self.panel = panel

    def run(self):
        while True:
            if self.vm.finished():
                return

            self.vm.add_input(self.panel.color(self.position))

            color = next(self.vm.run())
            direction = next(self.vm.run())

            self.panel.paint(self.position, color)
            self.move(direction)

    def move(self, direction):
        if direction == self.LEFT:
            if self.direction == self.DIRECTIONS['up']:
                self.direction = self.DIRECTIONS['left']
            elif self.direction == self.DIRECTIONS['left']:
                self.direction = self.DIRECTIONS['down']
            elif self.direction == self.DIRECTIONS['down']:
                self.direction = self.DIRECTIONS['right']
            elif self.direction == self.DIRECTIONS['right']:
                self.direction = self.DIRECTIONS['up']
        if direction == self.RIGHT:
            if self.direction == self.DIRECTIONS['up']:
                self.direction = self.DIRECTIONS['right']
            elif self.direction == self.DIRECTIONS['right']:
                self.direction = self.DIRECTIONS['down']
            elif self.direction == self.DIRECTIONS['down']:
                self.direction = self.DIRECTIONS['left']
            elif self.direction == self.DIRECTIONS['left']:
                self.direction = self.DIRECTIONS['up']

        px, py = self.position
        dx, dy = self.direction
        self.position = (px + dx, py + dy)
Beispiel #5
0
class Robot:
    def __init__(self, memory):
        self.brain = IntCodeVM(memory, self, self)
        self.white = set()
        self.black = set()
        self.location = (0, 0)
        self.direction = (0, -1)
        self.put_state = 0

    def get(self):
        "What colour is the current location?"
        if self.location in self.white:
            return 1
        return 0

    def put(self, value):
        "Take input from the brain"
        if self.put_state == 0:
            # What to paint
            if value == 0:
                if self.location in self.white:
                    self.white.remove(self.location)
                self.black.add(self.location)
            elif value == 1:
                if self.location in self.black:
                    self.black.remove(self.location)
                self.white.add(self.location)
            else:
                raise Exception("unexpected paint instruction")
            self.put_state = 1
        elif self.put_state == 1:
            # How to turn
            if value == 0:
                # turn left
                self.direction = (-self.direction[1], self.direction[0])
            elif value == 1:
                # turn right
                self.direction = (self.direction[1], -self.direction[0])
            else:
                raise Exception("unexpected turn instruction")
            self.put_state = 0
            self.location = (
                self.location[0] + self.direction[0],
                self.location[1] + self.direction[1],
            )
            print(f"Now at {self.location}")
        else:
            raise Exception("state machine fail")

    def run(self):
        "Go!"
        self.brain.run()
Beispiel #6
0
    def test_first_given_example(self):
        program = [
            109, 1, 204, -1, 1001, 100, 1, 100, 1008, 100, 16, 101, 1006, 101,
            0, 99
        ]

        self.assertEqual(list(IntCodeVM(program, [1]).run())[0], 109)
Beispiel #7
0
 def __init__(self, memory):
     self.output_queue = Queue()
     self.input_queue = Queue()
     # _our_ output queue is _their_ input queue and vice versa
     self.vm = IntCodeVM(memory, self.output_queue, self.input_queue)
     self.thread = Thread(target=run_vm, args=[self.vm])
     self.thread.start()
     self.room = Room()
     self.robot = (0,0)
     self.path = [(0,0)]
Beispiel #8
0
 def __init__(self, addr, memory):
     self.addr = addr
     self.addr_loaded = False
     self.pktin = Queue()
     self.vm = IntCodeVM(memory, self, self)
     self.thread = Thread(target=run_vm, args=[self.vm])
     self.state = 0
     self.idle = False
     self.dest = None
     self.x = None
     self.y = None
     self.reading = None
Beispiel #9
0
    def get(self):
        pass

    def put(self, value):
        self.inputs.append(value)
        if len(self.inputs) == 3:
            self.place(*self.inputs)
            self.inputs = []

    def print(self):
        for line in self.positions:
            print("".join(line))


infile = open(sys.argv[1])
line = infile.readline()
memory = [int(x) for x in line.split(",")]

screen = Screen()
vm = IntCodeVM(memory, screen, screen)
vm.run()

blocks = len(screen.places)
breakpoint()

screen.print()

blocks = [k for (k, v) in screen.places.items() if v == BLOCK]
print(f"{blocks} blocks on screen")
Beispiel #10
0
 def __init__(self, program, panel):
     self.vm = IntCodeVM(program, [])
     self.position = (0, 0)
     self.direction = self.DIRECTIONS['up']
     self.panel = panel
Beispiel #11
0
#!/usr/bin/env python3

import sys
import math
import re
import time
from threading import Thread
from queue import Queue, Empty

infile = open(sys.argv[1])

from intcode import IntCodeVM, IntIO, StdIO


def run_vm(vm):
    vm.run()


def read_memory(f):
    line = f.readline()
    return [int(x) for x in line.split(",")]

infile = open(sys.argv[1])
memory = read_memory(infile)

io = StdIO()
vm = IntCodeVM(memory, io, io)

run_vm(vm)
Beispiel #12
0
    def test_second_given_example(self):
        program = [104, 1125899906842624, 99]

        self.assertEqual(
            list(IntCodeVM(program, [1]).run())[0], 1125899906842624)
Beispiel #13
0
#!/usr/bin/env python3

import sys
import math
import re

infile = open(sys.argv[1])

from intcode import IntCodeVM, IntIO, StdIO


def run_vm(vm):
    vm.run()


def read_memory(f):
    line = f.readline()
    return [int(x) for x in line.split(",")]


infile = open(sys.argv[1])
memory = read_memory(infile)

#i = IntIO()
i = StdIO()
vm = IntCodeVM(memory, i, i)
vm.run()
Beispiel #14
0
class Arcade:
    BLOCK_TILE = 2
    PADDLE_TILE = 3
    BALL_TILE = 4

    JOYSTICK_NEUTRAL = 0
    JOYSTICK_LEFT = -1
    JOYSTICK_RIGHT = 1

    def __init__(self, program, play_for_free=False, debug=False):
        if play_for_free:
            program = self._remove_check_for_quarters(program)
        self.debug = debug
        self.vm = IntCodeVM(program)
        self.grid = Grid()

        self.joystick_direction = self.JOYSTICK_NEUTRAL
        self.score = None

    def run_instruction(self):
        x, y, value = self.get_instruction()
        if self.is_score(x, y):
            self.score = value
            self._debug(f"Setting score to {value}")
        else:
            self._debug(f'Setting tile of type {value} to ({x}, {y})')
            self.set_tile(x, y, value)
            self.play()

    def play(self):
        self._debug(
            f'Paddle: {self.paddle_position()}, Ball: {self.ball_position()}, Direction: {self.joystick_direction}'
        )

        if None in [self.paddle_position(), self.ball_position()]:
            return
        if self.paddle_position()[0] > self.ball_position()[0]:
            self.move_joystick(self.JOYSTICK_LEFT)
        elif self.paddle_position()[0] < self.ball_position()[0]:
            self.move_joystick(self.JOYSTICK_RIGHT)
        else:
            self.move_joystick(self.JOYSTICK_NEUTRAL)

    def is_score(self, x, y):
        return x == -1 and y == 0

    def set_tile(self, x, y, tile):
        self.grid.set(x, y, tile)

    def get_instruction(self):
        return list(next(self.vm.run()) for _ in range(3))

    def run(self):
        while not self.vm.finished():
            self.vm.set_input(self.joystick_direction)
            self.run_instruction()

    def blocks_count(self):
        return self.grid.count_of_tiles_of_type(self.BLOCK_TILE)

    def ball_position(self):
        return self.grid.get_position_of(self.BALL_TILE)

    def paddle_position(self):
        return self.grid.get_position_of(self.PADDLE_TILE)

    def move_joystick(self, direction):
        assert (direction in [
            self.JOYSTICK_LEFT, self.JOYSTICK_NEUTRAL, self.JOYSTICK_RIGHT
        ])

        self.joystick_direction = direction

    def _remove_check_for_quarters(self, program):
        program[0] = 2
        return program

    def _debug(self, message):
        if self.debug:
            print('[DEBUG] ' + message)
Beispiel #15
0
    def test_third_given_example(self):
        program = [1102, 34915192, 34915192, 7, 4, 7, 99, 0]

        self.assertEqual(
            list(IntCodeVM(program, [1]).run())[0], 1219070632396864)
Beispiel #16
0
    def __init__(self):
        self.pos = (0, 0)
        self.grid = Grid()

    def put(self, value):
        if value == 10:
            self.pos = (0, self.pos[1] + 1)
            return
        self.grid[self.pos] = chr(value)
        self.pos = (self.pos[0] + 1, self.pos[1])


infile = open(sys.argv[1])
memory = read_memory(infile)
reader = GridReader()
vm = IntCodeVM(memory, reader, reader)

vm.run()

reader.grid.print()


def adjacent(pos):
    return [
        (pos[0] + 1, pos[1]),
        (pos[0] - 1, pos[1]),
        (pos[0], pos[1] + 1),
        (pos[0], pos[1] - 1),
    ]

Beispiel #17
0
 def test_intcode(self):
     program = [3,21,1008,21,8,20,1005,20,22,107,8,21,20,1006,20,31,1106,0,36,98,0,0,1002,21,125,20,4,20,1105,1,46,104,999,1105,1,46,1101,1000,1,20,4,20,1105,1,46,98,99]
     
     self.assertEqual(list(IntCodeVM(program, [7]).run())[0], 999)
     self.assertEqual(list(IntCodeVM(program, [8]).run())[0], 1000)
     self.assertEqual(list(IntCodeVM(program, [9]).run())[0], 1001)
Beispiel #18
0
        print(chr(value), end="")

    def get(self):
        if not self.buffer:
            self.buffer = [ord(c) for c in input()] + [10]
        c = self.buffer[0]
        self.buffer = self.buffer[1:]
        return c


infile = open(sys.argv[1])
memory = read_memory(infile)
reader = GridReader()
io = InputReader()
memory[0] = 2
vm = IntCodeVM(memory, io, io)

vm.run()

reader.grid.print()


def adjacent(pos):
    return [
        (pos[0] + 1, pos[1]),
        (pos[0] - 1, pos[1]),
        (pos[0], pos[1] + 1),
        (pos[0], pos[1] - 1),
    ]