-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path08-1.rb
executable file
·87 lines (77 loc) · 1.64 KB
/
08-1.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/usr/bin/env ruby
require 'numo/narray'
class Map
attr_reader :antennae, :map, :antinodes
def initialize(input)
@height = input.size
@width = input[0].size
@antennae = Hash.new []
@map = Numo::UInt8.zeros(@height, @width)
@antinodes = Numo::UInt8.zeros(@height, @width)
@height.times do |y|
@width.times do |x|
case input[y][x]
when '.'
@map[y, x] = 0
else
@map[y, x] = input[y][x].codepoints[0]
@antennae[input[y][x].codepoints[0]] += [[y, x]]
end
end
end
end
def find_antinodes(a, b)
possibles = [
[
a[0] + (b[0] - a[0]) * 2,
a[1] + (b[1] - a[1]) * 2
],
[
b[0] + (a[0] - b[0]) * 2,
b[1] + (a[1] - b[1]) * 2
]
]
possibles.reject { |p| p[0] < 0 || p[0] >= @height || p[1] < 0 || p[1] >= @width }
end
def fill!
antennae.each_value do |ants|
ants.combination(2) do |pair|
find_antinodes(pair[0], pair[1]).each do |a|
@antinodes[a[0], a[1]] += 1
end
end
end
end
def score
count = 0
@height.times do |y|
@width.times do |x|
count += 1 if @antinodes[y, x].positive?
end
end
count
end
def inspect
to_s
end
def to_s
s = "<#{self.class}:\n"
@height.times do |y|
@width.times do |x|
if @map[y, x] != 0
s << @map[y, x]
elsif @antinodes[y, x] != 0
s += '#'
else
s += '.'
end
end
s += "\n"
end
s += ">"
s
end
end
map = Map.new File.read('08.input').lines.map(&:strip)
map.fill!
puts map.score