-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathT-3.rb
72 lines (61 loc) · 1.53 KB
/
T-3.rb
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
def solution(a)
current = [0, 0]
direction = :north
lines = []
a.each_with_index { |v, i|
step = current.clone
case direction
when :north
step[1] -= v
direction = :east
when :east
step[0] += v
direction = :south
when :south
step[1] += v
direction = :west
when :west
step[0] -= v
direction = :north
end
current_line = [current, step]
if lines.count > 1
(0...lines.count - 1).each { |j|
return i + 1 if intersect(lines[j], current_line)
}
end
#todo: limit to accessible lines
lines << [current, step]
current = step
}
0
end
def intersect(a, b)
condition = lambda { |a, b|
a[0][0] >= [b[0][0], b[1][0]].min && a[0][0] <= [b[0][0], b[1][0]].max \
&& b[0][1] >= [a[0][1], a[1][1]].min && b[0][1] <= [a[0][1], a[1][1]].max
}
condition[a, b] || condition[b, a]
end
require "minitest/autorun"
require "byebug"
require "timeout"
# byebug
# solution(nil)
# exit
class TestSolution < Minitest::Test
def test_default
assert_equal 7, solution([1, 3, 2, 5, 4, 4, 6, 3, 2] )
end
# def test_simple
# assert_equal nil, solution(nil)
# end
# def test_small
# assert_equal nil, solution(nil)
# end
# def test_large
# Timeout::timeout(6) {
# assert_equal 0, solution([0] * 100_000 )
# }
# end
end