|
| 1 | +# -------------------------------- Input data -------------------------------- # |
| 2 | +import os |
| 3 | + |
| 4 | +test_data = {} |
| 5 | + |
| 6 | +test = 1 |
| 7 | +test_data[test] = {"input": """aaaaa-bbb-z-y-x-123[abxyz] |
| 8 | +a-b-c-d-e-f-g-h-987[abcde] |
| 9 | +not-a-real-room-404[oarel] |
| 10 | +totally-real-room-200[decoy]""", |
| 11 | + "expected": ['1415', 'Unknown'], |
| 12 | + } |
| 13 | + |
| 14 | +test += 1 |
| 15 | +test_data[test] = {"input": """amppmqgtc-afmamjyrc-bctcjmnkclr-730[jbafl]""", |
| 16 | + "expected": ['Unknown', 'Unknown'], |
| 17 | + } |
| 18 | + |
| 19 | +test = 'real' |
| 20 | +input_file = os.path.join(os.path.dirname(__file__), 'Inputs', os.path.basename(__file__).replace('.py', '.txt')) |
| 21 | +test_data[test] = {"input": open(input_file, "r+").read().strip(), |
| 22 | + "expected": ['173787', '548'], |
| 23 | + } |
| 24 | + |
| 25 | +# -------------------------------- Control program execution -------------------------------- # |
| 26 | + |
| 27 | +case_to_test = 'real' |
| 28 | +part_to_test = 2 |
| 29 | +verbose_level = 1 |
| 30 | + |
| 31 | +# -------------------------------- Initialize some variables -------------------------------- # |
| 32 | + |
| 33 | +puzzle_input = test_data[case_to_test]['input'] |
| 34 | +puzzle_expected_result = test_data[case_to_test]['expected'][part_to_test-1] |
| 35 | +puzzle_actual_result = 'Unknown' |
| 36 | + |
| 37 | + |
| 38 | +# -------------------------------- Actual code execution -------------------------------- # |
| 39 | + |
| 40 | +sum_sectors = 0 |
| 41 | + |
| 42 | +for string in puzzle_input.split('\n'): |
| 43 | + count_letters = {} |
| 44 | + if string == '': |
| 45 | + continue |
| 46 | + for i in range(len(string)): |
| 47 | + letter = string[i] |
| 48 | + if letter == '-': |
| 49 | + continue |
| 50 | + elif letter in 'azertyuiopqsdfghjklmwxcvbn': |
| 51 | + if letter in count_letters: |
| 52 | + count_letters[letter] += 1 |
| 53 | + else: |
| 54 | + count_letters[letter] = 1 |
| 55 | + elif letter in '0123456789': |
| 56 | + sector = int(string[i:i+3]) |
| 57 | + checksum_real = string[i+4:i+9] |
| 58 | + |
| 59 | + checksum_calc = '' |
| 60 | + for count in range (len(string), 0, -1): |
| 61 | + letters = [x for x in count_letters if count_letters[x] == count] |
| 62 | + if letters: |
| 63 | + letters.sort() |
| 64 | + checksum_calc += ''.join(letters) |
| 65 | + if len(checksum_calc) >= 5: |
| 66 | + if checksum_real == checksum_calc[0:5]: |
| 67 | + sum_sectors += sector |
| 68 | + |
| 69 | + |
| 70 | + decrypted_room_name = [chr((ord(letter) - 97 + sector) % 26 + 97) if letter != '-' else ' ' for letter in string] |
| 71 | + if 'north' in ''.join(decrypted_room_name): |
| 72 | + if part_to_test == 2: |
| 73 | + puzzle_actual_result = sector |
| 74 | + |
| 75 | + break |
| 76 | + break |
| 77 | + if part_to_test == 1: |
| 78 | + puzzle_actual_result = sum_sectors |
| 79 | + |
| 80 | + |
| 81 | +# -------------------------------- Outputs / results -------------------------------- # |
| 82 | + |
| 83 | +if verbose_level >= 3: |
| 84 | + print ('Input : ' + puzzle_input) |
| 85 | +print ('Expected result : ' + str(puzzle_expected_result)) |
| 86 | +print ('Actual result : ' + str(puzzle_actual_result)) |
| 87 | + |
| 88 | + |
| 89 | + |
| 90 | + |
0 commit comments