Beispiel #1
0
def part2(program, input_instruction=5):
    runner = IntcodeRunner(program)
    run = runner.run()
    while True:
        try:
            next(run)
            run.send(input_instruction)
        except HaltExecution:
            break

    return runner.output
Beispiel #2
0
def part1(program):
    runner = IntcodeRunner(program)
    run = runner.run()

    while True:
        try:
            next(run)
            run.send(1)
        except HaltExecution:
            break

    return runner.output
Beispiel #3
0
def part2(program):
    runner = IntcodeRunner(program)
    run = runner.run()
    grid = defaultdict(lambda: 0)
    x, y = (0, 0) #robot initial position
    grid[(x, y)] = 1
    direction = 'u' #robot initial direction

    while True: 
        try:
            next(run)
            color = run.send(grid[(x, y)])
            turn = run.send(None)
            grid[(x, y)] = color

            if turn: #turn right 90 degrees
                if direction == 'u':
                    direction = 'r'
                    x += 1
                elif direction == 'r':
                    direction = 'd'
                    y += 1
                elif direction == 'd':
                    direction = 'l'
                    x -= 1
                else:
                    direction = 'u'
                    y -= 1
            else:   #turn left 90 degrees
                if direction == 'u':
                    direction = 'l'
                    x -= 1
                elif direction == 'l':
                    direction = 'd'
                    y += 1
                elif direction == 'd':
                    direction = 'r'
                    x += 1
                else:
                    direction = 'u'
                    y -= 1

        except HaltExecution:
            break
    
    registration = [[' '] * 40 for i in range(10)]

    for x, y in [k for k, v in grid.items() if v == 1]:
        registration[2 + y][x] = '#'

    for row in registration:
        print(''.join(col*2 for col in row))
Beispiel #4
0
def part2(program):
    max_thruster = -1
    for p in permutations([5, 6, 7, 8, 9]):
        amplifiers = [IntcodeRunner(program, i).run() for i in range(5)]
        for i, amp in zip(p, amplifiers):
            next(amp)
            amp.send(i)
        val = 0
        e = amplifiers[4]  #Amplifier E
        while True:
            try:
                for i, amp in enumerate(amplifiers):
                    val = amp.send(val)
                    if val and amp == e:
                        if max_thruster < val:
                            max_thruster = val
                    next(amp)
            except StopIteration:
                pass
            except HaltExecution:
                amplifiers.remove(amp)
                if not amplifiers:
                    break

    return max_thruster
Beispiel #5
0
def part1(program):
    runner = IntcodeRunner(program)
    run = runner.run()
    grid = defaultdict(lambda: 0)
    x, y = (0, 0) #robot initial position
    direction = 'u' #robot initial direction

    while True: 
        try:
            next(run)
            color = run.send(grid[(x, y)])
            turn = run.send(None)
            grid[(x, y)] = color

            if turn: #turn right 90 degrees
                if direction == 'u':
                    direction = 'r'
                    x += 1
                elif direction == 'r':
                    direction = 'd'
                    y += 1
                elif direction == 'd':
                    direction = 'l'
                    x -= 1
                else:
                    direction = 'u'
                    y -= 1
            else:   #turn left 90 degrees
                if direction == 'u':
                    direction = 'l'
                    x -= 1
                elif direction == 'l':
                    direction = 'd'
                    y += 1
                elif direction == 'd':
                    direction = 'r'
                    x += 1
                else:
                    direction = 'u'
                    y -= 1

        except HaltExecution:
            break

    return len(grid.keys())
Beispiel #6
0
def part2(program, target=19690720):
    runner = IntcodeRunner(program)
    for noun in range(100, -1, -1):
        for verb in range(100):
            runner.set_mem(1, noun)
            runner.set_mem(2, verb)
            while True:
                try:
                    next(runner.run())
                except HaltExecution:
                    break

            if runner.get_mem(0) == target:
                return 100 * noun + verb

            runner.reset()
Beispiel #7
0
def part1(program, noun=12, verb=2):
    runner = IntcodeRunner(program)
    runner.set_mem(1, noun)
    runner.set_mem(2, verb)

    while True:
        try:
            next(runner.run())
        except HaltExecution:
            break

    return runner.get_mem(0)
Beispiel #8
0
def part1(program):
    max_thruster = -1
    for p in permutations([0, 1, 2, 3, 4]):
        amplifiers = [IntcodeRunner(program).run() for i in range(5)]
        for i, amp in zip(p, amplifiers):
            next(amp)
            amp.send(i)

        val = 0
        while True:
            try:
                for i, amp in enumerate(amplifiers):
                    val = amp.send(val)
                    if val and i == 4:
                        if max_thruster < val:
                            max_thruster = val
            except StopIteration:
                #amplifiers.remove(amp)
                pass
            except HaltExecution:
                break

    return max_thruster