Solve day 3 with python
This commit is contained in:
parent
4e9b501da1
commit
c583bfb813
|
@ -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)
|
Loading…
Reference in New Issue