Beispiel #1
0
        elif move_direction == 'N':
            self.waypoint_y += distance
        elif move_direction == 'W':
            self.waypoint_x -= distance
        elif move_direction == 'S':
            self.waypoint_y -= distance

    def _to_waypoint(self, times):
        self.position_x += times * self.waypoint_x
        self.position_y += times * self.waypoint_y

    def _rotate_waypoint(self, direction, degrees):
        left_quarter_turns = (degrees//90 if direction == 'L' else -degrees//90) % 4
        if left_quarter_turns == 1:
            self.waypoint_x, self.waypoint_y = -self.waypoint_y, self.waypoint_x
        elif left_quarter_turns == 2:
            self.waypoint_x, self.waypoint_y = -self.waypoint_x, -self.waypoint_y
        elif left_quarter_turns == 3:
            self.waypoint_x, self.waypoint_y = self.waypoint_y, -self.waypoint_x


if __name__ == '__main__':
    pattern = r'^(\w)(\d+)$'
    test_data = regex_parse_input(__file__, pattern, test=True)
    data = regex_parse_input(__file__, pattern)

    test_solution(compute1, test_data, 25)
    test_solution(compute1, data)
    test_solution(compute2, test_data, 286)
    test_solution(compute2, data)
Beispiel #2
0
def compute2(instructions):
    mask = ''
    mem = {}
    for inst, val in instructions:
        if inst == 'mask':
            mask = val
        else:
            masked_loc = build_masked_loc(inst[4:-1], mask)
            possibilities = combinations(masked_loc.count('X'))
            locations = []
            for p in possibilities:
                mask_values = list(p)
                locations.append(''.join([
                    c if c != 'X' else mask_values.pop() for c in masked_loc
                ]))
            for loc in locations:
                mem[int(loc, 2)] = int(val)
    return sum([mem[k] for k in mem])


if __name__ == '__main__':
    pattern = r'(.+) = ([X0-9]+)'
    test_data = regex_parse_input(__file__, pattern, test=True)
    test_data_b = regex_parse_input(__file__, pattern, test=True, version='b')
    data = regex_parse_input(__file__, pattern)

    test_solution(compute1, test_data, 165)
    test_solution(compute1, data, 6386593869035)
    test_solution(compute2, test_data_b, 208)
    test_solution(compute2, data, 4288986482164)