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
NEIGHBORS = [
[-1,-1], [-1,0], [-1,1],
[ 0,-1], [ 0,1],
[ 1,-1], [ 1,0], [1 ,1],
]
Grid = Data.define(:rolls) do
def size = rolls.size
def accessible
rolls.select {|(y,x)|
NEIGHBORS.count {|(dy,dx)|
rolls.include?([y+dy, x+dx])
} < 4
}
end
def removed
return enum_for(__method__) unless block_given?
grid = self
loop do
yield grid
grid = Grid.new(grid.rolls - grid.accessible)
end
end
end
grid = Grid.new(
DATA.read
.lines(chomp: true)
.each.with_index.inject(Set.new) do |rolls, (line, y)|
rolls.merge(
line.chars.each.with_index
.select {|roll,_| roll == ?@ }
.map { [y, _2] }
)
end
)
chunk = grid.removed.slice_when { _1.size == _2.size }.next
pp chunk.first.size - chunk.last.size
__END__