|
4 | 4 | test_data = {}
|
5 | 5 |
|
6 | 6 | test = 1
|
7 |
| -test_data[test] = {"input": """cpy 41 a |
| 7 | +test_data[test] = { |
| 8 | + "input": """cpy 41 a |
8 | 9 | inc a
|
9 | 10 | inc a
|
10 | 11 | dec a
|
11 | 12 | jnz a 2
|
12 | 13 | dec a""",
|
13 |
| - "expected": ['42', 'Unknown'], |
14 |
| - } |
| 14 | + "expected": ["42", "Unknown"], |
| 15 | +} |
15 | 16 |
|
16 | 17 | test += 1
|
17 |
| -test_data[test] = {"input": """""", |
18 |
| - "expected": ['Unknown', 'Unknown'], |
19 |
| - } |
20 |
| - |
21 |
| -test = 'real' |
22 |
| -input_file = os.path.join(os.path.dirname(__file__), 'Inputs', os.path.basename(__file__).replace('.py', '.txt')) |
23 |
| -test_data[test] = {"input": open(input_file, "r+").read().strip(), |
24 |
| - "expected": ['318083', '9227737'], |
25 |
| - } |
| 18 | +test_data[test] = { |
| 19 | + "input": """""", |
| 20 | + "expected": ["Unknown", "Unknown"], |
| 21 | +} |
| 22 | + |
| 23 | +test = "real" |
| 24 | +input_file = os.path.join( |
| 25 | + os.path.dirname(__file__), |
| 26 | + "Inputs", |
| 27 | + os.path.basename(__file__).replace(".py", ".txt"), |
| 28 | +) |
| 29 | +test_data[test] = { |
| 30 | + "input": open(input_file, "r+").read().strip(), |
| 31 | + "expected": ["318083", "9227737"], |
| 32 | +} |
26 | 33 |
|
27 | 34 | # -------------------------------- Control program execution -------------------------------- #
|
28 | 35 |
|
29 |
| -case_to_test = 'real' |
30 |
| -part_to_test = 2 |
| 36 | +case_to_test = "real" |
| 37 | +part_to_test = 2 |
31 | 38 | verbose_level = 1
|
32 | 39 |
|
33 | 40 | # -------------------------------- Initialize some variables -------------------------------- #
|
34 | 41 |
|
35 |
| -puzzle_input = test_data[case_to_test]['input'] |
36 |
| -puzzle_expected_result = test_data[case_to_test]['expected'][part_to_test-1] |
37 |
| -puzzle_actual_result = 'Unknown' |
| 42 | +puzzle_input = test_data[case_to_test]["input"] |
| 43 | +puzzle_expected_result = test_data[case_to_test]["expected"][part_to_test - 1] |
| 44 | +puzzle_actual_result = "Unknown" |
38 | 45 |
|
39 | 46 |
|
40 | 47 | # -------------------------------- Actual code execution -------------------------------- #
|
41 |
| -registers = {'a':0, 'b':0, 'c':0, 'd':0} |
| 48 | +registers = {"a": 0, "b": 0, "c": 0, "d": 0} |
42 | 49 | if part_to_test == 2:
|
43 |
| - registers['c'] = 1 |
| 50 | + registers["c"] = 1 |
44 | 51 |
|
45 | 52 |
|
46 |
| -instructions = puzzle_input.split('\n') |
| 53 | +instructions = [line.split(" ") for line in puzzle_input.split("\n")] |
47 | 54 | i = 0
|
48 | 55 | while True:
|
49 |
| - instruction = instructions[i] |
| 56 | + ins = instructions[i] |
50 | 57 | i += 1
|
51 | 58 |
|
52 |
| - if instruction[0:3] == 'cpy': |
53 |
| - _, val, target = instruction.split(' ') |
| 59 | + if ins[0] == "cpy": |
54 | 60 | try:
|
55 |
| - registers[target] = int(val) |
| 61 | + registers[ins[2]] = int(ins[1]) |
56 | 62 | except ValueError:
|
57 |
| - registers[target] = registers[val] |
58 |
| - |
59 |
| - elif instruction[0:3] == 'inc': |
60 |
| - _, target = instruction.split(' ') |
61 |
| - registers[target] += 1 |
62 |
| - elif instruction[0:3] == 'dec': |
63 |
| - _, target = instruction.split(' ') |
64 |
| - registers[target] -= 1 |
65 |
| - |
66 |
| - elif instruction[0:3] == 'jnz': |
67 |
| - _, target, jump = instruction.split(' ') |
68 |
| - if target == '0': |
| 63 | + registers[ins[2]] = registers[ins[1]] |
| 64 | + |
| 65 | + elif ins[0] == "inc": |
| 66 | + registers[ins[1]] += 1 |
| 67 | + elif ins[0] == "dec": |
| 68 | + registers[ins[1]] -= 1 |
| 69 | + |
| 70 | + elif ins[0] == "jnz": |
| 71 | + if ins[1] == "0": |
69 | 72 | pass
|
70 | 73 | else:
|
71 | 74 | try:
|
72 |
| - if int(target): |
73 |
| - i = i + int(jump) - 1 # -1 to compensate for what we added before |
| 75 | + if int(ins[1]): |
| 76 | + i += int(ins[2]) - 1 # -1 to compensate for what we added before |
74 | 77 | except ValueError:
|
75 |
| - if registers[target] != 0: |
76 |
| - i = i + int(jump) - 1 # -1 to compensate for what we added before |
| 78 | + if registers[ins[1]] != 0: |
| 79 | + i += int(ins[2]) - 1 # -1 to compensate for what we added before |
77 | 80 |
|
78 | 81 | if i >= len(instructions):
|
79 | 82 | break
|
80 | 83 |
|
81 |
| -puzzle_actual_result = registers['a'] |
82 |
| - |
83 |
| - |
84 |
| - |
| 84 | +puzzle_actual_result = registers["a"] |
85 | 85 |
|
86 | 86 |
|
87 | 87 | # -------------------------------- Outputs / results -------------------------------- #
|
88 | 88 |
|
89 | 89 | if verbose_level >= 3:
|
90 |
| - print ('Input : ' + puzzle_input) |
91 |
| -print ('Expected result : ' + str(puzzle_expected_result)) |
92 |
| -print ('Actual result : ' + str(puzzle_actual_result)) |
93 |
| - |
94 |
| - |
95 |
| - |
96 |
| - |
| 90 | + print("Input : " + puzzle_input) |
| 91 | +print("Expected result : " + str(puzzle_expected_result)) |
| 92 | +print("Actual result : " + str(puzzle_actual_result)) |
0 commit comments