|
| 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": """5764801 |
| 38 | +17807724""", |
| 39 | + "expected": ["14897079", "Unknown"], |
| 40 | +} |
| 41 | + |
| 42 | +test = "real" |
| 43 | +input_file = os.path.join( |
| 44 | + os.path.dirname(__file__), |
| 45 | + "Inputs", |
| 46 | + os.path.basename(__file__).replace(".py", ".txt"), |
| 47 | +) |
| 48 | +test_data[test] = { |
| 49 | + "input": open(input_file, "r+").read(), |
| 50 | + "expected": ["18293391", "Unknown"], |
| 51 | +} |
| 52 | + |
| 53 | + |
| 54 | +# -------------------------------- Control program execution ------------------------- # |
| 55 | + |
| 56 | +case_to_test = "real" |
| 57 | +part_to_test = 1 |
| 58 | + |
| 59 | +# -------------------------------- Initialize some variables ------------------------- # |
| 60 | + |
| 61 | +puzzle_input = test_data[case_to_test]["input"] |
| 62 | +puzzle_expected_result = test_data[case_to_test]["expected"][part_to_test - 1] |
| 63 | +puzzle_actual_result = "Unknown" |
| 64 | + |
| 65 | + |
| 66 | +# -------------------------------- Actual code execution ----------------------------- # |
| 67 | + |
| 68 | +if part_to_test == 1: |
| 69 | + card_public_key, door_public_key = ints(puzzle_input) |
| 70 | + |
| 71 | + number = 1 |
| 72 | + i = 1 |
| 73 | + card_loop_size = 0 |
| 74 | + door_loop_size = 0 |
| 75 | + while True: |
| 76 | + number *= 7 |
| 77 | + number %= 20201227 |
| 78 | + |
| 79 | + if number == card_public_key: |
| 80 | + card_loop_size = i |
| 81 | + elif number == door_public_key: |
| 82 | + door_loop_size = i |
| 83 | + |
| 84 | + if card_loop_size != 0 and door_loop_size != 0: |
| 85 | + break |
| 86 | + i += 1 |
| 87 | + |
| 88 | + # #print (card_loop_size) |
| 89 | + # #print (door_loop_size) |
| 90 | + |
| 91 | + number = 1 |
| 92 | + for i in range(door_loop_size): |
| 93 | + number *= card_public_key |
| 94 | + number %= 20201227 |
| 95 | + encryption_key = number |
| 96 | + |
| 97 | + puzzle_actual_result = encryption_key |
| 98 | + |
| 99 | + |
| 100 | +# -------------------------------- Outputs / results --------------------------------- # |
| 101 | + |
| 102 | +print("Case :", case_to_test, "- Part", part_to_test) |
| 103 | +print("Expected result : " + str(puzzle_expected_result)) |
| 104 | +print("Actual result : " + str(puzzle_actual_result)) |
| 105 | +# Date created: 2020-12-25 06:00:01.023157 |
| 106 | +# Part 1: 2020-12-25 06:17:12 |
| 107 | +# Part 2: 2020-12-25 06:17:23 |
0 commit comments