|
| 1 | +#!/usr/bin/python3 |
| 2 | +# |
| 3 | +# --- Advent Of Code 2022 --- |
| 4 | +# |
| 5 | +# Santa's reindeer typically eat regular reindeer food, but they need a lot of magical energy to |
| 6 | +# deliver presents on Christmas. For that, their favorite snack is a special type of star fruit that |
| 7 | +# only grows deep in the jungle. The Elves have brought you on their annual expedition to the grove |
| 8 | +# where the fruit grows. |
| 9 | +# |
| 10 | +# To supply enough magical energy, the expedition needs to retrieve a minimum of fifty stars by |
| 11 | +# December 25th. Although the Elves assure you that the grove has plenty of fruit, you decide to |
| 12 | +# grab any fruit you see along the way, just in case. |
| 13 | +# |
| 14 | +# Collect stars by solving puzzles. Two puzzles will be made available on each day in the Advent |
| 15 | +# calendar; the second puzzle is unlocked when you complete the first. Each puzzle grants one star. |
| 16 | +# Good luck! |
| 17 | + |
| 18 | +import sys, os |
| 19 | + |
| 20 | +def runAll(): |
| 21 | + |
| 22 | + for d in range(1, 31): |
| 23 | + runDay(d) |
| 24 | + |
| 25 | +def runDay(day, part=None, runExample=False): |
| 26 | + |
| 27 | + if(part != None): |
| 28 | + print("- Run day %d - Part %d:" % (day, part)) |
| 29 | + else: |
| 30 | + print("- Run day %d:" % (day)) |
| 31 | + |
| 32 | + directory = "day{:02d}".format(day) |
| 33 | + if not os.path.exists("./"+directory): |
| 34 | + print("Day %d skipped, dir '%s' not found" % (day, directory)) |
| 35 | + return |
| 36 | + |
| 37 | + if not os.path.exists("./"+directory+"/day.py"): |
| 38 | + print("Day %d skipped, file '%s/day.py' not found" % (day, directory)) |
| 39 | + return |
| 40 | + |
| 41 | + if runExample == True: |
| 42 | + if not os.path.exists("./"+directory+"/example.txt"): |
| 43 | + print("Day %d skipped, file '%s/example.txt' not found" % (day, directory)) |
| 44 | + return |
| 45 | + else: |
| 46 | + if not os.path.exists("./"+directory+"/input.txt"): |
| 47 | + print("Day %d skipped, file '%s/input.txt' not found" % (day, directory)) |
| 48 | + return |
| 49 | + |
| 50 | + module = "day{:02d}.day".format(day) |
| 51 | + |
| 52 | + try: |
| 53 | + moduleObj = __import__(module) |
| 54 | + submodule = getattr(moduleObj, 'day') # returns the module object "modules.update" |
| 55 | + puzzle = getattr(submodule, 'puzzle') # returns the module object "modules.update" |
| 56 | + |
| 57 | + except ImportError: |
| 58 | + return |
| 59 | + |
| 60 | + # Open text file in read mode and read puzzle input |
| 61 | + # |
| 62 | + if runExample == True: |
| 63 | + input_file = open("./"+directory+"/example.txt", "r") |
| 64 | + else: |
| 65 | + input_file = open("./"+directory+"/input.txt", "r") |
| 66 | + |
| 67 | + puzzle_input = input_file.read().strip() |
| 68 | + input_file.close() |
| 69 | + |
| 70 | + if puzzle_input == "": |
| 71 | + print("Day %d skipped, input from file was empty" % (day, directory)) |
| 72 | + return |
| 73 | + |
| 74 | + puzzle = puzzle() |
| 75 | + puzzle.setInput(puzzle_input) |
| 76 | + puzzle.handleInput() |
| 77 | + |
| 78 | + if part==None or part == 1: |
| 79 | + # from day01.solve import day |
| 80 | + answer = puzzle.part1() |
| 81 | + print("- Answer for day %d - Part 1: %s" % (day, answer)) |
| 82 | + |
| 83 | + if part==None or part == 2: |
| 84 | + answer = puzzle.part2() |
| 85 | + print("- Answer for day %d - Part 2: %s" % (day, answer)) |
| 86 | + |
| 87 | + print("--------------------------------------") |
| 88 | + |
| 89 | +def runPart(day, part, runExample=False): |
| 90 | + return runDay(day, part, runExample) |
| 91 | + |
| 92 | +def checkAnswer(day, part, answer): |
| 93 | + |
| 94 | + answers = { |
| 95 | + 1: {1: 75501, 2: 215594}, |
| 96 | + 2: {1: 14163, 2: 12091} |
| 97 | + } |
| 98 | + |
| 99 | + return None |
| 100 | + |
| 101 | +if __name__ == "__main__": |
| 102 | + |
| 103 | + print("--- Advent Of Code 2022 ---") |
| 104 | + |
| 105 | + if len(sys.argv) == 1: |
| 106 | + runAll() |
| 107 | + |
| 108 | + elif len(sys.argv) == 2: |
| 109 | + runDay(int(sys.argv[1])) |
| 110 | + |
| 111 | + elif len(sys.argv) == 3: |
| 112 | + if sys.argv[2] == "example": |
| 113 | + runDay(int(sys.argv[1]), None, True) |
| 114 | + else: |
| 115 | + runPart(int(sys.argv[1]), int(sys.argv[2])) |
| 116 | + |
| 117 | + elif len(sys.argv) == 4: |
| 118 | + if sys.argv[3] == "example": |
| 119 | + runPart(int(sys.argv[1]), int(sys.argv[2]), True) |
0 commit comments