Skip to content

Commit b46cf34

Browse files
committed
Prepare for 2025
1 parent f16264e commit b46cf34

2 files changed

Lines changed: 125 additions & 0 deletions

File tree

2024/python/day-06/main.py

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
# --- Day 6: Guard Gallivant ---
2+
3+
import sys
4+
5+
sys.setrecursionlimit(10 ** 6)
6+
directions_deltas = {
7+
'up': (-1, 0),
8+
'right': (0, 1),
9+
'down': (1, 0),
10+
'left': (0, -1),
11+
}
12+
directions = ['up', 'right', 'down', 'left']
13+
14+
15+
def is_obstacle_ahead(position, direction, rows:int, cols:int, G):
16+
delta = directions_deltas[direction]
17+
new_position = (position[0] + delta[0], position[1] + delta[1])
18+
row, col = new_position
19+
if 0 <= row < rows and 0 <= col < cols:
20+
return G[row][col] == '#'
21+
return True
22+
23+
24+
def turn_right(direction: str) -> str:
25+
"""
26+
Return the new direction when the guard turns right.
27+
:param direction: input direction as string
28+
:return: new direction as string
29+
"""
30+
new_index = (directions.index(direction) + 1) % len(directions)
31+
return directions[new_index]
32+
33+
34+
def move_forward(position, direction):
35+
"""
36+
Move the guard forward.
37+
:param position: Starting position
38+
:param direction: Which direction to move
39+
:return: new positions
40+
"""
41+
delta = directions_deltas[direction]
42+
new_row = position[0] + delta[0]
43+
new_col = position[1] + delta[1]
44+
return (new_row, new_col)
45+
46+
47+
def part1():
48+
with open('../../rust/data/inputs/06.txt', mode='r') as f:
49+
data = f.read().strip()
50+
51+
G = data.split('\n')
52+
rows = len(G)
53+
cols = len(G[0])
54+
for row in range(rows):
55+
for col in range(cols):
56+
if G[row][col] == '^':
57+
start_row, start_col = row, col
58+
59+
guard_position = (start_row, start_col)
60+
guard_direction = 'up'
61+
visited = set()
62+
# Track visited states (position + direction) to detect cycles
63+
visited_states = set()
64+
65+
while True:
66+
# Check if we've been in this exact state before
67+
state = (guard_position, guard_direction)
68+
if state in visited_states:
69+
break
70+
71+
visited_states.add(state)
72+
73+
if is_obstacle_ahead(guard_position, guard_direction, rows, cols, G):
74+
guard_direction = turn_right(guard_direction)
75+
else:
76+
guard_position = move_forward(guard_position, guard_direction)
77+
visited.add(guard_position)
78+
79+
row, col = guard_position
80+
if row < 0 or row >= rows or col < 0 or col >= cols:
81+
break
82+
83+
return len(visited)
84+
# for o_r in range(rows):
85+
# for o_c in range(cols):
86+
# r, c = start_row, start_col
87+
# d = 0 # 0=up, 1=right, 2=down, 3=left
88+
# SEEN = set()
89+
# SEEN_RC = set()
90+
# while True:
91+
# # if (r, c, d) in SEEN:
92+
# # p2 += 1
93+
# # break
94+
# SEEN.add((r, c, d))
95+
# SEEN_RC.add((r, c))
96+
# dr, dc = [(-1, 0), (0, 1), (1, 0), (0, -1)][d]
97+
# rr = r + dr
98+
# cc = c + dc
99+
# if not (0 <= rr < rows and 0 <= cc < cols):
100+
# if G[o_r][o_c] == '#':
101+
# result = len(SEEN_RC)
102+
# break
103+
# if G[rr][cc] == '#' or rr == o_r and cc == o_c:
104+
# d = (d + 1) % 4
105+
# else:
106+
# r = rr
107+
# c = cc
108+
# return result
109+
110+
111+
def part2():
112+
pass
113+
# with open(infile, mode='r') as f:
114+
# lines = f.read().split("\n\n")
115+
# result = 0
116+
# for line in lines:
117+
# # Process each line here
118+
# pass
119+
# return result
120+
121+
122+
if __name__ == '__main__':
123+
print(part1())
124+
print(part2())

2025/.gitkeep

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

0 commit comments

Comments
 (0)