Example #1
0
def checkCoordinates(program, x, y):
    input_handler = InputModule(x, y)
    output_handler = OutputModule()
    intcodeComputer = IntcodeComputer(program,
                                      input_handler=input_handler,
                                      output_handler=output_handler)
    intcodeComputer.run()
    result = output_handler.result
    return result
Example #2
0
    def moveRobot(puzzle_input, path):
        # transform path into series of movements
        turn = path[0][1]
        steps = 0
        move_commands = []
        for i, node in enumerate(path[1:]):
            if node[1] != Turn.NONE or i == len(path) - 2:
                if i == len(path) - 2:
                    steps += 1
                # starting new segment
                move_commands.append(('R' if turn == Turn.RIGHT else 'L') +
                                     str(steps))
                steps = 1
                turn = node[1]
            else:
                steps += 1

        # look for patterns, decide on A, B, and C routines
        patterns = []
        choosePatterns(move_commands, 0, patterns, 3)

        # decide what order patterns should go in
        start_index = 0
        pattern_order = []
        while start_index < len(move_commands):
            found_pattern = False
            sub_commands = move_commands[start_index:]
            for pattern_index, pattern in enumerate(patterns):
                if len(pattern) <= len(sub_commands) and sub_commands[:len(
                        pattern)] == pattern:
                    pattern_order.append(pattern_index)
                    start_index += len(pattern)
                    found_pattern = True
                    break
            if not found_pattern:
                raise Exception(
                    "Unable to find matching pattern for commands {} starting at {} with patterns {}"
                    .format(move_commands, start_index, patterns))

        # generate actual input
        robot_input = []
        robot_input.append(','.join([chr(i + 65) for i in pattern_order]))
        for pattern in patterns:
            robot_input.append(getFinalCommandForMovementPattern(pattern))
        robot_input = '\n'.join(robot_input) + '\nn\n'
        print(robot_input)

        # RERUN!
        puzzle_input = puzzle_input.strip()
        output = Output()
        intcodeComputer = IntcodeComputer(puzzle_input,
                                          input_handler=Input(robot_input),
                                          output_handler=output)
        intcodeComputer.memory[0] = 2
        intcodeComputer.run()
        path = output.getPathOfScaffold()
Example #3
0
def walkTheRegion(input_string, space):
    stdscr = curses.initscr()
    game = Game(space)
    intcodeComputer = IntcodeComputer(input_string,
                                      input_handler=InputModule(game, stdscr),
                                      output_handler=OutputModule(
                                          game, stdscr, 39, 13))
    intcodeComputer.run()
    curses.endwin()
    return game
Example #4
0
def solve(input):
    print("TEST ID 1")
    intcodeComputer = IntcodeComputer(input)
    output = intcodeComputer.run(input_values=[1])
    print("diagnostic code: " + str(output))

    print("TEST ID 5")
    intcodeComputer = IntcodeComputer(input)
    output = intcodeComputer.run(input_values=[5])
    print("diagnostic code: " + str(output))
    return
Example #5
0
class Amplifier:
    def __init__(self, input_string, phase, return_on_output=False):
        self.intcode_computer = IntcodeComputer(input_string)
        self.phase = phase
        self.return_on_output = return_on_output

    def run(self, input=0):
        return self.intcode_computer.run(
            [self.phase, input], return_on_output=self.return_on_output)
def paint(space, input_string, starting_white=False):
    panels_painted = 0
    x = 0
    y = 0
    direction = Direction.UP

    intcodeComputer = IntcodeComputer(input_string)

    while (True):
        if (x, y) not in space:
            space[(x, y)] = starting_white
            panels_painted += 1
        current_color = int(space[(x, y)])

        white = bool(
            intcodeComputer.run([current_color], return_on_output=True))
        right = bool(
            intcodeComputer.run([current_color], return_on_output=True))

        space[(x, y)] = white

        # decide where to go next
        if direction == Direction.UP:
            direction = Direction.RIGHT if right else Direction.LEFT
        elif direction == Direction.RIGHT:
            direction = Direction.DOWN if right else Direction.UP
        elif direction == Direction.DOWN:
            direction = Direction.LEFT if right else Direction.RIGHT
        elif direction == Direction.LEFT:
            direction = Direction.UP if right else Direction.DOWN

        if direction == Direction.UP:
            y -= 1
        elif direction == Direction.RIGHT:
            x += 1
        elif direction == Direction.DOWN:
            y += 1
        elif direction == Direction.LEFT:
            x -= 1

        if intcodeComputer.halted:
            break
    def test(self):
        # position mode
        intcodeComputer = IntcodeComputerV2("3,9,8,9,10,9,4,9,99,-1,8")
        self.assertEqual(0, intcodeComputer.run([7]))
        intcodeComputer = IntcodeComputerV2("3,9,8,9,10,9,4,9,99,-1,8")
        self.assertEqual(1, intcodeComputer.run([8]))
        intcodeComputer = IntcodeComputerV2("3,9,8,9,10,9,4,9,99,-1,8")
        self.assertEqual(0, intcodeComputer.run([9]))

        # immediate mode
        intcodeComputer = IntcodeComputerV2("3,3,1108,-1,8,3,4,3,99")
        self.assertEqual(0, intcodeComputer.run([7]))
        intcodeComputer = IntcodeComputerV2("3,3,1108,-1,8,3,4,3,99")
        self.assertEqual(1, intcodeComputer.run([8]))
        intcodeComputer = IntcodeComputerV2("3,3,1108,-1,8,3,4,3,99")
        self.assertEqual(0, intcodeComputer.run([9]))
