コード例 #1
0
 def __init__(self, program):
     self.tiles = collections.defaultdict(int)
     self.interpretor = intcode.Interpretor(input_from=self.choose_move)
     self.program = program
     self.score = 0
     self.player_x = 0
     self.ball_x = 0
コード例 #2
0
 def __init__(self, program):
     self.program = program
     self.interpretor = intcode.Interpretor()
     self.rows = {
         0: (0, 1)
     }  # self.rows[y] = (start, end) of beam for row y
     self.find_start()
コード例 #3
0
 def __init__(self, program):
     self.program = program
     self.interpretor = intcode.Interpretor(input_from=self.check)
     self.panels = set()  # the set of white panels
     self.painted = set()  # the set of panels that were ever painted
     self.position = 0, 0
     self.facing = 0, -1  # up is negative
コード例 #4
0
    def __init__(self, program):
        self.interpretor = intcode.Interpretor()
        self.program_output = self.interpretor.run(program)

        self.pos = 0, 0
        self.station = None
        self.maze = {}
コード例 #5
0
def run_tests(program, input_constant):
    """Run the tests giving a certain input."""
    interpretor = intcode.Interpretor()
    interpretor.queue_input(input_constant)
    outputs = list(interpretor.run(program))
    if any(output != 0 for output in outputs[:-1]):
        print(f"WARNING: test failed")
    return outputs[-1]
コード例 #6
0
def run_droid(program, script, debug=False):
    """Execute a springdroid script, expected as a sequence of instruction strings."""
    interpretor = intcode.Interpretor()
    for instr in script:
        interpretor.queue_inputs(bytes(instr + "\n", "ascii"))
    if debug:
        print(bytes(interpretor.run(program)).decode("ascii"))
        return None
    return ilast(interpretor.run(program))
コード例 #7
0
 def run(self, nat, nics):
     """Run the controller in an infinite loop."""
     machine = intcode.Interpretor(input_from=self._get_input)
     machine.queue_input(self.address)
     for dest, packet_x, packet_y in machine.run(self.program, group=3):
         self.input_cum = 0
         self.idle_flag.clear()
         packet = packet_x, packet_y
         if dest == 255:
             nat.received.put(packet)
         else:
             nics[dest].received.put(packet)
コード例 #8
0
def part2(opcodes):
    """Solve for the answer to part 2."""
    interpretor = intcode.Interpretor()

    # assume the program is linear
    constant_term = run_prog(interpretor, opcodes, 0, 0)
    noun_term = run_prog(interpretor, opcodes, 1, 0) - constant_term
    verb_term = run_prog(interpretor, opcodes, 0, 1) - constant_term

    desired_output = 19690720
    verbs = [(desired_output - noun_term * noun - constant_term) / verb_term
             for noun in range(100)]
    verbs = [verb for verb in verbs if verb >= 0 and verb == int(verb)]
    noun, verb = len(verbs) - 1, int(verbs[-1])
    assert run_prog(interpretor, opcodes, noun, verb) == desired_output

    return 100 * noun + verb
コード例 #9
0
def part1(program):
    """Solve for the answer to part 1."""
    output_queue = []
    solver = Solver()
    interpretor = intcode.Interpretor()

    def ascii_input():
        description = bytes(output_queue).decode("ascii")
        output_queue.clear()
        command = solver.get_command(description)
        inputs = bytes(command + "\n", "ascii")
        interpretor.queue_inputs(inputs[1:])
        return inputs[0]

    interpretor.input_from = ascii_input
    for code in interpretor.run(program):
        output_queue.append(code)
    finish_text = bytes(output_queue).decode("ascii")
    return int(re.search(r"\d+", finish_text).group(0))
コード例 #10
0
def pipeline(program, amplifier_count, phase_settings, loop=False):
    """Run a signal that starts at 0 through a pipeline of amplifiers."""
    # Create the amplifiers
    amplifiers = [
        Amplifier(interpretor, output=interpretor.run(program))
        for _ in range(amplifier_count)
        for interpretor in [intcode.Interpretor()]
    ]
    # Setup phase settings
    for amplifier, phase_setting in zip(amplifiers, phase_settings):
        amplifier.interpretor.queue_input(phase_setting)
    # Run the signal through
    signal = 0
    curr = 0
    while curr < len(amplifiers):
        amplifiers[curr].interpretor.queue_input(signal)
        try:
            signal = next(amplifiers[curr].output)
        except StopIteration:
            break
        curr += 1
        if loop:
            curr = curr % len(amplifiers)
    return signal
コード例 #11
0
def part1(opcodes):
    """Solve for the answer to part 1."""
    return run_prog(intcode.Interpretor(), opcodes, 12, 2)