def test_immediate_mode(self):
        # immediate mode addition
        comp = incode_computer.IntcodeComputer([1002, 4, 3, 4, 33])
        comp.run()
        self.assertEqual([1002, 4, 3, 4, 99], comp.memory.dump())

        # immediate mode addition
        comp = incode_computer.IntcodeComputer([1101, 100, -1, 4, 0])
        comp.run()
        self.assertEqual([1101, 100, -1, 4, 99], comp.memory.dump())
    def test_jump(self):
        comp = incode_computer.IntcodeComputer(
            [3, 12, 6, 12, 15, 1, 13, 14, 13, 4, 13, 99, -1, 0, 1, 9])
        comp.run([0])
        self.assertEqual([0], comp.output_list)

        # immediate mode
        comp = incode_computer.IntcodeComputer(
            [3, 12, 6, 12, 15, 1, 13, 14, 13, 4, 13, 99, -1, 0, 1, 9])
        comp.run([8484734])
        self.assertEqual([1], comp.output_list)
    def test_large_numbers(self):
        # number with 16 digits
        comp = incode_computer.IntcodeComputer(
            [1102, 34915192, 34915192, 7, 4, 7, 99, 0])
        comp.run()
        self.assertTrue(
            10000000000000000 > comp.output_list[0] > 999999999999999,
            f"result was #{comp.output_list[0]}")

        # output the large number
        comp = incode_computer.IntcodeComputer([104, 1125899906842624, 99])
        comp.run()
        self.assertEqual(1125899906842624, comp.output_list[0])
    def test_input_output(self):
        # out the input
        comp = incode_computer.IntcodeComputer([3, 0, 4, 0, 99])
        comp.run([17])
        self.assertEqual([17], comp.output_list)

        # write relative param
        comp = incode_computer.IntcodeComputer([109, 15, 204, -11, 99])
        comp.run()
        self.assertEqual([99], comp.output_list)

        # read relative param
        comp = incode_computer.IntcodeComputer([109, 15, 203, -10, 104, 0, 99])
        comp.run([33])
        self.assertEqual([33], comp.output_list)
    def test_equlas(self):
        comp = incode_computer.IntcodeComputer(
            [3, 9, 8, 9, 10, 9, 4, 9, 99, -1, 8])
        comp.run([8])
        self.assertEqual([1], comp.output_list)

        comp = incode_computer.IntcodeComputer(
            [3, 9, 8, 9, 10, 9, 4, 9, 99, -1, 8])
        comp.run([7])
        self.assertEqual([0], comp.output_list)

        comp = incode_computer.IntcodeComputer(
            [3, 3, 1108, -1, 8, 3, 4, 3, 99])
        comp.run([18])
        self.assertEqual([0], comp.output_list)

        comp = incode_computer.IntcodeComputer(
            [3, 3, 1108, -1, 8, 3, 4, 3, 99])
        comp.run([8])
        self.assertEqual([1], comp.output_list)
    def test_relative_mode(self):
        # add to relative base
        mem = [109, 19, 99]
        comp = incode_computer.IntcodeComputer(mem.copy())
        comp.relative_base = 2000
        comp.run()
        self.assertEqual(2019, comp.relative_base)

        # subtract from relative base
        mem = [109, -19, 99]
        comp = incode_computer.IntcodeComputer(mem.copy())
        comp.relative_base = 2000
        comp.run()
        self.assertEqual(1981, comp.relative_base)

        # returns a copy of itself
        mem = [
            109, 1, 204, -1, 1001, 100, 1, 100, 1008, 100, 16, 101, 1006, 101,
            0, 99
        ]
        comp = incode_computer.IntcodeComputer(mem.copy())
        comp.run()
        self.assertEqual(mem, comp.output_list)
Beispiel #7
0
#     The computer's available memory should be much larger than the initial program. Memory beyond the initial program starts with the value 0 and can be read or written like any other memory. (It is invalid to try to access memory at a negative address, though.)
#     The computer should have support for large numbers. Some instructions near the beginning of the BOOST program will verify this capability.
#
# Here are some example programs that use these features:
#
#     109,1,204,-1,1001,100,1,100,1008,100,16,101,1006,101,0,99 takes no input and produces a copy of itself as output.
#     1102,34915192,34915192,7,4,7,99,0 should output a 16-digit number.
#     104,1125899906842624,99 should output the large number in the middle.
#
# The BOOST program will ask for a single input; run it in test mode by providing it the value 1. It will perform a series of checks on each opcode, output any opcodes (and the associated parameter modes) that seem to be functioning incorrectly, and finally output a BOOST keycode.
#
# Once your Intcode computer is fully functional, the BOOST program should report no malfunctioning opcodes when run in test mode; it should only output a single value, the BOOST keycode. What BOOST keycode does it produce?

