|
| 1 | +# -------------------------------- Input data ---------------------------------------- # |
| 2 | +importos,grid,graph,dot,assembly,re,itertools |
| 3 | +fromcollectionsimportCounter,deque,defaultdict |
| 4 | + |
| 5 | +fromcompassimport* |
| 6 | + |
| 7 | + |
| 8 | +# This functions come from https://github.com/mcpower/adventofcode - Thanks! |
| 9 | +deflmap(func,*iterables): |
| 10 | +returnlist(map(func,*iterables)) |
| 11 | + |
| 12 | + |
| 13 | +defints(s:str): |
| 14 | +returnlmap(int,re.findall(r"-?\d+",s))# thanks mserrano! |
| 15 | + |
| 16 | + |
| 17 | +defpositive_ints(s:str): |
| 18 | +returnlmap(int,re.findall(r"\d+",s))# thanks mserrano! |
| 19 | + |
| 20 | + |
| 21 | +deffloats(s:str): |
| 22 | +returnlmap(float,re.findall(r"-?\d+(?:\.\d+)?",s)) |
| 23 | + |
| 24 | + |
| 25 | +defpositive_floats(s:str): |
| 26 | +returnlmap(float,re.findall(r"\d+(?:\.\d+)?",s)) |
| 27 | + |
| 28 | + |
| 29 | +defwords(s:str): |
| 30 | +returnre.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 | +ifpart_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 | +whileTrue: |
| 76 | +number*=7 |
| 77 | +number%=20201227 |
| 78 | + |
| 79 | +ifnumber==card_public_key: |
| 80 | +card_loop_size=i |
| 81 | +elifnumber==door_public_key: |
| 82 | +door_loop_size=i |
| 83 | + |
| 84 | +ifcard_loop_size!=0anddoor_loop_size!=0: |
| 85 | +break |
| 86 | +i+=1 |
| 87 | + |
| 88 | +# #print (card_loop_size) |
| 89 | +# #print (door_loop_size) |
| 90 | + |
| 91 | +number=1 |
| 92 | +foriinrange(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 |