Beispiel #1
0
 def __init__(self, init_mem):
     self.computer = IntCodeComputer(init_mem, [])
     self.pos = (0, 0)
     self.direction = (0, -1)
     self.painted_panels = {}
     # initial panel is white
     self.paint_panel(1)
Beispiel #2
0
class Robot:
    def __init__(self, init_mem):
        self.computer = IntCodeComputer(init_mem, [])
        self.pos = (0, 0)
        self.direction = (0, -1)
        self.painted_panels = {}
    
    def get_panel_colour(self):
        return self.painted_panels.get(self.pos, 0) # painted colour or black if never painted
    
    def paint_panel(self, colour):
        self.painted_panels[self.pos] = colour

    def run(self):
        self.computer.inputs = [self.get_panel_colour()]
        self.computer.run()
        if self.computer.is_halted():
            return False
        else:
            [new_colour, new_direction] = self.computer.outputs
            self.paint_panel(new_colour)
            (xd, yd) = self.direction
            if new_direction == 0:
                # turn left
                self.direction = (yd, -xd)
            elif new_direction == 1:
                # turn right
                self.direction = (-yd, xd)
            else:
                raise Exception("Unrecognised direction code")
            (x, y) = self.pos
            (xd, yd) = self.direction
            self.pos = (x + xd, y + yd)
            return True
Beispiel #3
0
 def __init__(self):
     self.comp = IntCodeComputer()
     A = Amplifier()
     B = Amplifier()
     C = Amplifier()
     D = Amplifier()
     E = Amplifier()
     self.amps = (A, B, C, D, E)
Beispiel #4
0
    def __init__(self):

        self.fuelSystem = FuelSystem()
        self.intCodeComputer = IntCodeComputer()
        self.wiring = Wiring()
        self.pwAnalyzer = PasswordAnalyzer()
        self.orbitMapper = OrbitMapper()
        self.ampPhaser = AmplifierPhaser()
Beispiel #5
0
class Robot:
    def __init__(self, init_mem):
        self.computer = IntCodeComputer(init_mem, [])
        self.pos = (0, 0)
        self.direction = (0, -1)
        self.painted_panels = {}
        # initial panel is white
        self.paint_panel(1)

    def get_panel_colour(self, position):
        return self.painted_panels.get(
            position, 0)  # painted colour or black if never painted

    def paint_panel(self, colour):
        self.painted_panels[self.pos] = colour

    def run(self):
        self.computer.inputs = [self.get_panel_colour(self.pos)]
        self.computer.run()
        if self.computer.is_halted():
            return False
        else:
            [new_colour, new_direction] = self.computer.outputs
            self.paint_panel(new_colour)
            (xd, yd) = self.direction
            if new_direction == 0:
                # turn left
                self.direction = (yd, -xd)
            elif new_direction == 1:
                # turn right
                self.direction = (-yd, xd)
            else:
                raise Exception("Unrecognised direction code")
            (x, y) = self.pos
            (xd, yd) = self.direction
            self.pos = (x + xd, y + yd)
            return True

    def print_panel_paint_pattern(self):
        (x_min, y_min, x_max, y_max) = (0, 0, 0, 0)
        for (x, y) in self.painted_panels:
            if x < x_min:
                x_min = x
            elif x > x_max:
                x_max = x
            if y < y_min:
                y_min = y
            elif y > y_max:
                y_max = y
        for y in range(y_min, y_max + 1):
            l = ""
            for x in range(x_min, x_max + 1):
                l += "XX" if self.get_panel_colour((x, y)) else "  "
            print(l)
Beispiel #6
0
def main():

    the_map = Map()
    the_map.set_paint(Color.WHITE)
    cpu = IntCodeComputer(scan_program_from_input(), MapIO(the_map))

    cpu.run()

    print(len(the_map.painted_panels))

    the_map.print()
