Beispiel #1
0
class Day25Computer:
    MAX_STEPS = 1_000_000

    def __init__(self):
        program = program_from_file("day25_input.txt")
        self.cpu = IntCode(program, self.input_fn, self.output_fn)
        self.input = []
        self.output = ""
        self.done = False

    def input_fn(self):
        if not self.input:
            command = self.command_fn(self.output)
            self.output = ""
            self.input = ["\n"]
            self.input.extend(reversed(command))
        return ord(self.input.pop())

    def output_fn(self, val):
        self.output += chr(val)

    def run(self):
        while not self.done and self.cpu.steps < self.MAX_STEPS:
            if not self.cpu.step():
                break
        self.done = True
Beispiel #2
0
class ArcadeCabinet:
    title = "Intcode Arcade Cabinet.  Score: "
    title_y = 1
    start_y = 2
    pause = 0.005

    def __init__(self, mem0=None):
        program = program_from_file("day13_input.txt")
        if mem0 is not None:
            program[0] = mem0
        self.intcode = IntCode(program, self.input_fn, self.output_fn)
        self.outs = []
        self.tiles = {}
        self.term = None
        self.score = 0
        self.ball_x = 0
        self.paddle_x = 0

    def input_fn(self):
        if self.pause:
            time.sleep(self.pause)
        if self.ball_x < self.paddle_x:
            return -1
        elif self.ball_x > self.paddle_x:
            return 1
        else:
            return 0

    def output_fn(self, val):
        self.outs.append(val)
        if len(self.outs) == 3:
            x, y, val = self.outs
            if (x, y) == (-1, 0):
                self.set_score(val)
            else:
                self.draw_tile(x, y, val)
                if val == 3:
                    self.paddle_x = x
                elif val == 4:
                    self.ball_x = x
            self.outs = []

    def set_score(self, score):
        self.score = score
        with self.term.location(len(self.title), self.title_y):
            print(f"{self.score:8d}")

    def draw_tile(self, x, y, tile):
        self.tiles[x, y] = tile
        with self.term.location(x, y + self.start_y):
            print(" |#_o"[tile])

    def num_blocks(self):
        return sum(int(tile == 2) for tile in self.tiles.values())

    def run(self):
        print("\n" * 60)
        self.term = blessings.Terminal()
        with self.term.hidden_cursor():
            with self.term.location(0, self.title_y):
                print(self.title)
            while self.intcode.step():
                pass