def remapSeatsNeighbors(grid: Grid, seatsGrid: dict) -> None: directions = [Coordinate(*d) for d in getAdjacentDirections(dimensions=2)] # Map seat grid neighbors with new rules for p, seat in seatsGrid.items(): visibleAdjacentSeats = [] for d in directions: position = Coordinate(p.x, p.y) while utils.inbetween(0, position.x, grid.width-1) and utils.inbetween(0, position.y, grid.height-1): position = Coordinate(position.x + d.x, position.y + d.y) if position in seatsGrid: visibleAdjacentSeats.append(seatsGrid[position]) break seat.adjacentSeats = visibleAdjacentSeats
def getNewState(self, cubesMap: dict) -> SYMBOL: nbActiveNeighbors = self.nbActiveNeighborCubes(cubesMap) if self.isActive(): return SYMBOL.ACTIVE if utils.inbetween(2, nbActiveNeighbors, 3) else SYMBOL.INACTIVE else: return SYMBOL.ACTIVE if nbActiveNeighbors == 3 else SYMBOL.INACTIVE
# FETCH DATA ########################### lines = utils.readFileLines(filename) ######## # PART 1 ######## pattern = re.compile(r'^(\d+)-(\d+) (\w+): (\w+)$') passwordData = [(int(min), int(max), letter, password) for (min, max, letter, password) in [pattern.findall(l)[0] for l in lines]] validPasswords = [ password for (min, max, letter, password) in passwordData if utils.inbetween(min, password.count(letter), max) ] print('1) Number of valid passwords: ', len(validPasswords), '/ %d' % len(passwordData)) ######## # PART 2 ######## def isValid(pos1, pos2, letter, password): sum = 0 if password[pos1 - 1] == letter: sum += 1 if password[pos2 - 1] == letter: sum += 1
def isHeightValid(v): if v.endswith('cm'): return utils.inbetween(150, int(v.replace('cm', '')), 193) if v.endswith('in'): return utils.inbetween(59, int(v.replace('in', '')), 76) return False
] HAIR_COLOR_REGEX = re.compile(r'^#[0-9a-f]{6}$') EYE_COLOR_REGEX = re.compile(r'^(amb|blu|brn|gry|grn|hzl|oth)$') def isHeightValid(v): if v.endswith('cm'): return utils.inbetween(150, int(v.replace('cm', '')), 193) if v.endswith('in'): return utils.inbetween(59, int(v.replace('in', '')), 76) return False PASSPORT_FIELDS_VALIDATORS = { BIRTH_YEAR: lambda v: len(v) == 4 and utils.inbetween(1920, int(v), 2002), ISSUE_YEAR: lambda v: len(v) == 4 and utils.inbetween(2010, int(v), 2020), EXPIRATION_YEAR: lambda v: len(v) == 4 and utils.inbetween(2020, int(v), 2030), HEIGHT: lambda v: isHeightValid(v), HAIR_COLOR: lambda v: HAIR_COLOR_REGEX.match(v) != None, EYE_COLOR: lambda v: EYE_COLOR_REGEX.match(v) != None, PASSPORT_ID: lambda v: len(v) == 9 and v.isdigit(), COUNTRY_ID: lambda v: True } class Passport: def __init__(self): self.fields = dict()