Exemplo n.º 1
0
import utility  # my own utility.pl file
import itertools  # groupby
import functools  # reduce


# Say out loud the given string of digits
def lookAndSay(digits):
    return "".join([
        f"{len(list(group))}{digit}"
        for digit, group in itertools.groupby(digits)
    ])


assert lookAndSay('1') == '11'
assert lookAndSay('11') == '21'
assert lookAndSay('21') == '1211'
assert lookAndSay('1211') == '111221'
assert lookAndSay('111221') == '312211'

# Display info message
print("Give a string of digits:\n")
inputDigits = utility.readInputList(joinedWith='')

# Display results
iteration40 = functools.reduce(lambda prev, i: lookAndSay(prev), range(40),
                               inputDigits)
iteration50 = functools.reduce(lambda prev, i: lookAndSay(prev), range(10),
                               iteration40)
print(f'{inputDigits} -> 40: {len(iteration40)} -> 50: {len(iteration50)}')
Exemplo n.º 2
0
            neighboursOn == 3)

    def relightCorners(self, withRelight):
        if withRelight:
            self.grid[[0, 0, -1, -1], [0, -1, 0, -1]] = 1


smallExample = ['.#.#.#', '...##.', '#....#', '..#...', '#.#..#', '####..']
assert LightAnimate(smallExample).iterate(4).lightsOn() == 4
assert LightAnimate(smallExample).iterate(5, withRelight=True).lightsOn() == 17

# Display info message
print(
    "Give a ligth grid of initial configuration ('.' means OFF, '#' means ON):\n"
)
inputStringList = utility.readInputList()

# Display results
print(f'Initial configuration:  {LightAnimate(inputStringList).lightsOn()}')
print(
    f'Ligths after 100 steps: {LightAnimate(inputStringList).iterate(100).lightsOn()}'
)
print(
    f'100 steps with relight: {LightAnimate(inputStringList).iterate(100, True).lightsOn()}'
)
print(
    f'Ligths after 1000 steps: {LightAnimate(inputStringList).iterate(1000).lightsOn()}'
)
print(
    f'1000 steps with relight: {LightAnimate(inputStringList).iterate(1000, True).lightsOn()}'
)
Exemplo n.º 3
0
    return sumOfTrees

# Multiply tree counts in random slopes together 
def multiplySlopes(treeGrid):
    T = lambda right, down: countTrees(treeGrid, right, down)
    return T(1, 1) * T(3, 1) * T(5, 1) * T(7, 1) * T(1, 2)

# Check test cases
smallExample = [
    '..##.......',
    '#...#...#..',
    '.#....#..#.',
    '..#.#...#.#',
    '.#...##..#.',
    '..#.##.....',
    '.#.#.#....#',
    '.#........#',
    '#.##...#...',
    '#...##....#',
    '.#..#...#.#']
assert countTrees(smallExample, right = 3, down = 1) == 7
assert multiplySlopes(smallExample) == 336

# Display info message
print("Tree list for the toboggan:\n")
treeGrid = utility.readInputList()

# Display results
print(f'{countTrees(treeGrid, right = 3, down = 1) = }')
print(f'{multiplySlopes(treeGrid) = }')
Exemplo n.º 4
0
            t = slice(i1, i2 + 1), slice(j1, j2 + 1)
            self.grid[t] = jumpTable[action](self.grid[t])
            self.grid.clip(min=0)
        return self


if __name__ == '__main__':
    assert LigthBulbGrid(1000, 1000).execute(['turn on 0,0 through 999,999'
                                              ]).lightsOn() == 1000000
    assert LigthBulbGrid(1000, 1000).execute(['toggle 0,0 through 999,0'
                                              ]).lightsOn() == 1000
    assert LigthBulbGrid(1000,
                         1000).execute(['turn on 499,499 through 500,500'
                                        ]).lightsOn() == 4

    assert LigthBulbGrid(1000, 1000).execute(['turn on 0,0 through 0,0'],
                                             True).lightsOn() == 1
    assert LigthBulbGrid(1000, 1000).execute(['toggle 0,0 through 999,999'],
                                             True).lightsOn() == 2000000

    # Display info message
    print("Give a list of light bulb setting instructions:\n")
    instructionList = utility.readInputList()

    # Display results
    numberOfBulbsOn = LigthBulbGrid(1000,
                                    1000).execute(instructionList).lightsOn()
    totalBrightness = LigthBulbGrid(1000, 1000).execute(instructionList,
                                                        True).lightsOn()
    print(f'{numberOfBulbsOn = }, {totalBrightness = }')