Example #8
0
def solve(input):

    intcodeComputer = IntcodeComputer(input, noun=12, verb=2)

    # part 1
    intcodeComputer.run()
    print(intcodeComputer.memory[0])

    # part 2, i'm lazy so i'll just brute force it
    for noun in range(0, 99):
        for verb in range(0, 99):
            intcodeComputer = IntcodeComputer(input, noun, verb)
            intcodeComputer.run()
            if intcodeComputer.memory[0] == 19690720:
                print((100 * noun) + verb)
                return
Example #9
0
def solve(input_string):
    # part 1
    intcodeComputer = IntcodeComputer(input_string)
    screen_dict = {}

    max_x = 0
    max_y = 0
    while not intcodeComputer.halted:
        x = intcodeComputer.run(return_on_output=True)
        y = intcodeComputer.run(return_on_output=True)
        value = intcodeComputer.run(return_on_output=True)
        screen_dict[(x, y)] = value
        if x > max_x:
            max_x = x
        if y > max_y:
            max_y = y

    # build screen and fill
    total_blocks = 0
    screen = []
    for i in range(0, max_y + 1):
        screen.append([2] * (max_x + 1))
    for key in screen_dict:
        screen[key[1]][key[0]] = screen_dict[key]
        total_blocks += 1 if screen_dict[key] == 2 else 0

    print(total_blocks)

    #part2
    game = Game()
    console = curses.initscr()
    output = OutputModule(game, console, max_y + 1, max_x + 1)
    inputModule = InputModule(game, output.screen)
    intcodeComputer = IntcodeComputer(input_string,
                                      noun=2,
                                      input_handler=inputModule,
                                      output_handler=output)
    while True:
        intcodeComputer.run()

    curses.endwin()
    def testJump(self):
        # position mode
        intcodeComputer = IntcodeComputerV2(
            "3,12,6,12,15,1,13,14,13,4,13,99,-1,0,1,9")
        self.assertEqual(1, intcodeComputer.run([-1]))
        intcodeComputer = IntcodeComputerV2(
            "3,12,6,12,15,1,13,14,13,4,13,99,-1,0,1,9")
        self.assertEqual(0, intcodeComputer.run([0]))
        intcodeComputer = IntcodeComputerV2(
            "3,12,6,12,15,1,13,14,13,4,13,99,-1,0,1,9")
        self.assertEqual(1, intcodeComputer.run([1]))

        # immediate mode
        intcodeComputer = IntcodeComputerV2(
            "3,3,1105,-1,9,1101,0,0,12,4,12,99,1")
        self.assertEqual(1, intcodeComputer.run([-1]))
        intcodeComputer = IntcodeComputerV2(
            "3,3,1105,-1,9,1101,0,0,12,4,12,99,1")
        self.assertEqual(0, intcodeComputer.run([0]))
        intcodeComputer = IntcodeComputerV2(
            "3,3,1105,-1,9,1101,0,0,12,4,12,99,1")
        self.assertEqual(1, intcodeComputer.run([1]))
 def testOuput(self):
     intcodeComputer = IntcodeComputerV2("3,0,4,4,99")
     output = intcodeComputer.run([1])
     self.assertEqual(99, output)
 def test(self):
     for i in self.testCases:
         intcodeComputer = IntcodeComputerV2(i[0])
         intcodeComputer.run([1])
         self.assertEqual(i[1], intcodeComputer.memory)
Example #13
0
 def __init__(self, input_string, phase, return_on_output=False):
     self.intcode_computer = IntcodeComputer(input_string)
     self.phase = phase
     self.return_on_output = return_on_output