-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rb
69 lines (54 loc) · 1.78 KB
/
main.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
COLS = 3
ROWS = 3
MINIMUM_PATTERN_SIZE = 4
$count = 0
$counts_by_size = {}
def is_within_bounds(x, y)
(x >= 0 && x < COLS) && (y >=0 && y < ROWS)
end
def is_visited(visited, x, y)
visited.include?([x,y])
end
def move(visited, x, y)
return if !is_within_bounds(x, y) || is_visited(visited, x, y)
unless (visited.length + 1) < MINIMUM_PATTERN_SIZE
$count = $count + 1
$counts_by_size[visited.length+1] = $counts_by_size[visited.length+1] == nil ? 1 : $counts_by_size[visited.length+1] + 1
puts "#{visited + [[x, y]]}"
end
# Horizontal
move(visited + [[x,y]], x-1, y)
move(visited + [[x,y]], x+1, y)
move(visited + [[x,y]], x-2, y) if visited.include?([x-1,y])
move(visited + [[x,y]], x+2, y) if visited.include?([x+1,y])
# Vertical
move(visited + [[x,y]], x, y-1)
move(visited + [[x,y]], x, y+1)
move(visited + [[x,y]], x, y-2) if visited.include?([x,y-1])
move(visited + [[x,y]], x, y+2) if visited.include?([x,y+1])
# Diagonals
move(visited + [[x,y]], x+1, y+1)
move(visited + [[x,y]], x-1, y+1)
move(visited + [[x,y]], x+1, y-1)
move(visited + [[x,y]], x-1, y-1)
move(visited + [[x,y]], x+2, y+1)
move(visited + [[x,y]], x-2, y+1)
move(visited + [[x,y]], x+2, y-1)
move(visited + [[x,y]], x-2, y-1)
move(visited + [[x,y]], x+1, y+2)
move(visited + [[x,y]], x-1, y+2)
move(visited + [[x,y]], x+1, y-2)
move(visited + [[x,y]], x-1, y-2)
# Non Adyacent Diagonals
move(visited + [[x,y]], x+2, y+2) if visited.include?([x+1,y+1])
move(visited + [[x,y]], x-2, y+2) if visited.include?([x-1,y+1])
move(visited + [[x,y]], x+2, y-2) if visited.include?([x+1,y-1])
move(visited + [[x,y]], x-2, y-2) if visited.include?([x-1,y-1])
end
(0..2).each do |x|
(0..2).each do |y|
move([], x, y)
end
end
puts $count
puts $counts_by_size