Solve day 3 with python

This commit is contained in:
Samuel Ortion 2024-11-27 10:50:13 +01:00
parent 4e9b501da1
commit c583bfb813
Signed by: sortion
GPG Key ID: 9B02406F8C4FB765
1 changed files with 34 additions and 0 deletions

34
2015/days/03/walk.py Normal file
View File

@ -0,0 +1,34 @@
#!/usr/bin/env python3
count = 0
x = 0
y = 0
visited = {}
MOTION = {
"^": [0, -1],
"<": [-1, 0],
"v": [0, 1],
">": [1, 0]
}
def move(char):
global x
global y
motion = MOTION[char]
x += motion[0]
y += motion[1]
visit(x, y)
def visit(x, y):
global count
if (x, y) not in visited:
visited[(x, y)] = 1
count += 1
visit(x, y)
with open("./data/input", "r") as f:
for line in f:
for char in line:
move(char)
print(count)