|
| 1 | +# 'Advent of code' solution for year 2024 day 5 |
| 2 | +import os |
| 3 | +import sys |
| 4 | + |
| 5 | +global DIR_PATH |
| 6 | +DIR_PATH = os.path.dirname(os.path.realpath(__file__)) |
| 7 | + |
| 8 | +def isUpdateCorrect(page_ordering: list[(int, int)], update: list[int]) -> tuple[bool, list[int]]: |
| 9 | + """Check if the given update is correct |
| 10 | + :param page_ordering: list of dependencies |
| 11 | + :param update: list of pages |
| 12 | + :return: True if the update is correct, False otherwise, |
| 13 | + :return: list of pages in correct order |
| 14 | + """ |
| 15 | + _dependencies = {} # page -> dependencies |
| 16 | + _print_order = [] |
| 17 | + fixed_update = update.copy() |
| 18 | + isCorrect = True |
| 19 | + |
| 20 | + for dependency in page_ordering: |
| 21 | + # both pages need to be in the update |
| 22 | + if not dependency[0] in update or not dependency[1] in update: |
| 23 | + continue |
| 24 | + if dependency[1] not in _dependencies: |
| 25 | + _dependencies[dependency[1]] = [dependency[0]] |
| 26 | + elif dependency[0] not in _dependencies[dependency[1]]: |
| 27 | + _dependencies[dependency[1]].append(dependency[0]) |
| 28 | + |
| 29 | + for page in update: |
| 30 | + # page has no dependencies |
| 31 | + if not page in _dependencies.keys(): |
| 32 | + _print_order.append(page) |
| 33 | + continue |
| 34 | + |
| 35 | + # page was rendered before all dependencies |
| 36 | + for dependency in _dependencies[page]: |
| 37 | + if dependency not in _print_order: |
| 38 | + isCorrect = False |
| 39 | + fixed_update.remove(page) |
| 40 | + fixed_update.insert(fixed_update.index(dependency) + 1, page) |
| 41 | + |
| 42 | + _print_order.append(page) |
| 43 | + |
| 44 | + return isCorrect, fixed_update |
| 45 | + |
| 46 | +def part1(page_ordering: list[(int, int)], updates: list[(int, int)]) -> int: |
| 47 | + """Find all correct udpates and sum the middle pages""" |
| 48 | + page_sum = 0 |
| 49 | + |
| 50 | + for update in updates: |
| 51 | + if isUpdateCorrect(page_ordering, update)[0]: |
| 52 | + _len = len(update) |
| 53 | + page_sum += update[_len // 2] |
| 54 | + |
| 55 | + return page_sum |
| 56 | + |
| 57 | +def part2(page_ordering: list[(int, int)], updates: list[(int, int)]) -> int: |
| 58 | + """Correct invalid udpates and sum the middle pages""" |
| 59 | + page_sum = 0 |
| 60 | + |
| 61 | + |
| 62 | + for update in updates: |
| 63 | + correct, fixed = isUpdateCorrect(page_ordering, update) |
| 64 | + if correct: |
| 65 | + continue |
| 66 | + |
| 67 | + while not correct: |
| 68 | + correct, fixed = isUpdateCorrect(page_ordering, fixed) |
| 69 | + _len = len(fixed) |
| 70 | + page_sum += fixed[_len // 2] |
| 71 | + |
| 72 | + return page_sum |
| 73 | + |
| 74 | +def get_input(): |
| 75 | + input = None |
| 76 | + if os.path.isfile(os.path.join(DIR_PATH, "input.txt")): |
| 77 | + with open(os.path.join(DIR_PATH, "input.txt"), "r") as file: |
| 78 | + input = file.read().strip().split("\n\n") |
| 79 | + return input |
| 80 | + else: |
| 81 | + print("Error! Input file does not exist!") |
| 82 | + sys.exit() |
| 83 | + |
| 84 | +if __name__ == "__main__": |
| 85 | + input = get_input() |
| 86 | + if not input: |
| 87 | + print("Error! Input file is empty!") |
| 88 | + sys.exit() |
| 89 | + |
| 90 | + # parse input |
| 91 | + page_ordering = input[0].split("\n") |
| 92 | + page_ordering = [[int(num) for num in dependeny.split("|")] for dependeny in page_ordering] |
| 93 | + updates = input[1].split("\n") |
| 94 | + updates = [[int(num) for num in update.split(",")] for update in updates] |
| 95 | + |
| 96 | + result1 = part1(page_ordering, updates) |
| 97 | + print(f"Part 1: {result1}") |
| 98 | + result2 = part2(page_ordering, updates) |
| 99 | + print(f"Part 2: {result2}") |
| 100 | + |
| 101 | + sys.exit() |
0 commit comments