-
Notifications
You must be signed in to change notification settings - Fork 0
/
10_part1.py
88 lines (71 loc) · 2.41 KB
/
10_part1.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
87
88
import math
def get_starting_point(sketch):
for rr in range(len(sketch)):
for cc in range(len(sketch[rr])):
if sketch[rr][cc] == "S":
starting_position = sketch[rr][cc]
return rr, cc
def get_next_position(sketch, rr, cc, current_direction):
current_pipe = sketch[rr][cc]
if current_pipe == "|" and current_direction == "up":
rr -= 1
elif current_pipe == "|" and current_direction == "down":
rr += 1
elif current_pipe == "-" and current_direction == "right":
cc += 1
elif current_pipe == "-" and current_direction == "left":
cc -= 1
elif current_pipe == "7" and current_direction == "right":
rr += 1
current_direction = "down"
elif current_pipe == "7" and current_direction == "up":
cc -= 1
current_direction = "left"
elif current_pipe == "L" and current_direction == "down":
cc += 1
current_direction = "right"
elif current_pipe == "L" and current_direction == "left":
rr -= 1
current_direction = "up"
elif current_pipe == "J" and current_direction == "down":
cc -= 1
current_direction = "left"
elif current_pipe == "J" and current_direction == "right":
rr -= 1
current_direction = "up"
elif current_pipe == "F" and current_direction == "left":
rr += 1
current_direction = "down"
elif current_pipe == "F" and current_direction == "up":
cc += 1
current_direction = "right"
return rr, cc, current_direction
line = input()
matrix = []
while line != "stop":
current_line = []
for char in line:
current_line.append(char)
matrix.append(current_line)
line = input()
row, col = get_starting_point(matrix)
direction = ""
if matrix[row][col + 1] != "." and matrix[row][col + 1] not in "|FL":
col += 1
direction = "right"
elif matrix[row][col - 1] != "." and matrix[row][col - 1] not in "|J7":
col -= 1
direction = "left"
elif matrix[row - 1][col] != "." and matrix[row - 1][col] not in "-LJ":
row -= 1
direction = "up"
elif matrix[row + 1][col] != "." and matrix[row + 1][col] not in "-7F":
row += 1
direction = "down"
all_steps = 0
while True:
row, col, direction = get_next_position(matrix, row, col, direction)
all_steps += 1
if matrix[row][col] == "S":
break
print(math.ceil(all_steps / 2))