-
Notifications
You must be signed in to change notification settings - Fork 0
/
10.py
87 lines (66 loc) · 1.93 KB
/
10.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
76
77
78
79
80
81
82
83
84
85
86
file = open('10-input.txt')
lines = file.readlines()
class Coordinate:
def __init__(self, x, y, pipe):
self.x = x
self.y = y
self.pipe = pipe
grid = []
testGrid = [[".",".",".",".","."],[".","S","-","7","."],[".","|",".","|","."],[".","L","-","J","."],[".",".",".",".","."]]
startPos = None
y = 0
for line in lines:
line = line.replace("\n", "")
array = []
x = 0
for char in line:
array.append(char)
if char == "S":
startPos = (x, y)
x += 1
grid.append(array)
y += 1
print(startPos)
def toupleToGrid(grid, touple):
return grid[touple[1]][touple[0]]
print(toupleToGrid(grid, startPos))
directions = {
"|" : ["S", "N"],
"-" : ["E", "W"],
"L" : ["N", "E"],
"J" : ["N", "W"],
"7" : ["S", "W"],
"F" : ["S", "E"],
"." : [""]
}
def navigate(input):
if input not in ["N", "S", "E", "W"]: return ValueError
if input == "S": return (0,1)
if input == "E": return (1,0)
if input == "N": return (0,-1)
if input == "W": return (-1,0)
def getNextPos(currentPos, direction):
steps = navigate(direction)
return (currentPos[0] + steps[0], currentPos[1] + steps[1])
def search(grid, startPos, next):
path = [toupleToGrid(grid, startPos)]
roundTrip = False
currentPos = startPos
nextDirection = next
while not roundTrip:
nextPos = getNextPos(currentPos, nextDirection)
nextTile = toupleToGrid(grid, nextPos)
if nextTile == "S":
path.append(nextTile)
roundTrip = True
break
possibleDirections = directions[nextTile]
if getNextPos(nextPos, possibleDirections[0]) == currentPos:
nextDirection = possibleDirections[1]
else:
nextDirection = possibleDirections[0]
path.append(nextTile)
currentPos = nextPos
print(path)
print(len(path)//2)
search(grid, startPos, "N")