Example #1
0
def main():
    program_text = next(open("input.txt"))

    computer = Computer(program_text, [2])

    output = computer.run([])
    print(output)
    while output is not None:
        output = computer.run([])
        print(output)
def part2():
    script_handler: JumpScriptHandler = JumpScriptHandler(False)
    instructions = INSTRUCTIONS.copy()
    computer: Computer = Computer(instructions,
                                  script_handler.handle_input,
                                  script_handler.handle_output)
    computer.execute()
def part1():
    instructions: [int] = INSTRUCTIONS
    cell: Cell = Cell()
    computer: Computer = Computer(instructions, lambda: 1,
                                  lambda output: cell.set_value(output))
    computer.execute()
    print(f'BOOST Test keycode: {cell.get_value()}')
def get_painted(start_position: int):
    instructions: [int] = INSTRUCTIONS.copy()
    cursor: Cursor = Cursor(start_position)
    computer: Computer = Computer(instructions, cursor.handle_input,
                                  cursor.handle_output)
    computer.execute()
    return cursor.painted
Example #5
0
def part2():
    instructions: [int] = INSTRUCTIONS.copy()
    instructions[0] = 2
    game: Game = Game()
    computer: Computer = Computer(instructions, game.handle_input, game.handle_output)
    computer.execute()
    print(f'Final score: {game.score}')
def get_shortest_path():
    instructions: [int] = INSTRUCTIONS.copy()
    computer: Computer = Computer(instructions, lambda: 0, cell.set_value)
    request: (InputInstruction, [int]) = computer.execute(True)
    path = Path({(0, 0)}, (0, 0), request[0], request[1])
    paths = {0: [path]}

    min_path_len = 0
    min_path = None
    while not min_path_len or min(paths) < min_path_len:
        next_length = min(paths)
        cursors = paths[next_length]
        del paths[next_length]

        for path in cursors:
            for next_path in path.get_next():
                next_len = len(next_path)
                if not next_path.done:
                    if next_len not in paths:
                        paths[next_len] = [next_path]
                    else:
                        paths[next_len].append(next_path)
                else:
                    if not min_path_len or min_path_len > next_len:
                        min_path_len = next_len
                        min_path = next_path
    return min_path
def part1():
    instructions: [int] = INSTRUCTIONS.copy()
    cell: Cell = Cell()
    computer: Computer = Computer(instructions, lambda: 1,
                                  lambda output: cell.set_value(output))
    computer.execute()
    print(f'Diagnostic Test Code: {cell.get_value()}')
def part1():
    beam: Beam = Beam(50)
    while not beam.done:
        instructions = INSTRUCTIONS.copy()
        computer: Computer = Computer(instructions, beam.handle_input,
                                      beam.handle_output)
        computer.execute()
    print(f'Pulling on: {len(beam.locations)} points')
Example #9
0
def get_board():
    board: Board = Board()
    instructions = INSTRUCTIONS.copy()
    computer: Computer = Computer(instructions,
                                  lambda: int(input('Please enter a number ')),
                                  board.fill)
    computer.execute()
    return board
Example #10
0
def part1():
    instructions: [int] = INSTRUCTIONS.copy()
    instructions[1] = 12
    instructions[2] = 2
    computer: Computer = Computer(instructions,
                                  lambda: int(input('Please enter a number')),
                                  lambda output: print(f'> {output}'))
    computer.execute()
    print(f'Value at position 0: {instructions[0]}')
def find_ship_beam_range(step_size: int, diagonal: int, start: int = 0) -> int:
    y = start
    while True:
        ship_beam: ShipBeam = ShipBeam(diagonal, y)
        while not ship_beam.done:
            instructions = INSTRUCTIONS.copy()
            computer: Computer = Computer(instructions, ship_beam.handle_input,
                                          ship_beam.handle_output)
            computer.execute()
        if ship_beam.fits:
            return y - step_size
        else:
            y += step_size
Example #12
0
def part2():
    for i in range(0, 100):
        for j in range(0, 100):
            instructions: [int] = INSTRUCTIONS.copy()
            instructions[1] = i
            instructions[2] = j
            computer: Computer = Computer(
                instructions, lambda: int(input('Please enter a number')),
                lambda output: print(f'> {output}'))
            computer.execute()
            if instructions[0] == 19690720:
                print(f'100 * {i} + {j} = {100 * i + j}')
                return
def part1():
    monitor: SignalMonitor = SignalMonitor()

    for permutation in itertools.permutations(range(5)):
        monitor.set_last_output(0)

        for setting in permutation:
            monitor.set_setting(setting)
            computer: Computer = Computer(INSTRUCTIONS.copy(), monitor.get_value,
                                          monitor.set_last_output)
            computer.execute()
            monitor.is_first = True
    print(monitor.highest)
