Beispiel #1
0
def part1(ins):
    p1i = list(ins)
    p1i[1] = 12
    p1i[2] = 2
    cpu = CPU(p1i)
    cpu.run()
    return cpu.memory[0]
Beispiel #2
0
def part1(ins):
    outs = []
    p1i = lambda: (x for x in [1])
    p1o = lambda x: outs.append(x)

    cpu = CPU(ins, p1i, p1o)
    cpu.run()
    return outs[-1]
Beispiel #3
0
def part2(ins):
    outs = []
    p2i = lambda: (x for x in [5])
    p2o = lambda x: outs.append(x)

    cpu = CPU(ins, p2i, p2o)
    cpu.run()
    return outs[-1]
Beispiel #4
0
def main():
    # Read in the program
    text = (Path(__file__).parent /
            "../../input/2019/input_05.txt").read_text()
    program = [int(x) for x in text.split(',')]
    # Run with each system ID
    for i, sys_id in enumerate([1, 5]):
        cpu = CPU(program)
        cpu.input.append(sys_id)
        cpu.run()
        print(f"Part {i + 1}: {cpu.output[-1]}")
Beispiel #5
0
def try_combo(program: list[int], phases: tuple[int, ...], loop: bool) -> int:
    # Setup amps
    amps = [CPU(program)]
    # Each amp's output is the next amp's input
    for i in range(1, 5):
        amps.append(CPU(program, input=amps[i - 1].output))
        amps[i].input.append(phases[i])
    if loop:
        amps[0].input = amps[-1].output
    amps[0].input.extend([phases[0], 0])
    # Run amps in sequence
    for i, amp in it.cycle(enumerate(amps)):
        op = amp.run()
        if (not loop or op == 99) and i == 4:
            break
    return amps[-1].output[-1]
Beispiel #6
0
def main():
    # Read in the program
    text = (Path(__file__).parent /
            "../../input/2019/input_02.txt").read_text()
    program = [int(x) for x in text.split(',')]
    # Part 1
    cpu = CPU(program)
    cpu.mem[1], cpu.mem[2] = 12, 2
    cpu.run()
    print("Part 1:", cpu.mem[0])
    # Part 2: Find the inputs that will produce 19690720
    for noun, verb in it.product(range(100), repeat=2):
        cpu = CPU(program.copy())
        cpu.mem[1], cpu.mem[2] = noun, verb
        cpu.run()
        if cpu.mem[0] == 19690720:
            print("Part 2:", 100 * noun + verb)
            return
Beispiel #7
0
def part2(ins):
    target = 19690720
    testi = list(ins)
    cpu = CPU(testi)

    for noun in range(0, 100):
        for verb in range(0, 100):
            cpu.memory[1] = noun
            cpu.memory[2] = verb
            cpu.run()
            if cpu.memory[0] == target:
                return (100 * noun) + verb
            else:
                cpu.reset()
Beispiel #8
0
def paint(program: list[int], start_white: bool = False) -> dict[tuple[int, int], int]:
    robot = CPU(program)
    x = y = 0
    direction = 0  # Robot starts facing up
    panels = {}
    if start_white:
        panels[(0, 0)] = 1
    # Run until the robot halts
    while True:
        # Give the robot its next colour
        robot.input.append(panels.get((x, y), 0))
        if robot.run() == 99:
            break
        # Paint the current tile
        panels[(x, y)] = robot.output.popleft()
        # Turn left or right
        turn =  -1 if robot.output.popleft() == 0 else 1
        direction = (direction + turn) % 4
        # Move forward
        dx, dy = deltas[direction]
        x, y = x + dx, y + dy
    return panels
Beispiel #9
0
def main():
    # part 1
    memory_ = numpy.loadtxt('day2\input.txt', delimiter=',', dtype=numpy.int64)
    memory = numpy.array(memory_, copy=True)
    memory[1] = 12
    memory[2] = 2
    cpu = CPU(memory)
    cpu.exec()
    print(f'Part 1: Result at index 0: {cpu.memory[0]}')

    # solution found from emperical data ¯\_(ツ)_/¯
    # output = mx + ny + c
    # m = 460800
    # y = 1
    # c = 337061
    memory = numpy.array(memory_, copy=True)
    x = 42
    y = 59
    memory[1] = x
    memory[2] = y
    cpu = CPU(memory)
    cpu.exec()
    print(f'Result for x: {x}, y: {y} is: {cpu.memory[0]}')
Beispiel #10
0
def run(ins, perms):
    def inputBuilder(phase, loc):
        def input():
            yield phase
            while True:
                val = connections[loc]
                connections[loc] = None
                yield val

        return input

    def outputBuilder(loc):
        def output(x):
            connections[(loc + 1) % AMP_STAGES] = x

        return output

    best = {}

    for p in perms:
        connections = [0, None, None, None, None]
        inputs = [
            inputBuilder(phase, pos)
            for phase, pos in zip(p, range(AMP_STAGES))
        ]
        outputs = [outputBuilder(pos) for pos in range(AMP_STAGES)]
        cpus = [CPU(ins, inputs[x], outputs[x]) for x in range(AMP_STAGES)]

        while not all([cpu.finished() for cpu in cpus]):
            for c in cpus:
                c.run()

        best[p] = connections[0]

    bestperm = max(best, key=lambda k: best[k])
    return (bestperm, best[bestperm])
Beispiel #11
0
                paddle = (x, y)
            elif tid == 4:
                ball = (x, y)
    return score, paddle, ball


def run(stdscr: Window) -> tuple[int, int]:
    stdscr.clear()
    text = (Path(__file__).parent / "../../input/2019/input_13.txt").read_text()
    # Replace the paddle row with walls
    pattern = r"1,(0,)+3,(0,)+1,"
    if m := re.search(pattern, text):
        text = re.sub(pattern, "1," * (len(m[0]) // 2), text)
    program = [int(x) for x in text.split(",")]
    # Initialize game
    cpu = CPU(program)
    tiles = {}
    cpu.run()
    score, paddle, ball = cast(tuple[int, int, int], update_tiles(cpu, tiles))
    # Get the screen dimensions
    width = max(t[0] for t in tiles) + 1
    height = max(t[1] for t in tiles) + 1
    win = curses.newwin(height + 1, width, 0, 0)
    block_count = sum(v == 2 for v in tiles.values())
    # Run game to completion
    cpu = CPU(program)
    cpu.mem[0] = 2
    tiles = {}
    while True:
        retcode = cpu.run()
        s, p, b = update_tiles(cpu, tiles)