示例#1
0
    def __init__(self, rules, ticket, nearby_tickets):
        self.fields = {}

        for rule in rules:
            regex = Re()
            if regex.match(TicketScanner.RULE_REGEX, rule):
                m = regex.last_match
                field, range1_lower, range1_upper, range2_lower, range2_upper = (
                    m.group('field'),
                    int(m.group('range1_lower')),
                    int(m.group('range1_upper')),
                    int(m.group('range2_lower')),
                    int(m.group('range2_upper')),
                )
                self.fields[field] = [
                    range1_lower,
                    range1_upper,
                    range2_lower,
                    range2_upper,
                ]
            else:
                raise Exception('Bad rule: %s' % rule)

        self.ticket = [int(x) for x in ticket[1].split(',')]
        self.nearby_tickets = [[int(x) for x in ticket.split(',')]
                               for ticket in nearby_tickets[1:]]
示例#2
0
    def __init__(self, instruction):
        self.delta_x = 0
        self.delta_y = 0
        self.turns = 0
        self.forward = 0

        regex = Re()
        if regex.match(Instruction.DIR_REGEX, instruction):
            m = regex.last_match
            action, value = (
                m.group('action'),
                int(m.group('value')),
            )

            if action in Ship.DIRECTION_MAP:
                x, y = Ship.DIRECTION_MAP[action]
                self.delta_x, self.delta_y = x * value, y * value
            elif action in ('R', 'L'):
                turn_multiplier = 1 if action == 'R' else -1
                if value % 90 == 0:
                    self.turns = turn_multiplier * (value / 90)
                else:
                    raise Exception('Bad turn angle: %s%s' % (action, value))
            elif action == 'F':
                self.forward = value
            else:
                raise Exception('Impossible case')
        else:
            raise Exception('Bad instruction: %s' % instruction)
示例#3
0
    def __init__(self, room):
        self.room = room

        regex = Re()
        if regex.match(self.ROOM_REGEX, room):
            m = regex.last_match
            self.encrypted_name, self.sector_id, self.checksum = (
                m.group('encrypted_name'), int(m.group('sector_id')),
                m.group('checksum'))
示例#4
0
 def __init__(self, ingredient_data):
     regex = Re()
     if regex.match(Ingredient.REGEX, ingredient_data):
         m = regex.last_match
         self.name = m.group('name')
         self.capacity = int(m.group('capacity'))
         self.durability = int(m.group('durability'))
         self.flavor = int(m.group('flavor'))
         self.texture = int(m.group('texture'))
         self.calories = int(m.group('calories'))
     else:
         raise Exception('Bad ingredient data: %s' % ingredient_data)
示例#5
0
    def __init__(self, reindeer_desc):
        regex = Re()
        if regex.match(Reindeer.REGEX, reindeer_desc):
            m = regex.last_match
            self.name, self.kms, self.endurance, self.rest = (
                m.group('name'),
                int(m.group('kms')),
                int(m.group('endurance')),
                int(m.group('rest')),
            )

            self.reset()
        else:
            raise Exception('Bad reindeer input: %s' % reindeer_desc)
示例#6
0
    def __init__(self, routes):
        self.nodes = {}

        regex = Re()
        for route in routes:
            if regex.match(self.ROUTE_REGEXP, route):
                m = regex.last_match
                origin_name, destination_name, weight = (
                    m.group('origin'),
                    m.group('destination'),
                    int(m.group('weight')),
                )
                origin = self.get_node(origin_name)
                destination = self.get_node(destination_name)

                origin.add_edge(destination, weight, is_bidirectional=True)
            else:
                raise Exception('Misformatted route: %s' % route)
示例#7
0
    def __init__(self, raw_instruction):
        self.is_mask = False

        regex = Re()
        if regex.match(Instruction.BITMASK_REGEX, raw_instruction):
            m = regex.last_match
            mask = m.group('mask')

            self.is_mask = True
            self.on_mask = int(mask.replace('X', '0'), 2)
            self.off_mask = int(mask.replace('X', '1'), 2)
        elif regex.match(Instruction.MEMSET_REGEX, raw_instruction):
            m = regex.last_match
            self.address, self.value = (
                int(m.group('address')),
                int(m.group('value')),
            )
        else:
            raise Exception('Bad instruction: %s' % raw_instruction)
示例#8
0
    def __init__(self, rules):
        self.rules = rules

        chart = defaultdict(lambda: defaultdict(int))

        for rule in rules:
            regex = Re()
            if regex.match(self.SEATING_REGEX, rule):
                m = regex.last_match
                name, change, amount, partner = (
                    m.group('name'),
                    m.group('change'),
                    int(m.group('amount')),
                    m.group('partner'),
                )
                multiplier = 1 if change == 'gain' else -1
                score = multiplier * amount

                chart[name][partner] = score
            else:
                raise Exception('Bad seating rule: %s' % rule)

        self.chart = chart
        self.players = sorted(list(chart.keys()))
示例#9
0
    def __init__(self, instruction):
        self.instruction = instruction

        regex = Re()

        if regex.match(self.SIGNAL_REGEXP, instruction):
            m = regex.last_match

            self.instruction_type = 'SIGNAL'

            self.w, self.wire = (
                m.group('w'),
                m.group('wire'),
            )
        elif regex.match(self.AND_REGEXP, instruction):
            m = regex.last_match

            self.instruction_type = 'AND'

            self.w1, self.w2, self.wire = (
                m.group('w1'),
                m.group('w2'),
                m.group('wire'),
            )
        elif regex.match(self.OR_REGEXP, instruction):
            m = regex.last_match

            self.instruction_type = 'OR'

            self.w1, self.w2, self.wire = (
                m.group('w1'),
                m.group('w2'),
                m.group('wire'),
            )
        elif regex.match(self.LSHIFT_REGEXP, instruction):
            m = regex.last_match

            self.instruction_type = 'LSHIFT'

            self.w, self.value, self.wire = (
                m.group('w'),
                int(m.group('value')),
                m.group('wire'),
            )
        elif regex.match(self.RSHIFT_REGEXP, instruction):
            m = regex.last_match

            self.instruction_type = 'RSHIFT'

            self.w, self.value, self.wire = (
                m.group('w'),
                int(m.group('value')),
                m.group('wire'),
            )
        elif regex.match(self.NOT_REGEXP, instruction):
            m = regex.last_match

            self.instruction_type = 'NOT'

            self.w, self.wire = (
                m.group('w'),
                m.group('wire'),
            )
        else:
            raise Exception('Bad instruction: %s' % instruction)