Exemplo n.º 5
0
            start += delta

    # Return
    value = sum(vents > 1 for vents in ocean.values())
    print(value)
    return value


# Verify test cases
smallExample = '''
0,9 -> 5,9
8,0 -> 0,8
9,4 -> 3,4
2,2 -> 2,1
7,0 -> 7,4
6,4 -> 2,0
0,9 -> 2,9
3,4 -> 1,4
0,0 -> 8,8
5,5 -> 8,2
'''.strip().split('\n')
assert (hydrothermalVents(smallExample) == 5)
assert (hydrothermalVents(smallExample, part2=True) == 12)

# Display info message
print("\nGive a list of hydrothermal vent line coordinates:")
lineCoordinates = utility.readInputList()

# Display results
print(f'{hydrothermalVents(lineCoordinates) = }')
print(f'{hydrothermalVents(lineCoordinates, part2=True) = }')
Exemplo n.º 6
0
        for i in range(travelTime):
            # Update Duration
            self.loc[self.Duration <= -self.RestDur,
                     'Duration'] = self.FlightDur
            self.Duration -= 1
            # Update Distance
            self.loc[self.Duration >= 0, 'Distance'] += self.Speed
            # Update Score
            self.loc[self.Distance == max(self.Distance), 'Score'] += 1
        self.Distance = pandas.to_numeric(self.Distance, downcast='signed')
        return self


smallExample = [
    'Comet can fly 14 km/s for 10 seconds, but then must rest for 127 seconds.',
    'Dancer can fly 16 km/s for 11 seconds, but then must rest for 162 seconds.'
]
smallHerd = Herd(smallExample).race(1000)
assert smallHerd.loc['Comet', 'Distance'] == 1120
assert smallHerd.loc['Dancer', 'Distance'] == 1056
assert smallHerd.loc['Comet', 'Score'] == 312
assert smallHerd.loc['Dancer', 'Score'] == 689

# Display info message
print("Give a list of reindeer descriptions:\n")
reindeerDescriptions = utility.readInputList()
travelTime = 2503

# Display results
print(Herd(reindeerDescriptions).race(travelTime))
Exemplo n.º 7
0
smallExample = '''
7,4,9,5,11,17,23,2,0,14,21,24,10,16,13,6,15,25,12,22,18,20,8,19,3,26,1

22 13 17 11  0
 8  2 23  4 24
21  9 14 16  7
 6 10  3 18  5
 1 12 20 15 19

 3 15  0  2 22
 9 18 13 17  5
19  8  7 25 23
20 11 10 24  4
14 21 16 12  6

14 21 17 24  4
10 16 15  9 19
18  8 23 26 20
22 11 13  6  5
 2  0 12  3  7
'''.strip().split('\n')
assert (finalBingoScore(smallExample) == 4512)
assert (finalBingoScore(smallExample, part2=True) == 1924)

# Display info message
print("\nGive a list of drawn numbers and table values:")
bingoSubsystem = utility.readInputList()

# Display results
print(f'{finalBingoScore(bingoSubsystem) = }')
print(f'{finalBingoScore(bingoSubsystem, part2=True) = }')
Exemplo n.º 8
0
# Check test cases
assert optimalPath(source=(0, 3), target=(3, 0),
                   passcode='ihgpwlah') == 'DDRRRD'
assert optimalPath(source=(0, 3), target=(3, 0),
                   passcode='kglvqrro') == 'DDUDRLRRUDRD'
assert optimalPath(source=(0, 3), target=(3, 0),
                   passcode='ulqzkmiv') == 'DRURDRUDDLLDLUURRDULRLDUUDDDRR'
assert optimalPath(source=(0, 3),
                   target=(3, 0),
                   passcode='ihgpwlah',
                   part2=True) == 370
assert optimalPath(source=(0, 3),
                   target=(3, 0),
                   passcode='kglvqrro',
                   part2=True) == 492
assert optimalPath(source=(0, 3),
                   target=(3, 0),
                   passcode='ulqzkmiv',
                   part2=True) == 830

