-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsungrid.rb
75 lines (57 loc) · 1.71 KB
/
sungrid.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
class SunGrid
attr_reader :rows, :grid_size, :number_of_results
def initialize(input)
data = input.split(' ').map { |x| x.to_i }
@number_of_results = data[0]
@grid_size = data[1]
@rows = data.drop(2).each_slice(@grid_size).to_a
end
def value_at(x, y)
valid_range?(x, y) ? @rows[y][x] : 0
end
def score(x, y)
neighbours = [ value_at(x-1, y-1), value_at(x, y-1), value_at(x+1, y-1),
value_at(x-1, y), value_at(x, y), value_at(x+1, y),
value_at(x-1, y+1), value_at(x, y+1), value_at(x+1, y+1)
]
neighbours.reduce(0, :+)
end
def scores
@scores = []
@rows.flatten.each_with_index do |cell, index|
@scores << {"#{x(index)},#{y(index)}" => score(x(index), y(index))}
end
@scores
end
def output
sorted_scores = @scores.sort_by {|score| score.values}.reverse!
results = sorted_scores.take(@number_of_results).inject([]) do |memo, score|
memo << "(#{score.keys} score:#{score.values})".gsub(Regexp.union(/\[/, /\]/, /\"/), '')
memo
end
results.join(' ')
end
def scores_to_colours
rows = @rows.flatten
points = scores.map(&:values).flatten
data = rows.zip(points)
heat_map = data.map do |value|
{ :value => value.first,
:score => value.last,
:color => (value.last * 5)
}
end
heat_map.each_slice(3).to_a
end
private
def valid_range?(x, y)
max_size = @grid_size-1
x.between?(0, max_size) && y.between?(0, max_size)
end
def y(index)
(index - x(index)) / @grid_size
end
def x(index)
index % @grid_size
end
end