Ejemplo n.º 1
0
 def __repr__(self):
   return '%s\n%s'%(
       get_class_name(self),
       '\n'.join(
         ' '.join(mapl(str,locs))
         for locs in self._loc_matrix)
       )
Ejemplo n.º 2
0
def part2(input):
    matrix = arrangeMatrix(input)
    mergedMatrix = mergeMatrix(matrix)
    print('len matrix', len(matrix))
    print('shape mergedMatrix', (mergedMatrix.shape))
    monster = '''                  # 
#    ##    ##    ###
 #  #  #  #  #  #   '''
    monsterNumber = len(re.findall('#', monster))
    monsterRegexes = createMonsterRegexes(monster, mergedMatrix.shape[0])
    mutatedMatrixStrings = mapl(matrixToString, map(lambda m: m['matrix'], getMutations(matrixToString(mergedMatrix))))
    maxMonsters = 0
    chosenMutation = mutatedMatrixStrings[0]
    for mutatedMatrixString in mutatedMatrixStrings:
        nbMonsters = findMonsters(monsterRegexes, mutatedMatrixString)
        print('mutatedMatrixString', '\n' + mutatedMatrixString)
        if (nbMonsters > maxMonsters):
            chosenMutation = mutatedMatrixString
            maxMonsters = nbMonsters
    print('chosenMutation', chosenMutation)
    print('monsterNumber', monsterNumber)
    print('maxMonsters', maxMonsters)
    roughness = len(re.findall('#', mutatedMatrixString)) - maxMonsters * monsterNumber
    print('roughness', roughness)
    return roughness
Ejemplo n.º 3
0
def part1(input):
    [cardPk, doorPk] = mapl(int, input)
    cardLoopSize = findLoopSize(7, cardPk)
    cardPrivKey = doLoop(doorPk, cardLoopSize)
    doorLoopSize = findLoopSize(7, doorPk)
    doorPrivKey = doLoop(cardPk, doorLoopSize)
    assert cardPrivKey == doorPrivKey
    return cardPrivKey
Ejemplo n.º 4
0
def calcTileId(tileFlip):
    dirCounts = {}
    for dir in DIRS:
        dirCounts[dir] = tileFlip.count(dir)
    NE = dirCounts['ne'] - dirCounts['sw']
    SE = dirCounts['se'] - dirCounts['nw']
    E = dirCounts['e'] - dirCounts['w']
    N = NE - SE
    E = 2 * E + NE + SE
    tileId = ','.join(mapl(str, [N, E]))
    # print('tileId', tileId)
    return tileId
Ejemplo n.º 5
0
def calcMaps(input):
    parsedInput = mapl(lineToArray, input)
    remainingOkIngredients = np.concatenate(
        np.array(mapl(lambda tup: tup[0], parsedInput),
                 dtype=object)).tolist()
    print('remainingOkIngredients', remainingOkIngredients)
    allergenMap = dict()
    foodMap = dict()
    for i, food in enumerate(parsedInput):
        for allergen in food[1]:
            if allergen in allergenMap:
                allergenMap[allergen].append(i)
            else:
                allergenMap[allergen] = [i]
        for food in food[0]:
            if food in foodMap:
                foodMap[food].append(i)
            else:
                foodMap[food] = [i]
    okFoods = []
    foodToAllergenMap = dict()
    for food in foodMap:
        foodIndexes = foodMap[food]
        badFood = False
        for allergen in allergenMap:
            if isSuperset(foodIndexes, allergenMap[allergen]):
                badFood = True
                if food in foodToAllergenMap:
                    foodToAllergenMap[food].append(allergen)
                    print('multiple food to allergen mappings found for food',
                          food, 'allergens', foodToAllergenMap[food])
                else:
                    foodToAllergenMap[food] = [allergen]
        if not badFood:
            okFoods.append(food)
    return foodToAllergenMap, foodMap, okFoods, allergenMap
Ejemplo n.º 6
0
def toPos(posKey):
    return mapl(int, posKey.split(','))
Ejemplo n.º 7
0
def toKey(pos):
    return ','.join(mapl(str, pos))
Ejemplo n.º 8
0
def part2(input):
    tileFlips: [[str]] = parseInput(input)
    actives = mapl(lambda id: mapl(int, id.split(',')), calcTileIds(tileFlips))
    # print('actives', actives)
    playGame(actives, 100)
    return len(actives)
Ejemplo n.º 9
0
def toMatrix(matrixStr: str) -> np.array:
    npArrayInput = mapl(lambda line: [char for char in line], matrixStr.splitlines())
    return np.array(npArrayInput)
Ejemplo n.º 10
0
def matrixToString(matrix: np.array):
    asList = matrix.tolist()
    result = '\n'.join(mapl(''.join, asList))
    # print('matrixToString\n', matrix, '\nresult\n', result, )
    return result
Ejemplo n.º 11
0
def getCorners(matrix: dict[str, MatrixVal]):
    minPos = getMinPos(matrix)
    maxAdd = int(math.sqrt(len(matrix))) - 1
    corners = [[0, 0], [0, maxAdd], [maxAdd, 0], [maxAdd, maxAdd]]
    return mapl(lambda corner: int(matrix[toKey(minPos[0] + corner[0], minPos[1] + corner[1])]['id']), corners)
Ejemplo n.º 12
0
def toRowCol(key: str):
    return mapl(int, key.split('rc'))