# Display info message
print("\nWhat's your passcode?")
passcode = utility.readInputList()[0]

# Display results
print(f'{optimalPath(source=(0, 3), target=(3, 0), passcode=passcode) = }\n')
print(
    f'{optimalPath(source=(0, 3), target=(3, 0), passcode=passcode, part2=True) = }\n'
)
Exemplo n.º 9
0
        return whiteElephantPartyFormula(elves, part2)
    return whiteElephantPartyAlgorithm(elves, part2)


# Assert that the WEP formula and the algorithm gives the same result
def testEmAll(maxElves):
    for elves in range(1, maxElves):
        for part2 in (False, True):
            algorithm = whiteElephantPartyAlgorithm(elves, part2)
            formula = whiteElephantPartyFormula(elves, part2)
            assert algorithm == formula


# List the winner of the WEPs from 1 to N
def printEmAllOut(maxElves, part2=False, formula=False):
    for elves in range(1, maxElves):
        print(whiteElephantPartyAlgorithm(elves, part2, formula))


assert whiteElephantParty(5) == 3
assert whiteElephantParty(5, part2=True) == 2
testEmAll(100)

# Display info message
print("\nGive initial state:")
elves = int(utility.readInputList()[0])

# Display results
print(f'{whiteElephantParty(elves) = }')
print(f'{whiteElephantParty(elves, part2=True) = }')
#printEmAllOut(10000, part2=False, formula=False)
Exemplo n.º 10
0
    middle = text[match.end():match.end() +
                  length]  # replacement text after (##x##)
    end = text[match.end() + length:]  # remaining text for further processing

    middleLength = length if part1 else decompressedLength(middle, part1)
    return beginLength + multiplier * middleLength + decompressedLength(
        end, part1)


assert decompressedLength('ADVENT') == 6
assert decompressedLength('A(1x5)BC') == 7
assert decompressedLength('(3x3)XYZ') == 9
assert decompressedLength('A(2x2)BCD(2x2)EFG') == 11
assert decompressedLength('(6x1)(1x3)A') == 6
assert decompressedLength('X(8x2)(3x3)ABCY') == 18

assert decompressedLength('(3x3)XYZ', False) == 9
assert decompressedLength('X(8x2)(3x3)ABCY', False) == 20
assert decompressedLength('(27x12)(20x12)(13x14)(7x10)(1x12)A',
                          False) == 241920
assert decompressedLength(
    '(25x3)(3x3)ABC(2x3)XY(5x2)PQRSTX(18x9)(3x2)TWO(5x7)SEVEN', False) == 445

# Display info message
print("Give a file in a peculiar compressed format:\n")
sequences = utility.readInputList()

# Display results
print(f"{sum(decompressedLength(seq) for seq in sequences) = }")
print(f"{sum(decompressedLength(seq, part1 = False) for seq in sequences) = }")
Exemplo n.º 11
0
# Perform operations line by line (for part 2 in reversed order)
def scramble(password, operations, part2=False):
    #print(password, part2)
    for operation in operations[::-1 if part2 else 1]:
        password = perform(password, operation, part2)
    return password

smallExample = """
swap position 4 with position 0
swap letter d with letter b
reverse positions 0 through 4
rotate left 1 step
move position 1 to position 4
move position 3 to position 0
rotate based on position of letter b
rotate based on position of letter d
""".strip().split('\n')

assert scramble('abcde', smallExample) == 'decab'


# Display info message
print("\nList of operations:")
operations = utility.readInputList()

# Display results
print(f'{scramble("abcdefgh", operations) = }')
assert scramble('gbhafcde', operations, part2=True) == 'abcdefgh'
print(f'{scramble("fbgdceah", operations, part2=True) = }')

Exemplo n.º 12
0
    if ''.join(letter for _, letter in mostCommon[:5]) == checksum:
        return sectorId
    return 0


def decypher(room):
    name, sectorId, checksum = (room[:-11], int(room[-10:-7]), room[-6:-1])
    a = ord('a')
    numLetters = ord('z') - ord('a') + 1
    decoded = ''.join(
        chr((ord(letter) - a + sectorId) % numLetters +
            a) if letter.isalpha() else ' ' for letter in name)
    print(decoded)
    return decoded


