Пример #1
0
def calculate_one_signal(program_code, amplifier_settings, initial_input):
    """ 
      Run the program X times where X is the number of settings in amplifier_settings
      Each run should have the input to the intcode machine set to [setting, X]
      On the first run, the X value is passed in initial_input
      After the first execution, the output value from the previous run becomes the X value 
    """
    value_X = initial_input
    for this_amplifier_setting in amplifier_settings:
        print(f"executing for {this_amplifier_setting},{value_X}")
        if has_cached_result(this_amplifier_setting, value_X):
            value_X = get_cached_result(this_amplifier_setting, value_X)
        else:
            c = ComputeEngine()
            c.load_memory(program_code)
            c.load_data([this_amplifier_setting, value_X])
            c.run(False)
            new_value_X = c.output_buffer[0]
            store_result_in_cache(this_amplifier_setting, value_X, new_value_X)
            value_X = new_value_X
    return value_X
Пример #2
0
#  999,1105,1,46,1101,1000,1,20,4,20,1105,1,46,98,99
#  The above example program uses an input instruction to ask for a single number. 
#  The program will then output 999 if the input value is below 8, output 1000 if the input value 
#  is equal to 8, or output 1001 if the input value is greater than 8.
#  
#  This time, when the TEST diagnostic program runs its input instruction to get the ID of the system 
#  to test, provide it 5, the ID for the ship's thermal radiator controller. This diagnostic test 
#  suite only outputs one number, the diagnostic code.
#  
#  What is the diagnostic code for system ID 5?
#  
#  

puzzle_input = '3,225,1,225,6,6,1100,1,238,225,104,0,1101,48,82,225,102,59,84,224,1001,224,-944,224,4,224,102,8,223,223,101,6,224,224,1,223,224,223,1101,92,58,224,101,-150,224,224,4,224,102,8,223,223,1001,224,3,224,1,224,223,223,1102,10,89,224,101,-890,224,224,4,224,1002,223,8,223,1001,224,5,224,1,224,223,223,1101,29,16,225,101,23,110,224,1001,224,-95,224,4,224,102,8,223,223,1001,224,3,224,1,223,224,223,1102,75,72,225,1102,51,8,225,1102,26,16,225,1102,8,49,225,1001,122,64,224,1001,224,-113,224,4,224,102,8,223,223,1001,224,3,224,1,224,223,223,1102,55,72,225,1002,174,28,224,101,-896,224,224,4,224,1002,223,8,223,101,4,224,224,1,224,223,223,1102,57,32,225,2,113,117,224,101,-1326,224,224,4,224,102,8,223,223,101,5,224,224,1,223,224,223,1,148,13,224,101,-120,224,224,4,224,1002,223,8,223,101,7,224,224,1,223,224,223,4,223,99,0,0,0,677,0,0,0,0,0,0,0,0,0,0,0,1105,0,99999,1105,227,247,1105,1,99999,1005,227,99999,1005,0,256,1105,1,99999,1106,227,99999,1106,0,265,1105,1,99999,1006,0,99999,1006,227,274,1105,1,99999,1105,1,280,1105,1,99999,1,225,225,225,1101,294,0,0,105,1,0,1105,1,99999,1106,0,300,1105,1,99999,1,225,225,225,1101,314,0,0,106,0,0,1105,1,99999,8,677,226,224,102,2,223,223,1006,224,329,101,1,223,223,107,677,677,224,1002,223,2,223,1006,224,344,101,1,223,223,8,226,677,224,102,2,223,223,1006,224,359,101,1,223,223,107,226,226,224,102,2,223,223,1005,224,374,1001,223,1,223,1108,677,226,224,1002,223,2,223,1006,224,389,101,1,223,223,107,677,226,224,102,2,223,223,1006,224,404,1001,223,1,223,1107,226,677,224,1002,223,2,223,1006,224,419,1001,223,1,223,108,677,677,224,102,2,223,223,1005,224,434,1001,223,1,223,1008,677,226,224,1002,223,2,223,1006,224,449,1001,223,1,223,7,226,677,224,1002,223,2,223,1006,224,464,1001,223,1,223,1007,677,677,224,102,2,223,223,1005,224,479,1001,223,1,223,1007,226,226,224,1002,223,2,223,1005,224,494,1001,223,1,223,108,226,226,224,1002,223,2,223,1005,224,509,1001,223,1,223,1007,226,677,224,1002,223,2,223,1006,224,524,101,1,223,223,1107,677,677,224,102,2,223,223,1005,224,539,101,1,223,223,1107,677,226,224,102,2,223,223,1005,224,554,1001,223,1,223,108,677,226,224,1002,223,2,223,1006,224,569,1001,223,1,223,1108,226,677,224,1002,223,2,223,1006,224,584,101,1,223,223,8,677,677,224,1002,223,2,223,1006,224,599,1001,223,1,223,1008,226,226,224,102,2,223,223,1006,224,614,101,1,223,223,7,677,677,224,1002,223,2,223,1006,224,629,101,1,223,223,1008,677,677,224,102,2,223,223,1005,224,644,101,1,223,223,7,677,226,224,1002,223,2,223,1005,224,659,101,1,223,223,1108,226,226,224,102,2,223,223,1006,224,674,1001,223,1,223,4,223,99,226'

