|
4 | 4 | test_data = {}
|
5 | 5 |
|
6 | 6 | test = 1
|
7 |
| -test_data[test] = {"input": """""", |
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": open(input_file, "r+").read().strip(), |
14 |
| - "expected": ['585', '83173'], |
15 |
| - } |
| 7 | +test_data[test] = { |
| 8 | + "input": """""", |
| 9 | + "expected": ["Unknown", "Unknown"], |
| 10 | +} |
| 11 | + |
| 12 | +test = "real" |
| 13 | +input_file = os.path.join( |
| 14 | + os.path.dirname(__file__), |
| 15 | + "Inputs", |
| 16 | + os.path.basename(__file__).replace(".py", ".txt"), |
| 17 | +) |
| 18 | +test_data[test] = { |
| 19 | + "input": open(input_file, "r+").read().strip(), |
| 20 | + "expected": ["585", "83173"], |
| 21 | +} |
16 | 22 |
|
17 | 23 | # -------------------------------- Control program execution -------------------------------- #
|
18 | 24 |
|
19 |
| -case_to_test = 'real' |
20 |
| -part_to_test = 2 |
| 25 | +case_to_test = "real" |
| 26 | +part_to_test = 2 |
21 | 27 | verbose_level = 1
|
22 | 28 |
|
23 | 29 | # -------------------------------- Initialize some variables -------------------------------- #
|
24 | 30 |
|
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' |
| 31 | +puzzle_input = test_data[case_to_test]["input"] |
| 32 | +puzzle_expected_result = test_data[case_to_test]["expected"][part_to_test - 1] |
| 33 | +puzzle_actual_result = "Unknown" |
28 | 34 |
|
29 | 35 |
|
30 | 36 | # -------------------------------- Actual code execution -------------------------------- #
|
|
34 | 40 |
|
35 | 41 |
|
36 | 42 | else:
|
37 |
| - used_frequencies = [0] |
| 43 | + data = list(map(int, puzzle_input.splitlines())) |
| 44 | + used_frequencies = [sum(data[0 : i + 1]) for i in range(len(data))] |
| 45 | + delta = sum(map(int, puzzle_input.splitlines())) |
38 | 46 | frequency = 0
|
| 47 | + i = 0 |
39 | 48 | while True:
|
40 |
| - for string in puzzle_input.split('\n'): |
41 |
| - frequency += int(string) |
42 |
| - if frequency in used_frequencies: |
43 |
| - puzzle_actual_result = frequency |
44 |
| - break |
45 |
| - used_frequencies.append(frequency) |
46 |
| - |
47 |
| - if puzzle_actual_result != 'Unknown': |
| 49 | + i += 1 |
| 50 | + new_freq = [x + i * delta for x in used_frequencies] |
| 51 | + reuse = [freq for freq in new_freq if freq in used_frequencies] |
| 52 | + if reuse: |
| 53 | + puzzle_actual_result = reuse[0] |
48 | 54 | break
|
49 | 55 |
|
50 | 56 |
|
51 |
| - |
52 | 57 | # -------------------------------- Outputs / results -------------------------------- #
|
53 | 58 |
|
54 | 59 | if verbose_level >= 3:
|
55 |
| - print ('Input : ' + puzzle_input) |
56 |
| -print ('Expected result : ' + str(puzzle_expected_result)) |
57 |
| -print ('Actual result : ' + str(puzzle_actual_result)) |
58 |
| - |
59 |
| - |
60 |
| - |
61 |
| - |
| 60 | + print("Input : " + puzzle_input) |
| 61 | +print("Expected result : " + str(puzzle_expected_result)) |
| 62 | +print("Actual result : " + str(puzzle_actual_result)) |
0 commit comments