assert (realSectorId('aaaaa-bbb-z-y-x-123[abxyz]') == 123)
assert (realSectorId('a-b-c-d-e-f-g-h-987[abcde]') == 987)
assert (realSectorId('not-a-real-room-404[oarel]') == 404)
assert (realSectorId('totally-real-room-200[decoy]') == 0)
assert (decypher('qzmt-zixmtkozy-ivhz-343[decoy]') == 'very encrypted name')

# Display info message
print("Give a list of room name, sector id, checksum triplets:\n")
rooms = utility.readInputList()

# Display results
print(f'{sum(realSectorId(room) for room in rooms) = }')
for room in rooms:
    if 'north' in decypher(room):
        print(f'{room = }, {decypher(room) = }')
Exemplo n.º 13
0
            sumOfYesAnswers += len(groupYesAnswers)
            groupYesAnswers = set()  # start a new group
            newGroup = True

        else:  # line not empty
            lineYesAnswers = set([question for question in line])

            if part2 and not newGroup:
                groupYesAnswers = lineYesAnswers.intersection(groupYesAnswers)
            else:
                groupYesAnswers = lineYesAnswers.union(groupYesAnswers)
                newGroup = False

    del yesAnswers[-1]  # remove temporary extra line break
    return sumOfYesAnswers


# Check test cases
smallExample = [
    'abc', '', 'a', 'b', 'c', '', 'ab', 'ac', '', 'a', 'a', 'a', 'a', '', 'b'
]
assert sumOfQuestionsAnsweredYes(smallExample) == 11
assert sumOfQuestionsAnsweredYes(smallExample, part2=True) == 6

# Display info message
print("Give list of yes answers for each person in each group:\n")
yesAnswers = utility.readInputList()

# Display results
print(f'{sumOfQuestionsAnsweredYes(yesAnswers) = }')
print(f'{sumOfQuestionsAnsweredYes(yesAnswers, part2 = True) = }')
Exemplo n.º 14
0
# Find the highest seat number in the list of boarding passes.
def highestSeatNumber(boardingPasses):
    existingSeatNumbers = list(map(seatNumber, boardingPasses))
    return max(existingSeatNumbers)


# My seat is the (only) empty seat on the plane.
# Find the set of missing numbers amongst the list of seat numbers.
def mySeatNumber(boardingPasses):
    existingSeatNumbers = list(map(seatNumber, boardingPasses))

    highest = highestSeatNumber(boardingPasses)
    lowest = min(existingSeatNumbers)
    allPossibleSeatNumbers = list(range(lowest, highest - 1))

    missingSeats = set(allPossibleSeatNumbers) - set(existingSeatNumbers)
    return missingSeats


# Check test cases
smallExample = ['BFFFBBFRRR', 'FFFBBBFRRR', 'BBFFBBFRLL']
assert highestSeatNumber(smallExample) == 820

# Display info message
print("Give boarding passes:\n")
boardingPasses = utility.readInputList()

# Display results
print(f'{highestSeatNumber(boardingPasses) = }')
print(f'{mySeatNumber(boardingPasses) = }')
Exemplo n.º 15
0
    tokens = re.split('-|: | ', line)

    min = int(tokens[0])
    max = int(tokens[1])
    letter = tokens[2]
    password = tokens[3]

    if part2:
        return (password[min - 1] == letter) != (password[max - 1] == letter)

    return min <= password.count(letter) <= max


# Number of valid passwords in the list
def sumValidPasswords(passwordList, part2=False):
    func = lambda line: int(isValid(line, part2) == True)
    return sum(list(map(func, passwordList)))


# Check test cases
smallExample = ['1-3 a: abcde', '1-3 b: cdefg', '2-9 c: ccccccccc']
assert sumValidPasswords(smallExample) == 2
assert sumValidPasswords(smallExample, part2=True) == 1

# Display info message
print("Rule - password list:\n")
passwordList = utility.readInputList()

# Display results
print(f'{sumValidPasswords(passwordList) = }')
print(f'{sumValidPasswords(passwordList, part2 = True) = }')
Exemplo n.º 16
0
import utility # my own utility.py file
import numpy as np

smallExample ="""
199
200
208
210
200
207
240
269
260
263""".strip().split('\n')