def part2():
    diagonal = 100
    min_search = find_ship_beam_range(20, diagonal)
    total_minimum = find_ship_beam_range(1, diagonal, min_search) + 1

    ship_beam: ShipBeam = ShipBeam(diagonal, total_minimum + diagonal)
    while not ship_beam.done:
        instructions = INSTRUCTIONS.copy()
        computer: Computer = Computer(instructions, ship_beam.handle_input,
                                      ship_beam.handle_output)
        computer.execute()
    print(
        f'{diagonal} x {diagonal} square corner:'
        f' {min([coord[0] - 1 for coord in ship_beam.locations]) * 10_000 + total_minimum}'
    )
Example #15
0
def solve_part_one():
    best = 0
    io = ListBasedIO()
    com = Computer(io)
    for settings in unique_combinations([0, 1, 2, 3, 4]):

        prev_out = 0
        for i in settings:
            io.set_input([i, prev_out])
            com.load_program('program.txt')
            com.run()

            prev_out = io.outputs[0]

        if prev_out > best:
            best = prev_out

    print(best)
def part2():
    monitor: SignalMonitor = SignalMonitor()

    for permutation in itertools.permutations(range(5, 10)):
        monitor.set_last_output(0)

        computers: [Computer] = [Computer(INSTRUCTIONS.copy(), monitor.get_value,
                                          monitor.set_last_output) for _ in
                                 range(len(permutation))]
        inputs = {}
        start = True

        while len(inputs) > 0 or start:
            for computer in computers:
                request: (InputInstruction, [int])

                if start:
                    monitor.set_setting(permutation[computers.index(computer)])
                    request: (InputInstruction, [int]) = computer.execute(True)
                else:
                    request = inputs[computers.index(computer)]

                request[0].execute(request[1])
                next_request = computer.execute(True)

                if next_request:
                    inputs[computers.index(computer)] = next_request
                else:
                    del inputs[computers.index(computer)]

                if start:
                    monitor.is_first = True

            monitor.is_first = False
            start = False
        monitor.reset()
    print(f'Highest feedback loop signal: {monitor.highest}')
Example #17
0
def run_feedback_loop(settings: list, filename: str):
    ios = [ListBasedIO()] * 5
    computers = [Computer(ios[i]) for i in range(5)]

    for com in computers:
        com.load_program(filename)

    prev_output = 0

    for i in range(5):
        ios[i].set_input([settings[i], prev_output])
        computers[i].run()
        prev_output = ios[i].outputs[0]

    i = 0
    while not computers[4].is_halted:
        ios[i].set_input([prev_output])
        computers[i].run()
        prev_output = ios[i].outputs[0]
        i += 1
        if i > 4:
            i = 0

    return prev_output
Example #18
0
def handle_connect(router: Router, instructions):
    computer = Computer(instructions.copy(), router.handle_input,
                        router.handle_output)
    router.connect(computer)
    computer.execute()
Example #19
0
class DroidScriptHandler:

    def __init__(self):
        self.action = ''
        self.line = ''

    def handle_input(self) -> int:
        if self.action:
            character = self.action[0]
            self.action = self.action[1:]
            return ord(character)
        else:
            self.action = input('> ') + '\n'
            return self.handle_input()

    def handle_output(self, output: int):
        character = str(chr(output))
        if character != '\n':
            self.line += character
        else:
            print(self.line)
            self.line = ''


if __name__ == '__main__':
    instructions = INSTRUCTIONS.copy()
    droidScript: DroidScriptHandler = DroidScriptHandler()
    computer: Computer = Computer(instructions, droidScript.handle_input, droidScript.handle_output)
    computer.execute()
Example #20
0
def main():
    program_text = next(open("day7.txt"))

    largest_output = 0
    best_combination = None
    for a, b, c, d, e in itertools.permutations(range(5, 10)):
        print(a, b, c, d, e, end=' ')
        amp1 = Computer(program_text, [a])
        amp2 = Computer(program_text, [b])
        amp3 = Computer(program_text, [c])
        amp4 = Computer(program_text, [d])
        amp5 = Computer(program_text, [e])

        output5 = 0
        new_output5 = "something"
        while new_output5 is not None:
            output1 = amp1.run([output5])
            print(repr(output1), end=' ')
            output2 = amp2.run([output1])
            print(repr(output2), end=' ')
            output3 = amp3.run([output2])
            print(repr(output3), end=' ')
            output4 = amp4.run([output3])
            print(repr(output4), end=' ')
            new_output5 = amp5.run([output4])
            if new_output5 is not None:
                output5 = new_output5
            print(repr(output5))

        if output5 > largest_output:
            largest_output = output5
            best_combination = (a, b, c, d, e)

    print(largest_output)
    print(best_combination)
Example #21
0
def part1():
    instructions: [int] = INSTRUCTIONS.copy()
    game: Game = Game()
    computer: Computer = Computer(instructions, game.handle_input, game.handle_output)
    computer.execute()
    print(f'Block tiles: {game.count_type(2)}')