from Day9.part1 import VM

if __name__ == "__main__":
    direction = "1"
    vm = VM(direction, direction, "input.txt")
    moves = {"1":(0,1), "2":(0,-1), "3":(-1,0), "4":(1,0)}
    next_dir = {"1":"3", "3":"2", "2":"4", "4":"1"}
    revert = {"1":"2", "2":"1", "4":"3", "3":"4"}
    results = {"error": 0, "moved": 1, "end":2}
    valid = set()
    valid.add((0,0))
    wall = set()
    o2 = set()
    counts = {}
    x = 0
    y = 0
    count = 0
    flag = False
    reversion = False
    path = []
    while not vm.is_done():
        count += 1
        vm.run()
        out = int(vm.output)
        if out == 0:
            wall.add((x + moves[direction][0], y + moves[direction][1]))
            direction = next_dir[direction]
            i = 0
            while (x + moves[direction][0], y + moves[direction][1]) in valid.union(wall, o2):
                if (x + moves[direction][0], y + moves[direction][1]) in counts:
                    if counts[(x + moves[direction][0], y + moves[direction][1])] > (len(path)+1):
from Day9.part1 import VM

if __name__ == "__main__":
    phase = "0"
    value = "0"
    vm = VM(phase, value, "input.txt")
    tiles = {0: ' ', 1:'X', 2: '#', 3:'-', 4:'o'}
    score = 0
    while not vm.is_done():
        vm.run()
        x = int(vm.output)
        vm.stopped = False
        vm.run()
        y = int(vm.output)
        vm.stopped = False
        vm.run()
        id = int(vm.output)
        vm.stopped = False
        if x == -1:
            if y == 0:
                score = id
    print(score)
#    print(vm.code)

if __name__ == "__main__":

    def move(x, y, facing):
        if facing == 0:
            return x + 1, y
        elif facing == 1:
            return x, y + 1
        elif facing == 2:
            return x - 1, y
        elif facing == 3:
            return x, y - 1

    phase = "1"
    value = "0"
    vm = VM(phase, value, "input.txt")
    area = {}
    x = 0
    y = 0
    facing = 0  # 0 - Up, 1 Right, 2 Down, 3 Left
    while not vm.is_done():
        vm.run()
        colour = vm.output
        area[(x, y)] = colour
        vm.stopped = False
        vm.run()
        direction = int(vm.output)
        if direction:
            facing = (facing + 1) % 4
        else:
            facing = (facing - 1) % 4