-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path15.a.py
72 lines (67 loc) · 2.16 KB
/
15.a.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
with open("2024/15.input.txt", encoding="utf-8") as file:
data = file.read()
map_str, moves = data.split("\n\n")
map_arr = [list(line) for line in map_str.split("\n")]
moves = moves.replace("\n", "")
pos: tuple[int, int]
for y, row in enumerate(map_arr):
for x, cell in enumerate(row):
if cell == "@":
pos = (x, y)
row[x] = "."
break
for move in moves:
if move == "^":
if map_arr[pos[1] - 1][pos[0]] == "#":
continue
if map_arr[pos[1] - 1][pos[0]] == "O":
x, y = pos[0], pos[1] - 1
while map_arr[y][x] == "O":
y -= 1
if map_arr[y][x] == "#":
continue
map_arr[y][x] = "O"
map_arr[pos[1] - 1][pos[0]] = "."
pos = (pos[0], pos[1] - 1)
elif move == "v":
if map_arr[pos[1] + 1][pos[0]] == "#":
continue
if map_arr[pos[1] + 1][pos[0]] == "O":
x, y = pos[0], pos[1] + 1
while map_arr[y][x] == "O":
y += 1
if map_arr[y][x] == "#":
continue
map_arr[y][x] = "O"
map_arr[pos[1] + 1][pos[0]] = "."
pos = (pos[0], pos[1] + 1)
elif move == "<":
if map_arr[pos[1]][pos[0] - 1] == "#":
continue
if map_arr[pos[1]][pos[0] - 1] == "O":
x, y = pos[0] - 1, pos[1]
while map_arr[y][x] == "O":
x -= 1
if map_arr[y][x] == "#":
continue
map_arr[y][x] = "O"
map_arr[pos[1]][pos[0] - 1] = "."
pos = (pos[0] - 1, pos[1])
elif move == ">":
if map_arr[pos[1]][pos[0] + 1] == "#":
continue
if map_arr[pos[1]][pos[0] + 1] == "O":
x, y = pos[0] + 1, pos[1]
while map_arr[y][x] == "O":
x += 1
if map_arr[y][x] == "#":
continue
map_arr[y][x] = "O"
map_arr[pos[1]][pos[0] + 1] = "."
pos = (pos[0] + 1, pos[1])
total = 0
for y, row in enumerate(map_arr):
for x, cell in enumerate(row):
if cell == "O":
total += 100 * y + x
print(total)