Esempio n. 1
0
File: 21.py Progetto: vidstige/aoc
# Jump if any space (unless !A AND B AND !C) and double landing spaces ok
# !(A AND AND B C OR) AND D AND (H OR E)

#ABCDEFGHI
#.#.##.#.####

program = """
NOT T T
AND A T
AND B T
AND C T
NOT T J
AND D J
NOT A T
AND A T
OR H T
OR E T
AND T J
RUN
"""

vm.write_ascii(program.lstrip())
while not vm.is_terminated():
    c = vm.run()
    if c is not None:
        if c > 255:
            print(c)
            break
        print(str(chr(c)), end='')
Esempio n. 2
0
class State:
    def __init__(self):
        self.intcode = Intcode(program)
        self.latest = None
        self.position = (0, 0)
        self.grid = dict()

    def go(self, direction):
        if direction in DIRECTIONS:
            self.grid[self.position] = 'visited'
            dx, dy = DIRECTIONS[direction]
            x, y = self.position
            self.position = x + dx, y + dy

        data = direction
        game.intcode.write_ascii(data)
        game.intcode.write(10)
        game.latest = self.intcode.read_ascii()
        self.update_seen()
        print(self.latest)

    def update_seen(self):
        # Update seen rooms
        nav = parse(self.latest).get('navigation') or []
        x, y = self.position
        for direction in nav:
            if direction in DIRECTIONS:
                dx, dy = DIRECTIONS[direction]
                p = x + dx, y + dy
                if p not in self.grid:
                    self.grid[p] = 'seen'

    def take(self, item):
        data = "take " + request.json
        self.intcode.write_ascii(data)
        self.intcode.write(10)
        self.latest = game.intcode.read_ascii()
        print(self.latest)

    def drop(self, item):
        data = "drop " + request.json
        self.intcode.write_ascii(data)
        self.intcode.write(10)
        self.latest = game.intcode.read_ascii()
        print(self.latest)

    def chart(self):
        w, h = 16, 16

        def tojs(p, **kwargs):
            x, y = p
            d = dict(x=x + w // 2, y=y + h // 2)
            d.update(kwargs)
            return d

        rooms = []
        for p, value in self.grid.items():
            rooms.append(tojs(p, value=value))
        return dict(rooms=rooms, position=tojs(self.position))

    def inventory(self):
        self.intcode.write_ascii('inv\n')
        raw = self.intcode.read_ascii()
        return parse_inventory(raw)

    def json(self):
        j = parse(game.latest)
        j['inventory'] = self.inventory()
        return j