|
4 | 4 | test_data = {} |
5 | 5 |
|
6 | 6 | test = 1 |
7 | | -test_data[test] = {"input": 18, |
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": 7165, |
14 | | - "expected": ['(235, 20) with 31', '(237, 223, 14) with 83'], |
15 | | - } |
| 7 | +test_data[test] = { |
| 8 | + "input": 18, |
| 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": 7165, |
| 20 | + "expected": ["(235, 20) with 31", "(237, 223, 14) with 83"], |
| 21 | +} |
16 | 22 |
|
17 | 23 | # -------------------------------- Control program execution -------------------------------- # |
18 | 24 |
|
19 | | -case_to_test = 'real' |
| 25 | +case_to_test = "real" |
20 | 26 | part_to_test = 2 |
21 | 27 |
|
22 | 28 | # -------------------------------- Initialize some variables -------------------------------- # |
23 | 29 |
|
24 | | -puzzle_input = test_data[case_to_test]['input'] |
25 | | -puzzle_expected_result = test_data[case_to_test]['expected'][part_to_test-1] |
26 | | -puzzle_actual_result = 'Unknown' |
| 30 | +puzzle_input = test_data[case_to_test]["input"] |
| 31 | +puzzle_expected_result = test_data[case_to_test]["expected"][part_to_test - 1] |
| 32 | +puzzle_actual_result = "Unknown" |
27 | 33 |
|
28 | 34 |
|
29 | 35 | # -------------------------------- Actual code execution -------------------------------- # |
30 | 36 |
|
31 | 37 |
|
32 | 38 | if part_to_test == 1: |
33 | | - grid_power = {(x, y): int(((((10+x)*y + puzzle_input) * (10+x)) // 100) % 10)-5 for x in range (1, 301) for y in range (1, 301)} |
34 | | - |
35 | | - sum_power = {(x, y): sum(grid_power[x1, y1] for x1 in range (x, x+3) for y1 in range (y, y+3)) for x in range (1, 299) for y in range (1, 299)} |
| 39 | + grid_power = { |
| 40 | + (x, y): int(((((10 + x) * y + puzzle_input) * (10 + x)) // 100) % 10) - 5 |
| 41 | + for x in range(1, 301) |
| 42 | + for y in range(1, 301) |
| 43 | + } |
| 44 | + |
| 45 | + sum_power = { |
| 46 | + (x, y): sum( |
| 47 | + grid_power[x1, y1] for x1 in range(x, x + 3) for y1 in range(y, y + 3) |
| 48 | + ) |
| 49 | + for x in range(1, 299) |
| 50 | + for y in range(1, 299) |
| 51 | + } |
36 | 52 |
|
37 | 53 | max_power = max(sum_power.values()) |
38 | 54 |
|
39 | | - puzzle_actual_result = list(coord for coord in sum_power if sum_power[coord] == max_power) |
| 55 | + puzzle_actual_result = list( |
| 56 | + coord for coord in sum_power if sum_power[coord] == max_power |
| 57 | + ) |
40 | 58 |
|
41 | 59 |
|
42 | 60 | else: |
43 | | - grid_power = {(x, y): int(((((10+x)*y + puzzle_input) * (10+x)) // 100) % 10)-5 for x in range (1, 301) for y in range (1, 301)} |
| 61 | + grid_power = { |
| 62 | + (x, y): int(((((10 + x) * y + puzzle_input) * (10 + x)) // 100) % 10) - 5 |
| 63 | + for x in range(1, 301) |
| 64 | + for y in range(1, 301) |
| 65 | + } |
44 | 66 |
|
45 | 67 | max_power = 31 |
46 | 68 | sum_power = grid_power.copy() |
47 | | - for size in range (2, 300): |
48 | | - sum_power = {(x, y, size): sum(grid_power[x1, y1] |
49 | | - for x1 in range (x, x+size) |
50 | | - for y1 in range (y, y+size)) |
51 | | - for x in range (1, 301-size+1) |
52 | | - for y in range (1, 301-size+1)} |
| 69 | + decreasing = False |
| 70 | + last_power = 0 |
| 71 | + for size in range(2, 300): |
| 72 | + sum_power = { |
| 73 | + (x, y, size): sum( |
| 74 | + grid_power[x1, y1] |
| 75 | + for x1 in range(x, x + size) |
| 76 | + for y1 in range(y, y + size) |
| 77 | + ) |
| 78 | + for x in range(1, 301 - size + 1) |
| 79 | + for y in range(1, 301 - size + 1) |
| 80 | + } |
53 | 81 |
|
54 | 82 | new_max = max(sum_power.values()) |
55 | 83 | if new_max > max_power: |
| 84 | + decreasing = False |
56 | 85 | max_power = new_max |
57 | | - puzzle_actual_result = list(coord + (size,) for coord in sum_power if sum_power[coord] == max_power) |
| 86 | + puzzle_actual_result = list( |
| 87 | + coord for coord in sum_power if sum_power[coord] == max_power |
| 88 | + ) |
58 | 89 |
|
59 | 90 | # Basically, let it run until it decreases multiple times |
60 | | - print (size, new_max, list(coord for coord in sum_power if sum_power[coord] == new_max)) |
| 91 | + # print (size, new_max, list(coord for coord in sum_power if sum_power[coord] == new_max)) |
61 | 92 |
|
| 93 | + if not decreasing and new_max < last_power: |
| 94 | + decreasing = True |
| 95 | + elif decreasing and new_max < last_power: |
| 96 | + break |
| 97 | + last_power = new_max |
62 | 98 |
|
63 | 99 |
|
64 | 100 | # -------------------------------- Outputs / results -------------------------------- # |
65 | 101 |
|
66 | | -print ('Expected result : ' + str(puzzle_expected_result)) |
67 | | -print ('Actual result : ' + str(puzzle_actual_result)) |
68 | | - |
69 | | - |
70 | | - |
71 | | - |
| 102 | +print("Expected result : " + str(puzzle_expected_result)) |
| 103 | +print("Actual result : " + str(puzzle_actual_result)) |
0 commit comments