|
| 1 | +# -------------------------------- Input data -------------------------------- # |
| 2 | +import os |
| 3 | + |
| 4 | +test_data = {} |
| 5 | + |
| 6 | +test = 1 |
| 7 | +test_data[test] = {"input": ('abcde', """s1,x3/4,pe/b"""), |
| 8 | + "expected": ['Unknown', 'Unknown'], |
| 9 | + } |
| 10 | + |
| 11 | +test = 'real' |
| 12 | +input_file = os.path.join(os.path.dirname(__file__), 'Inputs', os.path.basename(__file__).replace('.py', '.txt')) |
| 13 | +test_data[test] = {"input": ('abcdefghijklmnop', open(input_file, "r+").read().strip()), |
| 14 | + "expected": ['ceijbfoamgkdnlph', 'Unknown'], |
| 15 | + } |
| 16 | + |
| 17 | +# -------------------------------- Control program execution -------------------------------- # |
| 18 | + |
| 19 | +case_to_test = 'real' |
| 20 | +part_to_test = 2 |
| 21 | +verbose_level = 1 |
| 22 | + |
| 23 | +# -------------------------------- Initialize some variables -------------------------------- # |
| 24 | + |
| 25 | +puzzle_input = test_data[case_to_test]['input'] |
| 26 | +puzzle_expected_result = test_data[case_to_test]['expected'][part_to_test-1] |
| 27 | +puzzle_actual_result = 'Unknown' |
| 28 | + |
| 29 | + |
| 30 | +# -------------------------------- Actual code execution -------------------------------- # |
| 31 | + |
| 32 | + |
| 33 | + |
| 34 | +if part_to_test == 1: |
| 35 | + programs = puzzle_input[0] |
| 36 | + for string in puzzle_input[1].split(','): |
| 37 | + if string[0] == 's': |
| 38 | + shift = int(string[1:]) |
| 39 | + programs = programs[-shift:] + programs[:-shift] |
| 40 | + elif string[0] == 'x': |
| 41 | + a, b = int(string[1:].split('/')[0]), int(string[1:].split('/')[1]) |
| 42 | + a, b = min(a, b), max(a, b) |
| 43 | + programs = programs[:a] + programs[b] + programs[a+1:b] + programs[a] + programs[b+1:] |
| 44 | + elif string[0] == 'p': |
| 45 | + a, b = string[1:].split('/')[0], string[1:].split('/')[1] |
| 46 | + programs = programs.replace(a, '#').replace(b, a).replace('#', b) |
| 47 | + |
| 48 | + puzzle_actual_result = programs |
| 49 | + |
| 50 | + |
| 51 | + |
| 52 | +else: |
| 53 | + programs = puzzle_input[0] |
| 54 | + positions = [programs] |
| 55 | + i = 0 |
| 56 | + while i < 10**9: |
| 57 | + i += 1 |
| 58 | + for string in puzzle_input[1].split(','): |
| 59 | + if string[0] == 's': |
| 60 | + shift = int(string[1:]) |
| 61 | + programs = programs[-shift:] + programs[:-shift] |
| 62 | + elif string[0] == 'x': |
| 63 | + a, b = int(string[1:].split('/')[0]), int(string[1:].split('/')[1]) |
| 64 | + a, b = min(a, b), max(a, b) |
| 65 | + programs = programs[:a] + programs[b] + programs[a+1:b] + programs[a] + programs[b+1:] |
| 66 | + elif string[0] == 'p': |
| 67 | + a, b = string[1:].split('/')[0], string[1:].split('/')[1] |
| 68 | + programs = programs.replace(a, '#').replace(b, a).replace('#', b) |
| 69 | + |
| 70 | + if programs in positions: |
| 71 | + cycle_length = i - positions.index(programs) |
| 72 | + i += (10**9 // cycle_length - 1) * cycle_length |
| 73 | + print ('cycle length', cycle_length) |
| 74 | + |
| 75 | + |
| 76 | + puzzle_actual_result = programs |
| 77 | + |
| 78 | + |
| 79 | + |
| 80 | +# -------------------------------- Outputs / results -------------------------------- # |
| 81 | + |
| 82 | +if verbose_level >= 3: |
| 83 | + print ('Input : ' + puzzle_input) |
| 84 | +print ('Expected result : ' + str(puzzle_expected_result)) |
| 85 | +print ('Actual result : ' + str(puzzle_actual_result)) |
| 86 | + |
| 87 | + |
| 88 | + |
| 89 | + |
0 commit comments