-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path11.rb
50 lines (36 loc) · 863 Bytes
/
11.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
# frozen_string_literal: true
input = File.readlines('11-1').map(&:strip)
# input[row][col]
def transform(cells)
height = cells.size
width = cells.first.size
new_cells = cells.map(&:dup)
(0...height).each do |row|
(0...width).each do |col|
count = 0
[[-1, 1], [0, 1], [1, 1],
[-1,0], [1, 0],
[-1,-1],[0,-1],[1,-1]].each do |(drow, dcol)|
if cells[row+drow]&.send(:[], col+dcol) == '#'
count += 1
end
end
if cells[row][col] == 'L' && count == 0
new_cells[row][col] = '#'
end
if cells[row][col] == '#' && count >= 4
puts [row,col].inspect
new_cells[row][col] = 'L'
end
end
end
new_cells
end
def show(cells)
puts cells.join("\n")
puts
end
cells = input
show(cells)
show(transform(cells))
show(transform(transform(cells)))