import os 
import sys 
sys.path.append("c:\\development\\advent-of-code-2019")
print(sys.path)

from compute_engine.engine import ComputeEngine

c = ComputeEngine()
c.load_memory(puzzle_input)
c.load_data([5])
c.run(True)




igen = [InstructionLooper() for _ in range(max_instructions)]

while (True):
    # increment the instructions
    i = 0
    while igen[i].increment():
        i += 1
    # generate the command
    instructions = [igen[i].output() for i in range(max_instructions)]
    combined = "".join(instructions)
    combined += "RUN\n"
    print(combined.replace('\n', ','))
    # execute the command, see if we win..

    # Build the ASCII Controller
    controller = AsciiController()
    controller.set_output(False)

    #spring_script = 'NOT B T\nOR T J\nNOT C T\nOR T J\nAND D J\nNOT A T\nOR T J\nWALK\n'
    #spring_script = 'NOT B T\nOR T J\nNOT C T\nOR T J\nAND D J\nNOT A T\nOR T J\nRUN\n'

    controller.set_input(combined)

    # build the computer and attach the screen
    c = ComputeEngine(program_code)
    # attach the controls
    c.attach_output_device(controller)
    c.attach_input_device(controller)
    c.run(False, False)
Пример #4
0
class AsciiAdventureBot:
    def __init__(self, program_code):
        """
        Configure an intcode machine for ASCII input to play Adventure - yay ! 
        """
        self.compute = ComputeEngine(program_code)
        self.compute.attach_input_device(self)
        self.compute.attach_output_device(self)
        self.current_output = ''
        self.input = ''
        self.alert = None

    def run(self):
        """Start the program"""
        self.compute.run(False, False, False)

    def receive_output(self, the_output):
        """ IntCode interface, the program is outputting an ASCII character """
        if 10 == the_output:
            print(f"{self.current_output}")
            if 'Alert!' in self.current_output:
                this_alert = self.current_output
                this_alert = this_alert.replace('A loud, robotic voice says',
                                                '')
                this_alert = this_alert.replace(
                    'and you are ejected back to the checkpoint', '')
                #this_combo = self.all_object_combos[self.this_combo_index]
                #self.combo_results[this_combo] = this_alert
                #print(f"Combo({this_combo}) -> {this_alert}")
            self.current_output = ''
        else:
            self.current_output += chr(the_output)

    def create_potential_item_combos(self, the_items):
        """
        Store all potential combinations of the items provided
        """
        result = list()
        for combo_length in range(1, len(the_items) + 1):
            this_set_of_combos = list(combinations(the_items, combo_length))
            result.extend(this_set_of_combos)
        # store the objects for later use
        self.all_object_combos = result
        self.this_combo_index = -1
        self.combo_results = dict()

    def provide_input(self):
        """
        Provide ASCII values as input
        """

        # not having the interactive session for now..
        if False:
            while 0 == len(self.input):
                new_input = input('input>').strip()
                if '' != new_input:
                    self.input += new_input + '\n'
        else:
            if 0 == len(self.input):
                # now create the commands for the next one of the sequences
                self.this_combo_index += 1
                print(
                    f"Combo will be ({self.all_object_combos[self.this_combo_index]}"
                )
                script = create_commands(
                    self.all_object_combos[self.this_combo_index])
                self.input += script

        # and now send the next input character
        result = ord(self.input[0])
        self.input = self.input[1:]
        return result

    def append_command(self, command):
        """
        Add a pre-defined command to the buffer 
        """
        self.input += command + '\n'
Пример #5
0
# seems ok for now..
test_chars = "#.0123456789LR\n"
for c in test_chars:
    print(f"ascii for {c} is {ascii_for(c)}")

