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