-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday_06.py
75 lines (55 loc) · 2.21 KB
/
day_06.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
def parse_map(map_str:str) -> tuple[list, tuple, int]:
""" Map coordinate system:
[0,0] - bottom left
x-axis increases to the right
y-axis increases to the top
"""
obstacles = []
vehicle_position = None
# Split the input string into lines and reverse to match the coordinate system
lines = map_str.strip().split('\n')[::-1]
for y, line in enumerate(lines):
for x, char in enumerate(line):
if char == '#':
obstacles.append((x, y))
elif char == '^':
vehicle_position = (x, y)
return obstacles, vehicle_position, len(lines)
def vehicle_movement(obstacles, vehicle_position, map_size):
def turn_right(v):
""" Turn the vehicle's direction 90 degrees to the right """
return v[1], -v[0]
obstacles = set(obstacles)
visited_positions = set()
visited_states = set()
current_position = vehicle_position
direction = (0, 1) # Initial direction is north (0,1)
while True:
visited_positions.add(current_position)
visited_states.add((current_position, direction))
next_position = (current_position[0] + direction[0], current_position[1] + direction[1])
if (next_position[0] < 0 or next_position[1] < 0 or
next_position[0] >= map_size or
next_position[1] >= map_size):
break # Out of bounds
elif (next_position, direction) in visited_states:
return 0 # a loop was detected
if next_position in obstacles:
direction = turn_right(direction)
else:
current_position = next_position
return len(visited_positions)
def all_obstacles(obstacles, vehicle_position, map_size):
counter = 0
for i in range(map_size):
for j in range(map_size):
if (i, j) not in obstacles + [vehicle_position]:
if vehicle_movement(obstacles + [(i, j)], vehicle_position, map_size) == 0:
counter += 1
return counter
if __name__ == '__main__':
with open('./data/data_06.txt') as f:
data = f.read()
data = parse_map(data)
print("Part 1:", vehicle_movement(*data))
print("Part 2:", all_obstacles(*data))