-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rb
100 lines (87 loc) · 1.91 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
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
#!/usr/bin/ruby
part, file_path = $*
file = File.open(file_path, 'r')
#noinspection RubyNilAnalysis
enhancement = file.gets.rstrip.chars.map { |b| b == ?# }
img = []
while (line = file.gets)
break unless line
line = line.strip
next if line.empty?
img = img.push line.chars.map { |b| b == ?# }
end
class Img
@data
def initialize(data)
@data = data
end
def height
@data.size
end
def width
@data[0].size
end
def [](y, x)
if y < 0 or y >= @data.size or x < 0 or x >= @data.size
return false
end
@data[y][x]
end
def print
@data.each { |line| puts line.map {|b| b ? '#' : '.'}.join }
end
def count_light
cnt = 0
@data.each { |line| line.each { |b| cnt += 1 if b } }
cnt
end
def pad(padding)
x_padding = [false] * padding
Img.new Array.new(self.height + 2 * padding) { |y|
y -= padding
if y >= 0 and y < @data.size
x_padding + @data[y] + x_padding
else
Array.new(self.width + 2 * padding, false)
end
}
end
def enhance(lookup)
result = Array.new(self.height) { Array.new(self.width) }
(0...self.height).each { |y|
(0...self.width).each { |x|
val = 0
i = 0b1_0000_0000
(-1..1).each { |dy|
(-1..1).each { |dx|
if self[y + dy, x + dx]
val |= i
end
i >>= 1
}
}
result[y][x] = lookup[val]
}
}
Img.new result
end
def cut_out(margin)
Img.new Array.new(self.height - 2 * margin) { |i| @data[i+margin][margin..self.width-margin] }
end
end
img = Img.new img
img.print
puts
case part
when "part01"
puts img.pad(12).enhance(enhancement).enhance(enhancement).cut_out(10).count_light
when "part02"
img = img.pad(100)
(1..50).each { |i|
puts "step #{i}"
img = img.enhance(enhancement)
}
puts img.cut_out(50).count_light
else
puts "unknown subcommand"
end