forked from ociule/codeeval
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrid_walk.rb
65 lines (56 loc) · 1.14 KB
/
grid_walk.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
require 'set'
start = [0, 0]
$visited = Set.new [start]
LIMIT = 19
$queue = [start]
def digits_sum n
sum = 0
while n > 0 do
d = n / 10
r = n % 10
sum += r
n -= r
if d > 0
n /= 10
end
end
return sum
sum = 0
while n > 0
highest_pow_10 = 10 ** (Math.log10 n).to_i
highest_digit = n / highest_pow_10
sum += highest_digit
n = n % highest_pow_10
end
sum += n
return sum
end
def accessible? p
return digits_sum(p[0].abs) + digits_sum(p[1].abs) <= LIMIT
end
def test_accessible?
puts "Testing"
raise "ERROR" if !accessible? [-5, -7]
raise "ERROR" if !accessible? [5, 7]
raise "ERROR" if accessible? [59, 79]
raise "ERROR" if accessible? [-59, -79]
end
def explore p
x, y = p[0], p[1]
if not $visited.include? p and accessible? p
$queue << p
$visited.add p
end
end
if ARGV[0] == "--test"
test_accessible?
else
while $queue.length > 0
x, y = $queue.shift
explore [x + 1, y]
explore [x - 1, y]
explore [x, y + 1]
explore [x, y - 1]
end
puts $visited.size
end