Esempio n. 1
0
def part_2(intcode):
    p2 = 0
    amp_a = IntCodeComputer(intcode)
    amp_b = IntCodeComputer(intcode)
    amp_c = IntCodeComputer(intcode)
    amp_d = IntCodeComputer(intcode)
    amp_e = IntCodeComputer(intcode)
    amps = [amp_a, amp_b, amp_c, amp_d, amp_e]
    for perm in permutations([5, 6, 7, 8, 9]):
        # Setup the amps phase settings
        for amp in amps:
            amp.reset()
        for index, amp in enumerate(amps):
            amp.write(perm[index])
            amp.run()
        amp_a.write(0)
        amp_a.run()
        curr_amp = 1
        output = amp_a.read()[0]
        while amp_e.status != Status.TERMINATED:
            amps[curr_amp].write(output)
            amps[curr_amp].run()
            output = amps[curr_amp].read()[0]
            curr_amp = ((curr_amp + 1) % len(amps))
        if p2 < int(output):
            p2 = int(output)
    return p2
Esempio n. 2
0
def solve_day_05(input):
    regex = re.compile("-{0,1}\d+")
    f = open(input, "r")
    computer = IntCodeComputer(regex.findall(f.readline()))
    computer.write(1)
    computer.run()
    p1 = computer.read()[-1]
    computer.reset()
    computer.write(5)
    computer.run()
    p2 = computer.read()[0]
    return (p1, p2)
Esempio n. 3
0
def part_1(intcode):
    p1 = 0
    computer = IntCodeComputer(intcode)
    for perm in permutations([0, 1, 2, 3, 4]):
        computer.write([perm[0], 0])
        computer.run()
        output = computer.read()[0]
        computer.reset()
        for p in perm[1:]:
            computer.write([p, output])
            computer.run()
            output = computer.read()[0]
            computer.reset()
        if p1 < output:
            p1 = output
    return p1
Esempio n. 4
0
def solve_day_19(input):
    regex = re.compile("-{0,1}\d+")
    f = open(input, "r")
    computer = IntCodeComputer(regex.findall(f.readline()))
    size = 50
    p1 = 0
    for x in range(size):
        for y in range(size):
            # I have no clue why I need three inputs, but I do
            computer.write([0, x, y])
            computer.run()
            result = computer.read()[0]
            p1 += result
            computer.reset()
            print(result, end="")
        print()
    return (p1, 0)
Esempio n. 5
0
def solve_day_13(input):
    regex = re.compile("-{0,1}\d+")
    f = open(input, "r")
    computer = IntCodeComputer(regex.findall(f.readline()))
    ts = play(computer, {})
    tiles = ts[0]
    score = ts[1]
    p1 = sum(value == 2 for value in tiles.values())
    print_arcade(tiles)
    computer.reset()
    computer.memory[0] = 2
    while computer.status != Status.TERMINATED:
        computer.write(get_joystick(tiles))
        ts = play(computer, tiles)
        tiles = ts[0]
        score = ts[1]
        # print_arcade(tiles)
    return (p1, score)
Esempio n. 6
0
def solve_day_11(input):
    # Instead of using a set for what panels have been painted
    # to see how many panels the robot paints at least once
    # I am just gonna gamble that part 2 asks about what was painted
    # so I can just go and print this dict
    panels = {}
    robot = Robot()
    regex = re.compile("-{0,1}\d+")
    f = open(input, "r")
    computer = IntCodeComputer(regex.findall(f.readline()))
    # computer.write(0) # Use this to start on a black panel for part 1
    computer.write(1)  # Use this to start on a white panel for part 2
    computer.run()
    output = computer.read()
    paint_as = output[0]
    turn = output[1]
    panels[(robot.x, robot.y)] = paint_as
    robot.turn(turn)
    robot.move()
    while computer.status != Status.TERMINATED:
        if (robot.x, robot.y) in panels:
            computer.write(panels[(robot.x, robot.y)])
        else:
            computer.write(0)
        computer.run()
        output = computer.read()
        paint_as = output[0]
        turn = output[1]
        panels[(robot.x, robot.y)] = paint_as
        robot.turn(turn)
        robot.move()
    pretty_print(panels)
    return len(panels)
Esempio n. 7
0
def solve_day_17(input):
    regex = re.compile("-{0,1}\d+")
    f = open(input, "r")
    computer = IntCodeComputer(regex.findall(f.readline()))
    computer.run()
    camera = computer.read()
    lst = []
    line = []
    for c in camera:
        if c == 10:
            lst.append(line)
            line = []
        else:
            line.append(c)
    p1 = 0
    for y, cy in enumerate(lst[1:-2], 1):
        for x, cx in enumerate(cy[1:-1], 1):
            if cx == 35:
                if lst[y - 1][x] == 35 and lst[y + 1][x] == 35 and lst[y][
                        x - 1] == 35 and lst[y][x + 1] == 35:
                    lst[y][x] = ord("O")
                    p1 += x * y
    # This was just for pretty printing so see how things look, but I am now using it
    # to manually solve part 2
    for cy in lst:
        for cx in cy:
            if cx == ord("."):
                print("⬛", end="")
            else:
                print("⬜", end="")
        print()
    # and after a little puzzling we get (see Scaffold.png)
    # "A"=65, "B"=66, "C"=67, ","=44, "\n"=10
    # "L"=76, "R"=82
    # "12"=49 50, "8"=56, "6"=54, "4"=52
    # "n" = 110
    # A = L12,L12,L6,L6
    # B = L12,L6,R12,R8
    # C = R8,R4,L12
    # A,C,A,B,C,A,B,C,A,B
    computer.reset()
    computer.memory[0] = 2
    instructions = [
        #A   ,   C   ,   A   ,   B   ,   C   ,   A   ,   B   ,   C   ,   A   ,   B  \n
        65,
        44,
        67,
        44,
        65,
        44,
        66,
        44,
        67,
        44,
        65,
        44,
        66,
        44,
        67,
        44,
        65,
        44,
        66,
        10,
        #L   ,   1   2   ,   L   ,   1   2   ,   L   ,   6   ,   L   ,   6  \n
        76,
        44,
        49,
        50,
        44,
        76,
        44,
        49,
        50,
        44,
        76,
        44,
        54,
        44,
        76,
        44,
        54,
        10,
        #L   ,   1   2   ,   L   ,   6   ,   R   ,   1   2   ,   R   ,   8  \n
        76,
        44,
        49,
        50,
        44,
        76,
        44,
        54,
        44,
        82,
        44,
        49,
        50,
        44,
        82,
        44,
        56,
        10,
        #R   ,   8   ,   R   ,   4   ,   L   ,   1   2  \n
        82,
        44,
        56,
        44,
        82,
        44,
        52,
        44,
        76,
        44,
        49,
        50,
        10,
        #n,  \n
        110,
        10
    ]
    computer.write(instructions)
    computer.run()
    p2 = computer.read()[-1]
    return (p1, p2)