|
| 1 | +# -------------------------------- Input data ---------------------------------------- # |
| 2 | +import os, grid, graph, dot, assembly, re, itertools |
| 3 | +from collections import Counter, deque, defaultdict |
| 4 | + |
| 5 | +from compass import * |
| 6 | + |
| 7 | + |
| 8 | +# This functions come from https://github.com/mcpower/adventofcode - Thanks! |
| 9 | +def lmap(func, *iterables): |
| 10 | + return list(map(func, *iterables)) |
| 11 | + |
| 12 | + |
| 13 | +def ints(s: str): |
| 14 | + return lmap(int, re.findall(r"-?\d+", s)) # thanks mserrano! |
| 15 | + |
| 16 | + |
| 17 | +def positive_ints(s: str): |
| 18 | + return lmap(int, re.findall(r"\d+", s)) # thanks mserrano! |
| 19 | + |
| 20 | + |
| 21 | +def floats(s: str): |
| 22 | + return lmap(float, re.findall(r"-?\d+(?:\.\d+)?", s)) |
| 23 | + |
| 24 | + |
| 25 | +def positive_floats(s: str): |
| 26 | + return lmap(float, re.findall(r"\d+(?:\.\d+)?", s)) |
| 27 | + |
| 28 | + |
| 29 | +def words(s: str): |
| 30 | + return re.findall(r"[a-zA-Z]+", s) |
| 31 | + |
| 32 | + |
| 33 | +test_data = {} |
| 34 | + |
| 35 | +test = 1 |
| 36 | +test_data[test] = { |
| 37 | + "input": """abc |
| 38 | +
|
| 39 | +a |
| 40 | +b |
| 41 | +c |
| 42 | +
|
| 43 | +ab |
| 44 | +ac |
| 45 | +
|
| 46 | +a |
| 47 | +a |
| 48 | +a |
| 49 | +a |
| 50 | +
|
| 51 | +b""", |
| 52 | + "expected": ["11", "6"], |
| 53 | +} |
| 54 | + |
| 55 | +test = "real" |
| 56 | +input_file = os.path.join( |
| 57 | + os.path.dirname(__file__), |
| 58 | + "Inputs", |
| 59 | + os.path.basename(__file__).replace(".py", ".txt"), |
| 60 | +) |
| 61 | +test_data[test] = { |
| 62 | + "input": open(input_file, "r+").read(), |
| 63 | + "expected": ["6782", "3596"], |
| 64 | +} |
| 65 | + |
| 66 | + |
| 67 | +# -------------------------------- Control program execution ------------------------- # |
| 68 | + |
| 69 | +case_to_test = 1 |
| 70 | +part_to_test = 2 |
| 71 | + |
| 72 | +# -------------------------------- Initialize some variables ------------------------- # |
| 73 | + |
| 74 | +puzzle_input = test_data[case_to_test]["input"] |
| 75 | +puzzle_expected_result = test_data[case_to_test]["expected"][part_to_test - 1] |
| 76 | +puzzle_actual_result = "Unknown" |
| 77 | + |
| 78 | + |
| 79 | +# -------------------------------- Actual code execution ----------------------------- # |
| 80 | + |
| 81 | +if part_to_test == 1: |
| 82 | + total_score = 0 |
| 83 | + for group in puzzle_input.split("\n\n"): |
| 84 | + group_size = len(group.split("\n")) |
| 85 | + answers = Counter(group.replace("\n", "")) |
| 86 | + nb_common = len(answers) |
| 87 | + total_score = total_score + nb_common |
| 88 | + |
| 89 | + puzzle_actual_result = total_score |
| 90 | + |
| 91 | + |
| 92 | +else: |
| 93 | + total_score = 0 |
| 94 | + for group in puzzle_input.split("\n\n"): |
| 95 | + group_size = len(group.split("\n")) |
| 96 | + answers = Counter(group.replace("\n", "")) |
| 97 | + nb_common = len([x for x in answers if answers[x] == group_size]) |
| 98 | + total_score = total_score + nb_common |
| 99 | + |
| 100 | + puzzle_actual_result = total_score |
| 101 | + |
| 102 | + |
| 103 | +# -------------------------------- Outputs / results --------------------------------- # |
| 104 | + |
| 105 | +print("Case :", case_to_test, "- Part", part_to_test) |
| 106 | +print("Expected result : " + str(puzzle_expected_result)) |
| 107 | +print("Actual result : " + str(puzzle_actual_result)) |
0 commit comments