-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathday24.py
102 lines (79 loc) · 2.76 KB
/
day24.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
from collections import defaultdict
from itertools import permutations
from sys import maxsize
def read_input() -> list:
rows = [row.strip() for row in open("input/day24.txt").readlines()]
return rows
def get_number_positions(board: list) -> dict:
numbers = defaultdict()
rows = len(board)
cols = len(board[0])
for r in range(rows):
for c in range(cols):
if board[r][c] != "#" and board[r][c] != ".":
numbers[board[r][c]] = (c, r)
return numbers
def get_moves(board: list, square: list) -> list:
x, y = square
moves = []
if board[y + 1][x] != "#":
moves.append((x, y + 1))
if board[y - 1][x] != "#":
moves.append((x, y - 1))
if board[y][x + 1] != "#":
moves.append((x + 1, y))
if board[y][x - 1] != "#":
moves.append((x - 1, y))
return moves
def shortest_path(board: list, start: list, goal: list) -> list:
visited = []
to_visit = [[start]]
if start == goal:
return []
while to_visit:
path = to_visit.pop(0)
square = path[-1]
if square not in visited:
moves = get_moves(board, square)
for move in moves:
new_path = list(path)
new_path.append(move)
to_visit.append(new_path)
if move == goal:
return new_path
visited.append(square)
return []
def get_steps(board: list, numbers: dict) -> dict:
combo_steps = defaultdict()
for num_i in numbers:
for num_j in numbers:
s_path = shortest_path(board, numbers[num_i], numbers[num_j])
steps = max(0, len(s_path) - 1)
combo_steps[(num_i, num_j)] = steps
combo_steps[(num_j, num_i)] = steps
return combo_steps
def get_least_steps(numbers: dict, combo_steps: dict, end_at_0=False) -> (int, list):
num_list = sorted([number for number in numbers])[1:]
min_steps = maxsize
min_path = []
nums = len(num_list)
for perm in permutations(num_list):
steps = combo_steps[("0", perm[0])]
for i in range(nums - 1):
steps += combo_steps[(perm[i], perm[i + 1])]
if end_at_0:
steps += combo_steps[(perm[-1], "0")]
if steps < min_steps:
min_steps = steps
min_path = perm
return min_steps, min_path
def puzzles():
board = read_input()
numbers = get_number_positions(board)
combo_steps = get_steps(board, numbers)
least_steps, min_path = get_least_steps(numbers, combo_steps)
print("least steps:", least_steps, min_path)
least_steps, min_path = get_least_steps(numbers, combo_steps, end_at_0=True)
print("least steps:", least_steps, min_path)
if __name__ == "__main__":
puzzles()