-
Notifications
You must be signed in to change notification settings - Fork 1
/
13-2.rb
executable file
·101 lines (86 loc) · 2.05 KB
/
13-2.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/usr/bin/env ruby
require 'numo/narray'
class Pattern
attr_accessor :map
def initialize(input)
@height = input.size
@width = input[0].size
@map = Numo::UInt8.zeros(@height, @width)
@height.times do |y|
@width.times do |x|
@map[y, x] = 1 if input[y][x] == '#'
end
end
end
def horizontal_reflection_defects(candidate_row)
return -1 if candidate_row <= 0 or candidate_row >= @height
defects = 0
1.upto([candidate_row, @height - candidate_row].min) do |check_offset|
@width.times do |x|
if @map[candidate_row + check_offset - 1, x] !=
@map[candidate_row - check_offset, x]
defects += 1
end
end
end
defects
end
def vertical_reflection_defects(candidate_col)
return -1 if candidate_col <= 0 or candidate_col >= @width
defects = 0
1.upto([candidate_col, @width - candidate_col].min) do |check_offset|
@height.times do |y|
if @map[y, candidate_col + check_offset - 1] !=
@map[y, candidate_col - check_offset]
defects += 1
end
end
end
defects
end
def score_horizontal_reflection
1.upto(@height - 1) do |candidate_row|
if horizontal_reflection_defects(candidate_row) == 1
return 100 * candidate_row
end
end
0
end
def score_vertical_reflection
1.upto(@width - 1) do |candidate_col|
if vertical_reflection_defects(candidate_col) == 1
return candidate_col
end
end
0
end
def score
score_horizontal_reflection + score_vertical_reflection
end
def inspect
to_s
end
def to_s
s = "<#{self.class}:\n"
@height.times do |y|
@width.times do |x|
s += @map[y, x].to_s
end
s += "\n"
end
s += ">"
end
end
input = File.read('13.input').lines.map(&:strip).map(&:chars)
patterns = []
lines = []
input.each do |line|
if line.empty?
patterns << Pattern.new(lines)
lines = []
else
lines << line
end
end
patterns << Pattern.new(lines)
print patterns.sum(&:score), "\n"