generated from alvesvaren/AoC-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path22.py
More file actions
127 lines (111 loc) · 4.34 KB
/
22.py
File metadata and controls
127 lines (111 loc) · 4.34 KB
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import re
grid = []
done = False
sequence = ""
for line in open("22t.txt"):
line = line[:-1]
if line == "":
done = True
if done:
sequence = line
else:
grid.append(line)
width = max(map(len, grid))
grid = [line + " " * (width - len(line)) for line in grid]
position = [0, 0]
next_pos = [0, 0]
move = [1, 0]
while grid[position[1]][position[0]] != ".":
position[0] += 1
for x, y in re.findall(r"(\d+)([RL]?)", sequence):
x = int(x)
for _ in range(x):
next_pos[0] = position[0]
next_pos[1] = position[1]
while True:
next_pos[1] = (next_pos[1] + move[1]) % len(grid)
next_pos[0] = (next_pos[0] + move[0]) % len(grid[0])
if grid[next_pos[1]][next_pos[0]] != " ":
break
if grid[next_pos[1]][next_pos[0]] == "#":
break
position[0] = next_pos[0]
position[1] = next_pos[1]
if y == "R":
move[1], move[0] = move[0], -move[1]
elif y == "L":
move[1], move[0] = -move[0], move[1]
mod = 0
match move:
case [1, 0]: mod = 0
case [0, 1]: mod = 1
case [-1, 0]: mod = 2
case [0, -1]: mod = 3
print("Part 1: " + str(1000 * (position[1] + 1) + 4 * (position[0] + 1) + mod))
position = [0, 0]
next_pos = [0, 0]
move = [1, 0]
while grid[position[1]][position[0]] != ".":
position[0] += 1
for x, y in re.findall(r"(\d+)([RL]?)", sequence):
x = int(x)
for _ in range(x):
pre_move = move[:]
next_pos[0] = position[0] + move[0]
next_pos[1] = position[1] + move[1]
if next_pos[1] < 0 and 50 <= next_pos[0] < 100 and move[1] == -1:
move[1], move[0] = 0, 1
next_pos[1], next_pos[0] = next_pos[0] + 100, 0
elif next_pos[0] < 0 and 150 <= next_pos[1] < 200 and move[0] == -1:
move[1], move[0] = 1, 0
next_pos[1], next_pos[0] = 0, next_pos[1] - 100
elif next_pos[1] < 0 and 100 <= next_pos[0] < 150 and move[1] == -1:
next_pos[1], next_pos[0] = 199, next_pos[0] - 100
elif next_pos[1] >= 200 and 0 <= next_pos[0] < 50 and move[1] == 1:
next_pos[1], next_pos[0] = 0, next_pos[0] + 100
elif next_pos[0] >= 150 and 0 <= next_pos[1] < 50 and move[0] == 1:
move[0] = -1
next_pos[1], next_pos[0] = 149 - next_pos[1], 99
elif next_pos[0] == 100 and 100 <= next_pos[1] < 150 and move[0] == 1:
move[0] = -1
next_pos[1], next_pos[0] = 149 - next_pos[1], 149
elif next_pos[1] == 50 and 100 <= next_pos[0] < 150 and move[1] == 1:
move[1], move[0] = 0, -1
next_pos[1], next_pos[0] = next_pos[0] - 50, 99
elif next_pos[0] == 100 and 50 <= next_pos[1] < 100 and move[0] == 1:
move[1], move[0] = -1, 0
next_pos[1], next_pos[0] = 49, next_pos[1] + 50
elif next_pos[1] == 150 and 50 <= next_pos[0] < 100 and move[1] == 1:
move[1], move[0] = 0, -1
next_pos[1], next_pos[0] = next_pos[0] + 100, 49
elif next_pos[0] == 50 and 150 <= next_pos[1] < 200 and move[0] == 1:
move[1], move[0] = -1, 0
next_pos[1], next_pos[0] = 149, next_pos[1] - 100
elif next_pos[1] == 99 and 0 <= next_pos[0] < 50 and move[1] == -1:
move[1], move[0] = 0, 1
next_pos[1], next_pos[0] = next_pos[0] + 50, 50
elif next_pos[0] == 49 and 50 <= next_pos[1] < 100 and move[0] == -1:
move[1], move[0] = 1, 0
next_pos[1], next_pos[0] = 100, next_pos[1] - 50
elif next_pos[0] == 49 and 0 <= next_pos[1] < 50 and move[0] == -1:
move[0] = 1
next_pos[1], next_pos[0] = 149 - next_pos[1], 0
elif next_pos[0] < 0 and 100 <= next_pos[1] < 150 and move[0] == -1:
move[0] = 1
next_pos[1], next_pos[0] = 149 - next_pos[1], 50
if grid[next_pos[1]][next_pos[0]] == "#":
move = pre_move[:]
break
position[0] = next_pos[0]
position[1] = next_pos[1]
if y == "R":
move[1], move[0] = move[0], -move[1]
elif y == "L":
move[1], move[0] = -move[0], move[1]
mod = 0
match move:
case [1, 0]: mod = 0
case [0, 1]: mod = 1
case [-1, 0]: mod = 2
case [0, -1]: mod = 3
print("Part 2: " + str(1000 * (position[1] + 1) + 4 * (position[0] + 1) + mod))