def depthIncreaseRate(data, part2 = False):
    overlap = 3 if part2 else 1
    data = np.array(list(map(int, data)))
    
    return sum(data[overlap:] - data[:-overlap] > 0)

assert(depthIncreaseRate(smallExample) == 7)
assert(depthIncreaseRate(smallExample, True) == 5)

# Display info message
print("Give sonar sweep readings:\n");
depthMeasurements = utility.readInputList()

# Display results
print (f'{depthIncreaseRate(depthMeasurements) = }')
print (f'{depthIncreaseRate(depthMeasurements, part2 = True) = }')
Exemplo n.º 17
0
        token = re.split(': |, ', auntDescription)
        self.name = token[0]
        self.things = {
            token[i]: int(token[i + 1])
            for i in range(1, len(token), 2)
        }

    def __str__(self):
        return self.name + ': ' + self.things.__str__()

    def match(self, other, comparators={}):
        return all(t not in other.things or comparators.get(t, operator.eq)(v, other.things[t]) \
          for t, v in self.things.items())


# Display info message
print("Give a list of aunt descriptions:\n")
auntDescriptions = utility.readInputList()
clue = 'Sue Clue: children: 3, cats: 7, samoyeds: 2, pomeranians: 3, akitas: 0, vizslas: 0, goldfish: 5, trees: 3, cars: 2, perfumes: 1'
realSue = Aunt(clue)

# Display results
aunts = [Aunt(description) for description in auntDescriptions]
matchingAunts = [aunt for aunt in aunts if aunt.match(realSue)]
matchingAunts2 = [
    aunt for aunt in aunts if aunt.match(realSue, Aunt.comparators)
]

print(realSue)
print(f'Match 1: {[aunt.__str__() for aunt in matchingAunts]}')
print(f'Match 2: {[aunt.__str__() for aunt in matchingAunts2]}')
Exemplo n.º 18
0
# Generate the MD5 hash of an input string and convert it to string 
def generateMd5Hash(secretKey):
	return hashlib.md5(secretKey.encode()).hexdigest()

# Find Santa the lowest positive number that appended to the key
# produces a hash starting with a given prefix 
def findLowestNumberMatchingThePrefix(secretKey, prefix):
	adventCoin = 0
	while not generateMd5Hash(secretKey + str(adventCoin)).startswith(prefix):
		adventCoin += 1
		
	md5Hash = generateMd5Hash(secretKey + str(adventCoin))
	print(f"{secretKey = }, {adventCoin = }, {md5Hash = }, {prefix = }")
	return adventCoin

assert findLowestNumberMatchingThePrefix('abcdef', '00000') == 609043
assert findLowestNumberMatchingThePrefix('pqrstuv', '00000') == 1048970

# Display info message
print("\nGive Secret Key to find the lowest number postfix for which the md5 hash has a 1-6 zero prefix\n");
secretKey = utility.readInputList(joinedWith = '')

# Display results
findLowestNumberMatchingThePrefix(secretKey, '')
findLowestNumberMatchingThePrefix(secretKey, '0')
findLowestNumberMatchingThePrefix(secretKey, '00')
findLowestNumberMatchingThePrefix(secretKey, '000')
findLowestNumberMatchingThePrefix(secretKey, '0000')
findLowestNumberMatchingThePrefix(secretKey, '00000')
findLowestNumberMatchingThePrefix(secretKey, '000000')
Exemplo n.º 19
0

# What is the position of the character that causes Santa to first enter the basement?
def firstEnteringBasement(instructionString):
    floorIncrement = {'(': 1, ')': -1}
    currentFloor = 0
    basement = -1

    for index, character in enumerate(instructionString):
        currentFloor += floorIncrement.get(
            character, 0)  # increment with zero for non-() characters
        if currentFloor == basement:
            return index + 1  # basement is reached at this position

    return -1


assert firstEnteringBasement(')') == 1
assert firstEnteringBasement('()())') == 5

# Display info message
print(
    "Give a string of '(' and ')' characters, where '(' means go up one floor, while ')' means go one floor down.\n"
)
instructionString = utility.readInputList(joinedWith='')