Beispiel #7
0
    def test_intCodeCalculator(self):
        # Initialize an intcode computer instance
        intCodeComp = IntCodeComputer()

        # Test the example int codes and verify the results
        intcode = [1, 0, 0, 0, 99]
        self.assertEqual(intCodeComp.compute(intcode), [2, 0, 0, 0, 99])

        intcode = [2, 3, 0, 3, 99]
        self.assertEqual(intCodeComp.compute(intcode), [2, 3, 0, 6, 99])

        intcode = [2, 4, 4, 5, 99, 0]
        self.assertEqual(intCodeComp.compute(intcode), [2, 4, 4, 5, 99, 9801])

        intcode = [1, 1, 1, 4, 99, 5, 6, 0, 99]
        self.assertEqual(intCodeComp.compute(intcode), [30, 1, 1, 4, 2, 5, 6, 0, 99])
Beispiel #8
0
class AmplifierPhaser:
    def __init__(self):
        self.comp = IntCodeComputer()
        A = Amplifier()
        B = Amplifier()
        C = Amplifier()
        D = Amplifier()
        E = Amplifier()
        self.amps = (A, B, C, D, E)

    def findMaxSignalSetting(self, program, minPhase, maxPhase):
        settings = self.phaseSettings(minPhase, maxPhase)
        maxOutput = 0

        for s in settings:
            output = self.run(program, s)
            if output > maxOutput:
                maxOutput = output

        return maxOutput

    def run(self, program, phaseSettings):
        phasePointer = 0

        for i, amp in enumerate(self.amps):
            amp.output = self.comp.computeOut(
                program, input=[phaseSettings[phasePointer], amp.input])[0]

            if i + 1 < len(self.amps):
                self.amps[i + 1].input = amp.output
                phasePointer += 1
            else:
                return amp.output

    def phaseSettings(self, start, end):
        r = range(start, end + 1)
        return list(permutations(r, len(r)))
Beispiel #9
0
class Starship:
    def __init__(self):

        self.fuelSystem = FuelSystem()
        self.intCodeComputer = IntCodeComputer()
        self.wiring = Wiring()
        self.pwAnalyzer = PasswordAnalyzer()
        self.orbitMapper = OrbitMapper()
        self.ampPhaser = AmplifierPhaser()

    def computeRequiredFuel(self, modulesMassData):
        """
        Computes and returns the required amount of fuel for the modules 
        """

        return self.fuelSystem.computeRequiredFuel(modulesMassData)

    def computeIntCodeResults(self, intCodeFile, a=12, b=2):
        intCode = open(intCodeFile).readline()
        intCode = intCode.strip('\n').split(',')

        return self.intCodeComputer.compute(intCode, a, b)

    def runIntCodeDiagnostics(self, intCodeFile, id):
        intCode = open(intCodeFile).readline()
        intCode = intCode.strip('\n').split(',')

        return self.intCodeComputer.diagnostics(intCode, id)

    def findClosestWiringIntersection(self, wiringData):
        self.wiring.loadWiringData(wiringData)

        return self.wiring.analyzeWires()

    def validPasswordCombinations(self, start, end):
        """
        Returns the number of valid password combinations within the given start to end range
        """
        return self.pwAnalyzer.validCombinations(start, end)

    def downloadOrbitMap(self, mapFile):
        mapData = open(mapFile).readlines()
        mapData = [p.strip('\n') for p in mapData]

        self.orbitMapper.loadMap(mapData)
        return self.orbitMapper.orbitCountChecksum()

    def findJumpDistance(self, start, end):
        return self.orbitMapper.jumpDistance(start, end)

    def findMaxThrusterSignal(self, ampControllerFile, minPhase, maxPhase):
        ampControllerData = open(ampControllerFile).readline()
        ampControlProgram = ampControllerData.strip('\n').split(',')

        return self.ampPhaser.findMaxSignalSetting(ampControlProgram, minPhase,
                                                   maxPhase)

    def imageChecksum(self, imageFile, width, height):
        decoder = SpaceImageFormatDecoder(imageFile)
        return decoder.checksum(width, height)

    def decodeBiosPassword(self, imageFile, width, height):
        decoder = SpaceImageFormatDecoder(imageFile)
        return decoder.decode(width, height)
Beispiel #10
0
from computer import IntCodeComputer
from itertools import permutations
path = 'input.txt'

