-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTask20.jl
101 lines (89 loc) · 2.1 KB
/
Task20.jl
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
89
90
91
92
93
94
95
96
97
98
99
100
101
using HorizonSideRobots
function moves!(r::Robot, side::HorizonSide)
while isborder(r, side) == false
move!(r, side)
end
end
function moves!(r::Robot, side::HorizonSide, num_steps::Int)
for _ in 1:num_steps
move!(r, side)
end
end
putmarkers!(r::Robot, side::HorizonSide) =
while isborder(r, side) == false
move!(r, side)
putmarker!(r)
end
function move_by_markers(r::Robot, side::HorizonSide)
while ismarker(r) == true
move!(r, side)
end
end
inverse(side::HorizonSide) = HorizonSide(mod(Int(side) + 2, 4))
nextCounterClockwise(side::HorizonSide) = HorizonSide(mod(Int(side) + 1, 4))
function moveWhileCan(r::Robot, sides)
moved = true
while moved
moved = false
for side in sides
if !isborder(r, side)
move!(r, side)
moved = true
end
end
end
end
function bypass(r::Robot, dir)
bypassDir = nextCounterClockwise(dir)
antiBypassDir = inverse(bypassDir)
numSteps = 0
while !isborder(r, bypassDir) && isborder(r, dir)
move!(r, bypassDir)
numSteps += 1
end
if isborder(r, dir) && isborder(r,bypassDir)
moves!(r, antiBypassDir, numSteps)
return false
end
move!(r, dir)
moves!(r, antiBypassDir, numSteps)
return true
end
function countSegments(r::Robot)
ans = 0
moveWhileCan(r, (West, Sud))
dir = Ost
ans += processRow(r, dir, Nord)
while !isborder(r, Nord)
move!(r, Nord)
dir = inverse(dir)
ans += processRow(r, dir, Nord)
end
moveWhileCan(r, (West, Nord))
dir = Sud
ans += processRow(r, dir, Ost)
while !isborder(r, Ost)
move!(r, Ost)
dir = inverse(dir)
ans += processRow(r, dir, Ost)
end
return ans - 2
end
function processRow(r::Robot, side, toCheckSide)
cnt = 0
flag = false
while !isborder(r, side) || bypass(r, side)
if isborder(r, toCheckSide)
if !flag
flag = true
cnt += 1
end
else
flag = false
end
if !isborder(r, side)
move!(r, side)
end
end
return cnt
end