# Display results
print(
    f"{lastFloor(instructionString) = }, {firstEnteringBasement(instructionString) = }"
)
Exemplo n.º 20
0
                self.minimumNumberOfContainers = numContainers
                self.differentMinimumContainers = 1
            elif numContainers == self.minimumNumberOfContainers:
                self.differentMinimumContainers += 1
            return 1
        elif remainingLiters < 0:
            return 0

        return sum(self.countSolutionsInSubTree(i + 1, remainingLiters - self.containers[i], numContainers + 1) \
          for i in range(firstIndex, len(self.containers)))

    def combinations(self):
        self.minimumNumberOfContainers = 1000000
        self.differentMinimumContainers = 0
        self.numberOfDifferentSolutions = self.countSolutionsInSubTree(
            0, self.eggnogLiters, 0)
        return self


smallExample = ['20', '15', '10', '5', '5']
smallEggnog = Eggnog(smallExample, 25).combinations()
assert smallEggnog.numberOfDifferentSolutions == 4
assert smallEggnog.minimumNumberOfContainers == 2
assert smallEggnog.differentMinimumContainers == 3

# Display info message
print("Give a list of container sizes in liters:\n")
containerList = utility.readInputList()

# Display results
print(Eggnog(containerList, 150).combinations())
Exemplo n.º 21
0
def firewallRules(rules, minIpAddress, maxIpAddress, part2=False):
    blacklist = parseBlockedIps(rules, maxIpAddress)
    simplifiedBlacklist = simplify(blacklist, minIpAddress, maxIpAddress)

    firstAllowedIp = next(iter(simplifiedBlacklist.items()))[1] + 1
    allIps = maxIpAddress - minIpAddress + 1
    allowedIps = allIps - length(simplifiedBlacklist)

    return allowedIps if part2 else firstAllowedIp


smallExample = """
5-8
0-2
4-7
""".strip().split('\n')

assert firewallRules(smallExample, minIpAddress=0, maxIpAddress=9) == 3
assert firewallRules(smallExample, minIpAddress=0, maxIpAddress=9,
                     part2=True) == 2

# Display info message
print("\nWhich IP ranges are blocked?")
rules = utility.readInputList()

# Display results
print(f'{firewallRules(rules, minIpAddress=0, maxIpAddress=4294967295) = }')
print(
    f'{firewallRules(rules, minIpAddress=0, maxIpAddress=4294967295, part2=True) = }'
)
Exemplo n.º 22
0
    indices = []
    tripletHistory = TripletHistory(1000)
    
    for i in range(999999999):
        if len(indices) >= numberOfKeys:
            return indices
        indices += getMatchingTripletIndices(salt, i, iterations, tripletHistory)
        
    return indices

# Which index produces the last key
def indexOfLastKey(salt, numberOfKeys, iterations=1):
    T = utility.SimpleTimer()
    indices = generateIndices(salt, numberOfKeys, iterations)
    solution = indices[numberOfKeys - 1]
    print(f'\nindexOfLastKey({salt}, {numberOfKeys}, {iterations}) = {solution}')
    return solution
    
smallExample = """
abc
""".strip().split('\n')[0]
assert indexOfLastKey(smallExample, numberOfKeys=64) == 22728
assert indexOfLastKey(smallExample, numberOfKeys=64, iterations=2017) == 22551

# Display info message
print("\nGive pre-arranged hash salt:")
salt = utility.readInputList()[0]

# Display results
print(f'{indexOfLastKey(salt, numberOfKeys=64) = }')
print(f'{indexOfLastKey(salt, numberOfKeys=64, iterations=2017) = }')
Exemplo n.º 23
0
                if newState.turn(decay, newEffect) and newState.turn(
                        bossDamage):  # continue game
                    GameState.bestSolution = min(
                        GameState.bestSolution,
                        newState.tryAllEffects(EffectList, decay))
                elif newState.playerHP > 0:  # Player has won
                    GameState.bestSolution = min(GameState.bestSolution,
                                                 newState.manaSpent)
        return GameState.bestSolution


Effect = collections.namedtuple('Effect', [
    'name', 'manaCost', 'startingDuration', 'damage', 'armor', 'heal', 'mana'
])
effects = [
    Effect('Magic Missile', 53, 1, 4, 0, 0, 0),
    Effect('Drain', 73, 1, 2, 0, 2, 0),
    Effect('Shield', 113, 6, 0, 7, 0, 0),
    Effect('Poison', 173, 6, 3, 0, 0, 0),
    Effect('Recharge', 229, 5, 0, 0, 0, 101)
]

