|
| 1 | +# -------------------------------- Input data -------------------------------- # |
| 2 | +import os, hashlib |
| 3 | + |
| 4 | +test_data = {} |
| 5 | + |
| 6 | +test = 1 |
| 7 | +test_data[test] = {"input": """abc""", |
| 8 | + "expected": ['22728', '22551'], |
| 9 | + } |
| 10 | + |
| 11 | +test += 1 |
| 12 | +test_data[test] = {"input": """""", |
| 13 | + "expected": ['Unknown', 'Unknown'], |
| 14 | + } |
| 15 | + |
| 16 | +test = 'real' |
| 17 | +test_data[test] = {"input": 'qzyelonm', |
| 18 | + "expected": ['15168', '20864'], |
| 19 | + } |
| 20 | + |
| 21 | +# -------------------------------- Control program execution -------------------------------- # |
| 22 | + |
| 23 | +case_to_test = 'real' |
| 24 | +part_to_test = 2 |
| 25 | +verbose_level = 1 |
| 26 | + |
| 27 | +# -------------------------------- Initialize some variables -------------------------------- # |
| 28 | + |
| 29 | +puzzle_input = test_data[case_to_test]['input'] |
| 30 | +puzzle_expected_result = test_data[case_to_test]['expected'][part_to_test-1] |
| 31 | +puzzle_actual_result = 'Unknown' |
| 32 | + |
| 33 | + |
| 34 | +# -------------------------------- Actual code execution -------------------------------- # |
| 35 | + |
| 36 | +if part_to_test == 1: |
| 37 | + index = 0 |
| 38 | + found_keys = 0 |
| 39 | + while True: |
| 40 | + index += 1 |
| 41 | + init_hash = hashlib.md5((puzzle_input + str(index)).encode('utf-8')).hexdigest() |
| 42 | + triplets = [x for x in '0123456789abcdef' if x*3 in init_hash] |
| 43 | + |
| 44 | + if triplets: |
| 45 | + first_triplet_position = min ([init_hash.find(x*3) for x in triplets]) |
| 46 | + triplet = init_hash[first_triplet_position] |
| 47 | + |
| 48 | + for i in range (1, 1000): |
| 49 | + new_hash = hashlib.md5((puzzle_input + str(index + i)).encode('utf-8')).hexdigest() |
| 50 | + if triplet * 5 in new_hash: |
| 51 | + found_keys += 1 |
| 52 | + print (init_hash, triplet, index, index + i, found_keys) |
| 53 | + break |
| 54 | + |
| 55 | + if found_keys == 64: |
| 56 | + puzzle_actual_result = index |
| 57 | + break |
| 58 | + |
| 59 | + |
| 60 | +else: |
| 61 | + hashes = [] |
| 62 | + hashes_first_triplet = {} |
| 63 | + hashes_quintuplets = {} |
| 64 | + index = -1 |
| 65 | + found_keys = 0 |
| 66 | + |
| 67 | + for i in range (30000): |
| 68 | + hash_text = puzzle_input + str(i) |
| 69 | + for y in range (2017): |
| 70 | + hash_text = hashlib.md5(hash_text.encode('utf-8')).hexdigest() |
| 71 | + hashes.append(hash_text) |
| 72 | + |
| 73 | + triplets = [x for x in '0123456789abcdef' if x*3 in hash_text] |
| 74 | + |
| 75 | + if triplets: |
| 76 | + first_triplet_position = min ([hash_text.find(x*3) for x in triplets]) |
| 77 | + hashes_first_triplet[i] = hash_text[first_triplet_position] |
| 78 | + |
| 79 | + quintuplets = [x for x in '0123456789abcdef' if x*5 in hash_text] |
| 80 | + |
| 81 | + if quintuplets: |
| 82 | + hashes_quintuplets[i] = quintuplets |
| 83 | + |
| 84 | + if i % 100 == 0: |
| 85 | + print ('calculated', i) |
| 86 | + |
| 87 | + print ('Calculated hashes') |
| 88 | + |
| 89 | + |
| 90 | + print (hashes_first_triplet) |
| 91 | + print (hashes_quintuplets) |
| 92 | + |
| 93 | + for index, triplet in hashes_first_triplet.items(): |
| 94 | + for i in range (1, 1000): |
| 95 | + if index + i in hashes_quintuplets: |
| 96 | + if triplet in hashes_quintuplets[index + i]: |
| 97 | + found_keys += 1 |
| 98 | + print (hashes[index], triplet, index, index + i, found_keys) |
| 99 | + break |
| 100 | + |
| 101 | + if found_keys == 64: |
| 102 | + puzzle_actual_result = index |
| 103 | + break |
| 104 | + |
| 105 | + |
| 106 | + |
| 107 | +# -------------------------------- Outputs / results -------------------------------- # |
| 108 | + |
| 109 | +if verbose_level >= 3: |
| 110 | + print ('Input : ' + puzzle_input) |
| 111 | +print ('Expected result : ' + str(puzzle_expected_result)) |
| 112 | +print ('Actual result : ' + str(puzzle_actual_result)) |
| 113 | + |
| 114 | + |
| 115 | + |
| 116 | + |
0 commit comments