Пример #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)