puzzle_program = '1,330,331,332,109,3974,1102,1182,1,15,1101,1475,0,24,1001,0,0,570,1006,570,36,101,0,571,0,1001,570,-1,570,1001,24,1,24,1106,0,18,1008,571,0,571,1001,15,1,15,1008,15,1475,570,1006,570,14,21102,1,58,0,1105,1,786,1006,332,62,99,21101,333,0,1,21102,73,1,0,1105,1,579,1102,0,1,572,1101,0,0,573,3,574,101,1,573,573,1007,574,65,570,1005,570,151,107,67,574,570,1005,570,151,1001,574,-64,574,1002,574,-1,574,1001,572,1,572,1007,572,11,570,1006,570,165,101,1182,572,127,1002,574,1,0,3,574,101,1,573,573,1008,574,10,570,1005,570,189,1008,574,44,570,1006,570,158,1105,1,81,21102,340,1,1,1105,1,177,21101,0,477,1,1106,0,177,21102,1,514,1,21101,0,176,0,1106,0,579,99,21102,184,1,0,1105,1,579,4,574,104,10,99,1007,573,22,570,1006,570,165,1002,572,1,1182,21101,0,375,1,21101,0,211,0,1105,1,579,21101,1182,11,1,21102,1,222,0,1106,0,979,21102,1,388,1,21102,1,233,0,1106,0,579,21101,1182,22,1,21101,0,244,0,1105,1,979,21101,0,401,1,21102,1,255,0,1106,0,579,21101,1182,33,1,21102,1,266,0,1106,0,979,21102,414,1,1,21102,277,1,0,1105,1,579,3,575,1008,575,89,570,1008,575,121,575,1,575,570,575,3,574,1008,574,10,570,1006,570,291,104,10,21102,1182,1,1,21102,1,313,0,1106,0,622,1005,575,327,1101,1,0,575,21101,327,0,0,1105,1,786,4,438,99,0,1,1,6,77,97,105,110,58,10,33,10,69,120,112,101,99,116,101,100,32,102,117,110,99,116,105,111,110,32,110,97,109,101,32,98,117,116,32,103,111,116,58,32,0,12,70,117,110,99,116,105,111,110,32,65,58,10,12,70,117,110,99,116,105,111,110,32,66,58,10,12,70,117,110,99,116,105,111,110,32,67,58,10,23,67,111,110,116,105,110,117,111,117,115,32,118,105,100,101,111,32,102,101,101,100,63,10,0,37,10,69,120,112,101,99,116,101,100,32,82,44,32,76,44,32,111,114,32,100,105,115,116,97,110,99,101,32,98,117,116,32,103,111,116,58,32,36,10,69,120,112,101,99,116,101,100,32,99,111,109,109,97,32,111,114,32,110,101,119,108,105,110,101,32,98,117,116,32,103,111,116,58,32,43,10,68,101,102,105,110,105,116,105,111,110,115,32,109,97,121,32,98,101,32,97,116,32,109,111,115,116,32,50,48,32,99,104,97,114,97,99,116,101,114,115,33,10,94,62,118,60,0,1,0,-1,-1,0,1,0,0,0,0,0,0,1,50,26,0,109,4,1201,-3,0,587,20102,1,0,-1,22101,1,-3,-3,21102,0,1,-2,2208,-2,-1,570,1005,570,617,2201,-3,-2,609,4,0,21201,-2,1,-2,1106,0,597,109,-4,2106,0,0,109,5,2101,0,-4,630,20101,0,0,-2,22101,1,-4,-4,21101,0,0,-3,2208,-3,-2,570,1005,570,781,2201,-4,-3,653,20101,0,0,-1,1208,-1,-4,570,1005,570,709,1208,-1,-5,570,1005,570,734,1207,-1,0,570,1005,570,759,1206,-1,774,1001,578,562,684,1,0,576,576,1001,578,566,692,1,0,577,577,21102,702,1,0,1106,0,786,21201,-1,-1,-1,1106,0,676,1001,578,1,578,1008,578,4,570,1006,570,724,1001,578,-4,578,21102,731,1,0,1105,1,786,1106,0,774,1001,578,-1,578,1008,578,-1,570,1006,570,749,1001,578,4,578,21101,756,0,0,1105,1,786,1106,0,774,21202,-1,-11,1,22101,1182,1,1,21102,774,1,0,1105,1,622,21201,-3,1,-3,1106,0,640,109,-5,2105,1,0,109,7,1005,575,802,20102,1,576,-6,20101,0,577,-5,1105,1,814,21101,0,0,-1,21101,0,0,-5,21101,0,0,-6,20208,-6,576,-2,208,-5,577,570,22002,570,-2,-2,21202,-5,51,-3,22201,-6,-3,-3,22101,1475,-3,-3,2102,1,-3,843,1005,0,863,21202,-2,42,-4,22101,46,-4,-4,1206,-2,924,21102,1,1,-1,1105,1,924,1205,-2,873,21102,1,35,-4,1106,0,924,1202,-3,1,878,1008,0,1,570,1006,570,916,1001,374,1,374,1201,-3,0,895,1101,0,2,0,2101,0,-3,902,1001,438,0,438,2202,-6,-5,570,1,570,374,570,1,570,438,438,1001,578,558,921,21002,0,1,-4,1006,575,959,204,-4,22101,1,-6,-6,1208,-6,51,570,1006,570,814,104,10,22101,1,-5,-5,1208,-5,49,570,1006,570,810,104,10,1206,-1,974,99,1206,-1,974,1102,1,1,575,21101,973,0,0,1105,1,786,99,109,-7,2105,1,0,109,6,21101,0,0,-4,21102,0,1,-3,203,-2,22101,1,-3,-3,21208,-2,82,-1,1205,-1,1030,21208,-2,76,-1,1205,-1,1037,21207,-2,48,-1,1205,-1,1124,22107,57,-2,-1,1205,-1,1124,21201,-2,-48,-2,1105,1,1041,21102,1,-4,-2,1106,0,1041,21101,0,-5,-2,21201,-4,1,-4,21207,-4,11,-1,1206,-1,1138,2201,-5,-4,1059,2102,1,-2,0,203,-2,22101,1,-3,-3,21207,-2,48,-1,1205,-1,1107,22107,57,-2,-1,1205,-1,1107,21201,-2,-48,-2,2201,-5,-4,1090,20102,10,0,-1,22201,-2,-1,-2,2201,-5,-4,1103,2102,1,-2,0,1106,0,1060,21208,-2,10,-1,1205,-1,1162,21208,-2,44,-1,1206,-1,1131,1106,0,989,21101,0,439,1,1105,1,1150,21101,477,0,1,1106,0,1150,21102,1,514,1,21102,1,1149,0,1105,1,579,99,21101,1157,0,0,1106,0,579,204,-2,104,10,99,21207,-3,22,-1,1206,-1,1138,2101,0,-5,1176,2102,1,-4,0,109,-6,2106,0,0,32,5,46,1,3,1,46,1,3,1,46,1,3,1,46,1,3,1,46,1,3,1,40,11,40,1,5,1,44,1,5,1,44,1,5,1,44,1,5,1,44,1,5,1,40,11,40,1,3,1,46,1,3,1,46,1,3,1,46,1,3,9,38,1,11,1,26,1,11,1,11,1,26,1,11,1,11,1,26,1,11,5,7,1,26,1,15,1,7,1,26,1,15,1,7,1,26,1,15,1,7,1,26,1,11,7,5,1,26,1,11,1,3,1,1,1,5,1,26,1,9,13,1,1,3,13,10,1,9,1,1,1,3,1,1,1,3,1,1,1,3,1,22,5,5,1,1,13,3,1,26,1,5,1,5,1,1,1,3,1,5,1,26,1,5,1,5,7,5,1,26,1,5,1,7,1,9,1,26,1,5,1,7,1,9,1,26,1,5,1,7,1,9,1,26,1,5,1,7,11,26,1,5,1,34,11,5,1,34,1,15,1,34,1,5,9,1,9,26,1,5,1,7,1,9,1,26,1,5,1,7,1,9,1,26,1,5,1,7,1,9,1,22,11,7,1,9,1,22,1,3,1,13,1,9,1,22,1,3,1,13,1,9,1,22,1,3,1,13,1,9,1,22,1,3,1,13,1,9,1,22,1,3,1,13,1,9,1,22,5,13,11,22'

# Build the ASCII Controller
controller = AsciiController()

# build the computer and attach the screen
compute = ComputeEngine(puzzle_program)
# attach the controls
compute.attach_output_device(controller)
#c.attach_input_device(solver)
compute.run(False, False)

controller.print()

alignment = controller.calculate_alignment_parameters()

controller.print()

print(f"Alignment is : {alignment}")

# ok, so let's setup the computer and try a trial run..
compute = ComputeEngine(puzzle_program)
# turn on the robot
compute.poke(0, 2)
# give it some input