|
| 1 | +# -------------------------------- Input data -------------------------------- # |
| 2 | +import os |
| 3 | + |
| 4 | +test_data = {} |
| 5 | + |
| 6 | +test = 1 |
| 7 | +test_data[test] = {"input": """abba[mnop]qrst |
| 8 | +abcd[bddb]xyyx |
| 9 | +aaaa[qwer]tyui |
| 10 | +ioxxoj[asdfgh]zxcvbn""", |
| 11 | + "expected": ['Unknown', 'Unknown'], |
| 12 | + } |
| 13 | + |
| 14 | +test += 1 |
| 15 | +test_data[test] = {"input": """aba[bab]xyz |
| 16 | +xyx[xyx]xyx |
| 17 | +aaa[kek]eke |
| 18 | +zazbz[bzb]cdb""", |
| 19 | + "expected": ['Unknown', 'Unknown'], |
| 20 | + } |
| 21 | + |
| 22 | +test = 'real' |
| 23 | +input_file = os.path.join(os.path.dirname(__file__), 'Inputs', os.path.basename(__file__).replace('.py', '.txt')) |
| 24 | +test_data[test] = {"input": open(input_file, "r+").read().strip(), |
| 25 | + "expected": ['115', 'Unknown'], |
| 26 | + } |
| 27 | + |
| 28 | +# -------------------------------- Control program execution -------------------------------- # |
| 29 | + |
| 30 | +case_to_test = 'real' |
| 31 | +part_to_test = 2 |
| 32 | +verbose_level = 1 |
| 33 | + |
| 34 | +# -------------------------------- Initialize some variables -------------------------------- # |
| 35 | + |
| 36 | +puzzle_input = test_data[case_to_test]['input'] |
| 37 | +puzzle_expected_result = test_data[case_to_test]['expected'][part_to_test-1] |
| 38 | +puzzle_actual_result = 'Unknown' |
| 39 | + |
| 40 | + |
| 41 | +# -------------------------------- Actual code execution -------------------------------- # |
| 42 | + |
| 43 | +if part_to_test == 1: |
| 44 | + count_abba = 0 |
| 45 | + for string in puzzle_input.split('\n'): |
| 46 | + abba = False |
| 47 | + if string == '': |
| 48 | + continue |
| 49 | + |
| 50 | + in_brackets = False |
| 51 | + |
| 52 | + for index in range(len(string)-3): |
| 53 | + if string[index] == '[': |
| 54 | + in_brackets = True |
| 55 | + continue |
| 56 | + elif string[index] == ']': |
| 57 | + in_brackets = False |
| 58 | + continue |
| 59 | + |
| 60 | + if string[index] == string[index+3] and string[index+1] == string[index+2] and string[index] != string[index+1]: |
| 61 | + if in_brackets: |
| 62 | + abba = False |
| 63 | + break |
| 64 | + else: |
| 65 | + abba = True |
| 66 | + if abba: |
| 67 | + count_abba += 1 |
| 68 | + puzzle_actual_result = count_abba |
| 69 | + |
| 70 | +else: |
| 71 | + ssl_support = 0 |
| 72 | + for string in puzzle_input.split('\n'): |
| 73 | + aba_sequences = [] |
| 74 | + bab_sequences = [] |
| 75 | + if string == '': |
| 76 | + continue |
| 77 | + |
| 78 | + in_brackets = False |
| 79 | + |
| 80 | + for index in range(len(string)-2): |
| 81 | + if string[index] == '[': |
| 82 | + in_brackets = True |
| 83 | + continue |
| 84 | + elif string[index] == ']': |
| 85 | + in_brackets = False |
| 86 | + continue |
| 87 | + |
| 88 | + if string[index] == string[index+2] and string[index] != string[index+1]: |
| 89 | + if in_brackets: |
| 90 | + aba_sequences.append(string[index:index+3]) |
| 91 | + else: |
| 92 | + bab_sequences.append(string[index:index+3]) |
| 93 | + matching = [x for x in aba_sequences if x[1] + x[0] + x[1] in bab_sequences] |
| 94 | + |
| 95 | + if matching: |
| 96 | + ssl_support += 1 |
| 97 | + puzzle_actual_result = ssl_support |
| 98 | + |
| 99 | + |
| 100 | +# -------------------------------- Outputs / results -------------------------------- # |
| 101 | + |
| 102 | +if verbose_level >= 3: |
| 103 | + print ('Input : ' + puzzle_input) |
| 104 | +print ('Expected result : ' + str(puzzle_expected_result)) |
| 105 | +print ('Actual result : ' + str(puzzle_actual_result)) |
| 106 | + |
| 107 | + |
| 108 | + |
| 109 | + |
0 commit comments