-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnaked_trips.rb
61 lines (47 loc) · 1.38 KB
/
naked_trips.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
def naked_trips
solve_for_boxes(array, :naked_trip)
solve_for_rows(array, :naked_trip)
solve_for_columns(array, :naked_trip)
end
def naked_trip?(trip, temp_array)
values = []
temp_array.each do |element|
next if element.is_a?(Integer)
values << element if (element - trip) == []
end
return trip if values.length == trip.length
nil
end
def remaining_numbers(trip)
[1, 2, 3, 4, 5, 6, 7, 8, 9] - trip
end
def location
@location
end
def clear_naked_trip(array, rows, columns, trip, location)
remaining_numbers(trip).each do |number|
rows.each do |row|
columns.each do |column|
element = array[row][column]
next unless element.is_a?(Array) && element.include?(number)
unless element == (element - trip)
@history << "#{location} [#{row}][#{column}] :naked_trip --- " \
"clearing #{trip} before: #{element} after: #{element -= trip}"
end
Common.clear_all(array)
array[row][column] = element
end
end
end
end
def naked_trip(array, rows, columns, location)
temp_array = Common.build_temp_array(array, rows, columns)
rows.each do |row|
columns.each do |column|
element = array[row][column]
trip = naked_trip?(element, temp_array) if element.size == 3
next if trip.nil?
clear_naked_trip(array, rows, columns, trip, location)
end
end
end