|
| 1 | +# -------------------------------- Input data -------------------------------- # |
| 2 | +import os |
| 3 | + |
| 4 | +test_data = {} |
| 5 | + |
| 6 | +test = 1 |
| 7 | +test_data[test] = {"input": """Begin in state A. |
| 8 | +Perform a diagnostic checksum after 6 steps. |
| 9 | +
|
| 10 | +In state A: |
| 11 | + If the current value is 0: |
| 12 | + - Write the value 1. |
| 13 | + - Move one slot to the right. |
| 14 | + - Continue with state B. |
| 15 | + If the current value is 1: |
| 16 | + - Write the value 0. |
| 17 | + - Move one slot to the left. |
| 18 | + - Continue with state B. |
| 19 | +
|
| 20 | +In state B: |
| 21 | + If the current value is 0: |
| 22 | + - Write the value 1. |
| 23 | + - Move one slot to the left. |
| 24 | + - Continue with state A. |
| 25 | + If the current value is 1: |
| 26 | + - Write the value 1. |
| 27 | + - Move one slot to the right. |
| 28 | + - Continue with state A.""", |
| 29 | + "expected": ['3', 'Unknown'], |
| 30 | + } |
| 31 | + |
| 32 | +test = 'real' |
| 33 | +input_file = os.path.join(os.path.dirname(__file__), 'Inputs', os.path.basename(__file__).replace('.py', '.txt')) |
| 34 | +test_data[test] = {"input": open(input_file, "r+").read().strip(), |
| 35 | + "expected": ['2794', 'Unknown'], |
| 36 | + } |
| 37 | + |
| 38 | +# -------------------------------- Control program execution -------------------------------- # |
| 39 | + |
| 40 | +case_to_test = 'real' |
| 41 | +part_to_test = 1 |
| 42 | +verbose_level = 1 |
| 43 | + |
| 44 | +# -------------------------------- Initialize some variables -------------------------------- # |
| 45 | + |
| 46 | +puzzle_input = test_data[case_to_test]['input'] |
| 47 | +puzzle_expected_result = test_data[case_to_test]['expected'][part_to_test-1] |
| 48 | +puzzle_actual_result = 'Unknown' |
| 49 | + |
| 50 | + |
| 51 | +# -------------------------------- Actual code execution -------------------------------- # |
| 52 | + |
| 53 | +if part_to_test == 1: |
| 54 | + states = {} |
| 55 | + for string in puzzle_input.split('\n'): |
| 56 | + if string == '': |
| 57 | + continue |
| 58 | + |
| 59 | + if string.startswith('Begin in state '): |
| 60 | + start_state = string[-2:-1] |
| 61 | + elif string.startswith('Perform a diagnostic checksum after '): |
| 62 | + _,_,_,_,_, steps, _ = string.split(' ') |
| 63 | + steps = int(steps) |
| 64 | + elif string.startswith('In state '): |
| 65 | + state = string[-2:-1] |
| 66 | + elif string.startswith(' If the current value is'): |
| 67 | + current_value = int(string[-2:-1]) |
| 68 | + elif string.startswith(' - Write the value'): |
| 69 | + target_value = int(string[-2:-1]) |
| 70 | + elif string.startswith(' - Move one slot to the'): |
| 71 | + direction = string.split(' ')[-1] |
| 72 | + elif string.startswith(' - Continue with state'): |
| 73 | + next_state = string[-2:-1] |
| 74 | + if state not in states: |
| 75 | + states[state] = {} |
| 76 | + states[state].update({current_value: (target_value, direction, next_state)}) |
| 77 | + |
| 78 | + state = start_state |
| 79 | + tape = {0:0} |
| 80 | + position = 0 |
| 81 | + |
| 82 | + for _ in range (steps): |
| 83 | + value = tape[position] if position in tape else 0 |
| 84 | + tape[position] = states[state][value][0] |
| 85 | + position += 1 if states[state][value][1] == 'right.' else -1 |
| 86 | + state = states[state][value][2] |
| 87 | + |
| 88 | + puzzle_actual_result = sum(tape[x] for x in tape) |
| 89 | + |
| 90 | + |
| 91 | + |
| 92 | +else: |
| 93 | + pass |
| 94 | + |
| 95 | + |
| 96 | + |
| 97 | + |
| 98 | +# -------------------------------- Outputs / results -------------------------------- # |
| 99 | + |
| 100 | +if verbose_level >= 3: |
| 101 | + print ('Input : ' + puzzle_input) |
| 102 | +print ('Expected result : ' + str(puzzle_expected_result)) |
| 103 | +print ('Actual result : ' + str(puzzle_actual_result)) |
| 104 | + |
| 105 | + |
| 106 | + |
| 107 | + |
0 commit comments