## part one
with open(path, "r") as f:
    inputs = [int(element) for element in f.read().split(',')]
best = 0
for setting in permutations([0, 1, 2, 3, 4]):
    signal = 0
    for phase in setting:
        computer = IntCodeComputer(inputs[:])
        computer.add_input(phase)
        computer.add_input(signal)
        computer.run()
        signal = computer.get_output()
    
    if signal > best:
        best = signal


print(best) # 17440

## part 2


best = 0
for setting in permutations([5, 6, 7, 8, 9]):
    with open(path, "r") as f:
        inputs = [int(element) for element in f.read().split(',')]
    computers = [IntCodeComputer(inputs[:]) for _ in range(5)]
Beispiel #11
0
def main():
    screen = Screen()
    cpu = IntCodeComputer(scan_program_from_input(), ArcadeIO(screen))
    cpu.hard_set_memory_cell(0, 2)

    cpu.run()
Beispiel #12
0
    def test_diagnostics(self):
        intCodeComp = IntCodeComputer()

        # Position mode - Input equal to 8 outputs 1, else 0        
        program = [3,9,8,9,10,9,4,9,99,-1,8]
        self.assertEqual(intCodeComp.diagnostics(program, 8), [1])
        self.assertEqual(intCodeComp.diagnostics(program, 0), [0])

        # Position mode - Input less than 8 outputs 1, else 0
        program = [3,9,7,9,10,9,4,9,99,-1,8]
        self.assertEqual(intCodeComp.diagnostics(program, 2), [1])
        self.assertEqual(intCodeComp.diagnostics(program, 8), [0])

        # Immediate mode - Input equal to 8 outputs 1, else 0
        program = [3,3,1108,-1,8,3,4,3,99]
        self.assertEqual(intCodeComp.diagnostics(program, 8), [1])
        self.assertEqual(intCodeComp.diagnostics(program, 0), [0])

        # Immediate mode - Input less than 8 outputs 1, else 0
        program = [3,3,1107,-1,8,3,4,3,99]
        self.assertEqual(intCodeComp.diagnostics(program, 6), [1])
        self.assertEqual(intCodeComp.diagnostics(program, 9), [0])

        # Jump tests, position mode
        program = [3,12,6,12,15,1,13,14,13,4,13,99,-1,0,1,9]
        self.assertEqual(intCodeComp.diagnostics(program, 0), [0])
        self.assertEqual(intCodeComp.diagnostics(program, 5), [1])
        self.assertEqual(intCodeComp.diagnostics(program, -1), [1])

        # Jump tests, immediate mode
        program = [3,3,1105,-1,9,1101,0,0,12,4,12,99,1]
        self.assertEqual(intCodeComp.diagnostics(program, 0), [0])
        self.assertEqual(intCodeComp.diagnostics(program, 5), [1])
        self.assertEqual(intCodeComp.diagnostics(program, -1), [1])

        program = [3,21,1008,21,8,20,1005,20,22,107,8,21,20,1006,20,31,
                    1106,0,36,98,0,0,1002,21,125,20,4,20,1105,1,46,104,
                    999,1105,1,46,1101,1000,1,20,4,20,1105,1,46,98,99]

        # ID < 8
        self.assertEqual(intCodeComp.diagnostics(program, 0), [999])
        self.assertEqual(intCodeComp.diagnostics(program, 7), [999])

        # ID == 8
        self.assertEqual(intCodeComp.diagnostics(program, 8), [1000])

        # ID > 8
        self.assertEqual(intCodeComp.diagnostics(program, 9), [1001])
        self.assertEqual(intCodeComp.diagnostics(program, 10), [1001])
Beispiel #13
0
from computer import IntCodeComputer

## Part one
HEAP_SIZE = 100000
with open("input.txt", "r") as f:
    inputs = [int(element) for element in f.read().split(',')
              ] + [0 for _ in range(HEAP_SIZE)]

computer = IntCodeComputer(inputs[:])
computer.add_input(1)
computer.run()
print(computer.get_output())  # 2662308295

## Part two

computer = IntCodeComputer(inputs[:])
computer.add_input(2)
computer.run()
print(computer.get_output())  # 63441