|
4 | 4 | test_data = {} |
5 | 5 |
|
6 | 6 | test = 1 |
7 | | -test_data[test] = {"input": """########### |
| 7 | +test_data[test] = { |
| 8 | + "input": """########### |
8 | 9 | #0.1.....2# |
9 | 10 | #.#######.# |
10 | 11 | #4.......3# |
11 | 12 | ###########""", |
12 | | - "expected": ['Unknown', 'Unknown'], |
13 | | - } |
14 | | - |
15 | | -test += 1 |
16 | | -test_data[test] = {"input": """""", |
17 | | - "expected": ['Unknown', 'Unknown'], |
18 | | - } |
19 | | - |
20 | | -test = 'real' |
21 | | -input_file = os.path.join(os.path.dirname(__file__), 'Inputs', os.path.basename(__file__).replace('.py', '.txt')) |
22 | | -test_data[test] = {"input": open(input_file, "r+").read().strip(), |
23 | | - "expected": ['442', 'Unknown'], |
24 | | - } |
| 13 | + "expected": ["Unknown", "Unknown"], |
| 14 | +} |
| 15 | + |
| 16 | +test = "real" |
| 17 | +input_file = os.path.join( |
| 18 | + os.path.dirname(__file__), |
| 19 | + "Inputs", |
| 20 | + os.path.basename(__file__).replace(".py", ".txt"), |
| 21 | +) |
| 22 | +test_data[test] = { |
| 23 | + "input": open(input_file, "r+").read().strip(), |
| 24 | + "expected": ["442", "660"], |
| 25 | +} |
25 | 26 |
|
26 | 27 | # -------------------------------- Control program execution -------------------------------- # |
27 | 28 |
|
28 | | -case_to_test = 'real' |
29 | | -part_to_test = 2 |
| 29 | +case_to_test = "real" |
| 30 | +part_to_test = 2 |
30 | 31 | verbose_level = 1 |
31 | 32 |
|
32 | 33 | # -------------------------------- Initialize some variables -------------------------------- # |
33 | 34 |
|
34 | | -puzzle_input = test_data[case_to_test]['input'] |
35 | | -puzzle_expected_result = test_data[case_to_test]['expected'][part_to_test-1] |
36 | | -puzzle_actual_result = 'Unknown' |
| 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" |
37 | 38 |
|
38 | 39 |
|
39 | 40 | # -------------------------------- Actual code execution -------------------------------- # |
40 | 41 |
|
41 | 42 | grid = puzzle_input |
42 | | -graph = pathfinding.WeightedGraph () |
43 | | -graph.grid_to_vertices(re.sub('[0-9]', '.', puzzle_input)) |
| 43 | +graph = pathfinding.WeightedGraph() |
| 44 | +graph.grid_to_vertices(re.sub("[0-9]", ".", puzzle_input)) |
44 | 45 |
|
45 | 46 | waypoints = {} |
46 | | -for i in range (10): |
| 47 | +for i in range(10): |
47 | 48 | if str(i) in grid: |
48 | | - waypoints[i] = (grid.find(str(i)) % (len(grid.split('\n')[0])+1), grid.find(str(i)) // (len(grid.split('\n')[0])+1)) |
| 49 | + waypoints[i] = ( |
| 50 | + grid.find(str(i)) % (len(grid.split("\n")[0]) + 1), |
| 51 | + grid.find(str(i)) // (len(grid.split("\n")[0]) + 1), |
| 52 | + ) |
49 | 53 |
|
50 | | -edges = {waypoints[x]:{} for x in waypoints} |
| 54 | +edges = {waypoints[x]: {} for x in waypoints} |
51 | 55 | for a in waypoints: |
52 | 56 | for b in waypoints: |
53 | 57 | if waypoints[a] <= waypoints[b]: |
|
59 | 63 | edges[waypoints[b]][waypoints[a]] = graph.distance_from_start[waypoints[b]] |
60 | 64 | graph.reset_search() |
61 | 65 |
|
62 | | -min_length = 10**6 |
| 66 | +min_length = 10 ** 6 |
63 | 67 | for order in itertools.permutations([waypoints[x] for x in waypoints if x != 0]): |
64 | 68 | length = 0 |
65 | 69 | current_waypoint = waypoints[0] |
|
74 | 78 | puzzle_actual_result = min_length |
75 | 79 |
|
76 | 80 |
|
77 | | - |
78 | | - |
79 | | - |
80 | | - |
81 | | - |
82 | | - |
83 | 81 | # -------------------------------- Outputs / results -------------------------------- # |
84 | 82 |
|
85 | 83 | if verbose_level >= 3: |
86 | | - print ('Input : ' + puzzle_input) |
87 | | -print ('Expected result : ' + str(puzzle_expected_result)) |
88 | | -print ('Actual result : ' + str(puzzle_actual_result)) |
89 | | - |
90 | | - |
91 | | - |
92 | | - |
| 84 | + print("Input : " + puzzle_input) |
| 85 | +print("Expected result : " + str(puzzle_expected_result)) |
| 86 | +print("Actual result : " + str(puzzle_actual_result)) |
0 commit comments