Exemplo n.º 1
0
def init_board(path):
    board = {}
    try:
        pc = Intcode(path)
        while True:
            x, y, id = (int(pc.read()), int(pc.read()), int(pc.read()))
            board[(x, y)] = id
    except IntcodeError:
        pass
    return board
Exemplo n.º 2
0
def b(path):
    l = get_combo_range(5, 9)
    max = 0
    max_combo = []
    for ll in l:
        l = 0
        last = 0
        pcs = []
        try:
            for i, lll in enumerate(ll):
                pc = Intcode(path)
                pc.write(f'{lll}')
                pcs.append(pc)

            flag = True
            while flag:
                for pc in pcs:
                    pc.write(f'{l}')
                    l = int(pc.read())
                    if pc.done():
                        flag = False
                        break
                last = l
        except IntcodeError as e:
            pass
        if last > max:
            max = last
            max_combo = ll

    print(f'Day7 B: {max}')
Exemplo n.º 3
0
def a(path):
    l = get_combo_range(0, 4)
    max = 0
    max_combo = []
    for ll in l:
        l = 0
        for i, lll in enumerate(ll):
            try:
                pc = Intcode(path)
                pc.write(f'{lll}')
                pc.write(f'{l}')
                l = int(pc.read())
            except IntcodeError:
                pass
        if l > max:
            max = l
            max_combo = ll
    print(f'Day7 A: {max}')
Exemplo n.º 4
0
class Robot:
    def __init__(self, path):
        self.int = Intcode(path)
        self.dir = 0
        self.directions = [(0, 1), (1, 0), (0, -1), (-1, 0)]
        self.x = 0
        self.y = 0
        self.painted = {(0, 0): "1"}

    def run(self):
        while not self.int.done():
            try:
                self.step()
            except IntcodeError:
                pass

    def step(self):
        w = self.int.write(f'{self.painted.get((self.x, self.y), "0")}')
        p = self.int.read()
        t = self.int.read()
        self.paint(p)
        self.turn(t)
        dx, dy = self.directions[self.dir]
        self.x += dx
        self.y += dy

    def paint(self, do):
        self.painted[(self.x, self.y)] = do

    def turn(self, direction):
        if direction == "0":
            self.dir -= 1
        elif direction == "1":
            self.dir += 1
        else:
            print("BAAAAAD")
        if self.dir < 0:
            self.dir += 4
        elif self.dir > 3:
            self.dir -= 4

    def get_output(self):
        x1 = -1
        y1 = -1
        x2 = 0
        y2 = 0
        for key in self.painted:
            x, y = key
            if y1 == -1 or y < y1:
                y1 = y
            if y > y2:
                y2 = y
            if x1 == 1 or x < x1:
                x1 = x
            if x > x2:
                x2 = x
        # print(f'{x1} {y1} {x2} {y2}')
        l = [[" " for xx in range(abs(x2 - x1) + 1)]
             for yy in range(abs(y2 - y1) + 1)]
        # print(f'l {len(l)}')
        # print(f'll {len(l[0])}')
        for key in self.painted:
            x, y = key
            if self.painted[key] == "1":
                l[y2 - y][x - x1] = "#"
        ss = ""
        for y in l:
            s = ""
            for x in y:
                s += x
            ss += f'{s}\n'
        return ss