input_1 = list(map(int, util.read_input("day9", ",")))

comp = incode_computer.IntcodeComputer(input_1)
comp.run([1])
print("Part One:", comp.output_list)

# --- Part Two ---
#
# You now have a complete Intcode computer.
#
# Finally, you can lock on to the Ceres distress signal! You just need to boost your sensors using the BOOST program.
#
# The program runs in sensor boost mode by providing the input instruction the value 2. Once run, it will boost the sensors automatically, but it might take a few seconds to complete the operation on slower hardware. In sensor boost mode, the program will output a single value: the coordinates of the distress signal.
#
# Run the BOOST program in sensor boost mode. What are the coordinates of the distress signal?

comp = incode_computer.IntcodeComputer(input_1)
comp.run([2])
Beispiel #8
0
# Try every combination of phase settings on the amplifiers. What is the highest signal that can be sent to the thrusters?

amps_count = 5
phase_setting_min = 0
phase_setting_max = 4

settings_list = itertools.permutations(
    range(phase_setting_min, phase_setting_max + 1), amps_count)

the_input = list(map(int, util.read_input("day7", ",")))

max_output = 0
for setting in settings_list:
    apm_input = 0
    for phase in setting:
        computer = incode_computer.IntcodeComputer(the_input)
        computer.run([phase, apm_input])
        apm_input = computer.output_list[-1]

    max_output = max(apm_input, max_output)

print("Part One:", max_output)

# --- Part Two ---
#
# It's no good - in this configuration, the amplifiers can't generate a large enough output signal to produce the thrust you'll need. The Elves quickly talk you through rewiring the amplifiers into a feedback loop:
#
#       O-------O  O-------O  O-------O  O-------O  O-------O
# 0 -+->| Amp A |->| Amp B |->| Amp C |->| Amp D |->| Amp E |-.
#    |  O-------O  O-------O  O-------O  O-------O  O-------O |
#    |                                                        |
 def test_multiplication(self):
     comp = incode_computer.IntcodeComputer([2, 3, 0, 3, 99])
     comp.run()
     self.assertEqual([2, 3, 0, 6, 99], comp.memory.dump())
Beispiel #10
0
#     It is important to remember that the instruction pointer should increase by the number of values in the instruction after the instruction finishes. Because of the new instructions, this amount is no longer always 4.
#     Integers can be negative: 1101,100,-1,4,0 is a valid program (find 100 + -1, store the result in position 4).
#
# The TEST diagnostic program will start by requesting from the user the ID of the system to test by running an input instruction - provide it 1, the ID for the ship's air conditioner unit.
#
# It will then perform a series of diagnostic tests confirming that various parts of the Intcode computer, like parameter modes, function correctly. For each test, it will run an output instruction indicating how far the result of the test was from the expected value, where 0 means the test was successful. Non-zero outputs mean that a function is not working correctly; check the instructions that were run before the output instruction to see which one failed.
#
# Finally, the program will output a diagnostic code and immediately halt. This final output isn't an error; an output followed immediately by a halt means the program finished. If all outputs were zero except the diagnostic code, the diagnostic program ran successfully.
#
# After providing 1 to the only input instruction and passing all the tests, what diagnostic code does the program produce?

the_input = list(map(int, util.read_input("day5", ",")))
the_output = []

# Input should be 1
the_output = incode_computer.IntcodeComputer(the_input).run([1])
print("Part One Finished")

# --- Part Two ---
#
# The air conditioner comes online! Its cold air feels good for a while, but then the TEST alarms start to go off. Since the air conditioner can't vent its heat anywhere but back into the spacecraft, it's actually making the air inside the ship warmer.
#
# Instead, you'll need to use the TEST to extend the thermal radiators. Fortunately, the diagnostic program (your puzzle input) is already equipped for this. Unfortunately, your Intcode computer is not.
#
# Your computer is only missing a few opcodes:
#
#     Opcode 5 is jump-if-true: if the first parameter is non-zero, it sets the instruction pointer to the value from the second parameter. Otherwise, it does nothing.
#     Opcode 6 is jump-if-false: if the first parameter is zero, it sets the instruction pointer to the value from the second parameter. Otherwise, it does nothing.
#     Opcode 7 is less than: if the first parameter is less than the second parameter, it stores 1 in the position given by the third parameter. Otherwise, it stores 0.
#     Opcode 8 is equals: if the first parameter is equal to the second parameter, it stores 1 in the position given by the third parameter. Otherwise, it stores 0.
#