-
Notifications
You must be signed in to change notification settings - Fork 0
/
11b_solution.rb
79 lines (65 loc) · 1.28 KB
/
11b_solution.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
require 'set'
input = File.read("./11input.txt").split("\n").map do |l|
l.split("").map(&:to_i)
end
def neighbours(board, ri, ci)
ns = []
max_ri = board.length-1
max_ci = board[ri].length-1
if ri != 0
ns << [ri-1,ci]
end
if ci != 0
ns << [ri,ci-1]
end
if ri != max_ri
ns << [ri+1,ci]
end
if ci != max_ci
ns << [ri,ci+1]
end
if ri != 0 && ci != 0
ns << [ri-1,ci-1]
end
if ri != 0 && ci != max_ci
ns << [ri-1,ci+1]
end
if ri != max_ri && ci != 0
ns << [ri+1,ci-1]
end
if ri != max_ri && ci != max_ci
ns << [ri+1,ci+1]
end
ns
end
def flash(board, ri, ci, flashed)
return if board[ri][ci] <= 9
return if flashed.include? [ri,ci]
flashed.add [ri,ci]
neighbours(board, ri, ci).each do |nri, nci|
board[nri][nci] += 1
flash(board, nri, nci, flashed)
end
end
sync_step = nil
1.step do |step|
flashed = Set.new
input.each_with_index do |row, ri|
row.each_with_index do |entry, ci|
input[ri][ci] += 1
end
end
input.each_with_index do |row, ri|
row.each_with_index do |entry, ci|
flash(input, ri, ci, flashed)
end
end
flashed.each do |ri, ci|
input[ri][ci] = 0
end
if flashed.count == input.count * input[0].count
sync_step = step
break
end
end
puts sync_step