# Display info message
print("Let the games begin!\n")
bossStats = utility.readInputList()

# Display results
print(f'Total mana spent (normal): {GameState().tryAllEffects(effects)}')
GameState.bestSolution = 1000000
print(f'Total mana spent (hard): {GameState().tryAllEffects(effects, 1)}')
Exemplo n.º 24
0
    print(leastCommon, mostCommon)
    value = listToBinary(leastCommon) * listToBinary(mostCommon)
    print(value)
    return value


# Verify test cases
smallExample = '''
00100
11110
10110
10111
10101
01111
00111
11100
10000
11001
00010
01010
'''.strip().split('\n')
assert (binaryDiagnostic(smallExample) == 198)
assert (binaryDiagnostic(smallExample, part2=True) == 230)

# Display info message
print("\nGive Binary Diagnostic data:")
diagnosticReport = utility.readInputList()

# Display results
print(f'{binaryDiagnostic(diagnosticReport) = }')
print(f'{binaryDiagnostic(diagnosticReport, part2=True) = }')
Exemplo n.º 25
0
fs-he
start-DX
pj-DX
end-zg
zg-sl
zg-pj
pj-he
RW-he
fs-DX
pj-RW
zg-RW
start-pj
he-WI
zg-he
pj-fs
start-RW
'''.strip().split('\n')
assert numberOfAllPaths(smallExample) == 10
assert numberOfAllPaths(smallExample2) == 19
assert numberOfAllPaths(smallExample3) == 226
assert numberOfAllPaths(smallExample, part2=True) == 36
assert numberOfAllPaths(smallExample2, part2=True) == 103
assert numberOfAllPaths(smallExample3, part2=True) == 3509

# Display info message
print("\nGive a list of cave connections:")
cavemap = utility.readInputList()

# Display results
print (f"{numberOfAllPaths(cavemap) = }")
print (f"{numberOfAllPaths(cavemap, part2=True) = }")
Exemplo n.º 26
0

# Check test cases
smallExample1 = '1 + 2 * 3 + 4 * 5 + 6'.split('\n')
smallExample2 = '1 + (2 * 3) + (4 * (5 + 6))'.split('\n')
smallExample3 = '2 * 3 + (4 * 5)'.split('\n')
smallExample4 = '5 + (8 * 3 + 9 + 3 * 4 * 3)'.split('\n')
smallExample5 = '5 * 9 * (7 * 3 * 3 + 9 * 3 + (8 + 6 * 4))'.split('\n')
smallExample6 = '((2 + 4 * 9) * (6 + 9 * 8 + 6) + 6) + 2 + 4 * 2'.split('\n')

assert sumOfExpressionResults(smallExample1) == 71
assert sumOfExpressionResults(smallExample2) == 51
assert sumOfExpressionResults(smallExample3) == 26
assert sumOfExpressionResults(smallExample4) == 437
assert sumOfExpressionResults(smallExample5) == 12240
assert sumOfExpressionResults(smallExample6) == 13632

assert sumOfExpressionResults(smallExample1, part2=True) == 231
assert sumOfExpressionResults(smallExample2, part2=True) == 51
assert sumOfExpressionResults(smallExample3, part2=True) == 46
assert sumOfExpressionResults(smallExample4, part2=True) == 1445
assert sumOfExpressionResults(smallExample5, part2=True) == 669060
assert sumOfExpressionResults(smallExample6, part2=True) == 23340

# Display info message
print("Give a list of expressions:\n")
expression = utility.readInputList()

# Display results
print(f'{sumOfExpressionResults(expression) = }')
print(f'{sumOfExpressionResults(expression, part2 = True) = }')
Exemplo n.º 27
0
        text[i + 1] + text[i] + text[i + 1] for i in range(len(text) - 2)
        if text[i] == text[i + 2] != text[i + 1]
    ]


# Does the IPv7 address support SSL (super-secret listening)?
def superSecretListening(IPv7):
    tokens = re.split('\[|\]', IPv7)
    supernetSequences, hypernetSequences = tokens[::2], tokens[1::2]
    # Collect all ABAs (Area-Broadcast Accessor) inside the supernet sequences and reverse them to BABs
    babCollection = [
        bab for token in supernetSequences
        for bab in potentialByteAllocationBlocks(token)
    ]
    # Check if there is a corresponing BAB (Byte Allocation Block) in the hypernet sequences
    return any(bab in token for bab in babCollection
               for token in hypernetSequences)


assert superSecretListening('aba[bab]xyz')
assert not superSecretListening('xyx[xyx]xyx')
assert superSecretListening('aaa[kek]eke')
assert superSecretListening('zazbz[bzb]cdb')

# Display info message
print("Give a list of IPv7 adresses:\n")
IPv7list = utility.readInputList()

# Display results
print(f"{sum(transportLayerSnooping(IPv7) for IPv7 in IPv7list) = }")
print(f"{sum(superSecretListening(IPv7) for IPv7 in IPv7list) = }")
Exemplo n.º 28
0
    numbers = list(map(int, startingNumbers[0].split(',')))
    age = {}
    for index in range(len(numbers) - 1):
        age[numbers[index]] = index  # initialize dictionary

    lastTurn = 30000000 if part2 else 2020
    prevNum = numbers[-1]

    for index in range(len(numbers) - 1, lastTurn - 1):
        currentNum = index - age[prevNum] if prevNum in age.keys() else 0
        age[prevNum] = index
        prevNum = currentNum

    print(prevNum, len(age.keys()))
    return prevNum


# Check test cases
smallExample = """
0,3,6
""".strip().split('\n')
assert memoryGame(smallExample) == 436
assert memoryGame(smallExample, part2=True) == 175594

# Display info message
print("Give a list of startingNumbers:\n")
startingNumbers = utility.readInputList()

# Display results
print(f'{memoryGame(startingNumbers) = }')
print(f'{memoryGame(startingNumbers, part2 = True) = }')
Exemplo n.º 29
0
    for i in range(start, len(jolts) - 1):
        if (jolts[i + 1] - jolts[i - 1]) <= difference:
            smallerJolts = jolts[:i] + jolts[i + 1:]
            count += countNumberOfArrangements(smallerJolts, difference, i)

    return count


# Check test cases
smallExample = ['16', '10', '15', '5', '1', '11', '7', '19', '6', '12', '4']
largeExample = [
    '28', '33', '18', '42', '31', '14', '46', '20', '48', '47', '24', '23',
    '49', '45', '19', '38', '39', '11', '1', '32', '25', '35', '8', '17', '7',
    '9', '4', '2', '34', '10', '3'
]
assert joltageDifferenceProduct(smallExample, 3) == 35
assert joltageDifferenceProduct(largeExample, 3) == 220

print(f'{differentArrangements(smallExample, 3) = }')
#assert differentArrangements(smallExample, 3) == 8
print(f'{differentArrangements(largeExample, 3) = }')
#assert differentArrangements(largeExample, 3) == 19208

# Display info message
print("Give list of bags and contents:\n")
bagList = utility.readInputList()

# Display results
print(f'{joltageDifferenceProduct(bagList, 3) = }')
print(f'{differentArrangements(bagList, 3) = }')
Exemplo n.º 30
0
# 2. does not contain the letters i, o, or l
# 3. contains at least two different, non-overlapping pairs of letters, like aa
def findNextValid(password):
    # Increment first in case it was already valid
    nextNumberList = next(toNumberList(password))
    # Preprocess ('ghijklmn' becomes 'ghjaaaaa')
    for i in range(len(nextNumberList)):
        if nextNumberList[i] in forbidden:
            nextNumberList[i] += 1
            nextNumberList[i + 1:] = (len(nextNumberList) - i - 1) * [a]
            break
    while not straightThree(nextNumberList) or not doublePair(nextNumberList):
        nextNumberList = next(nextNumberList)
    return toString(nextNumberList)


assert findNextValid('abcdefgh') == 'abcdffaa'
assert findNextValid('ghijklmn') == 'ghjaabcc'

# Display info message
print("Give a password:\n")
password = utility.readInputList(joinedWith='')

# Display results
nextPassword = findNextValid(password)
nextPasswordAfterThat = findNextValid(nextPassword)
print(f'{password} -> {nextPassword} -> {nextPasswordAfterThat}')

#cqjxjnds
#cqjxxyzz
#cqkaabcc