Skip to content

Commit 644f876

Browse files
committed
Day 11 Ruby solutions
1 parent b7d5a22 commit 644f876

File tree

2 files changed

+207
-0
lines changed

2 files changed

+207
-0
lines changed

11-1.rb

+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
#!/usr/bin/env ruby
2+
3+
require 'numo/narray'
4+
5+
class Map
6+
attr_accessor :num_flashes
7+
8+
def initialize(input)
9+
@size = 10
10+
@map = Numo::UInt8.zeros(@size, @size)
11+
input.each_index do |row|
12+
input[row].each_index do |col|
13+
@map[row, col] = input[row][col]
14+
end
15+
end
16+
@flashed = Numo::UInt8.zeros(@size, @size)
17+
@num_flashes = 0
18+
end
19+
20+
def increase(row, col)
21+
return unless row.between?(0, @size - 1)
22+
return unless col.between?(0, @size - 1)
23+
24+
@map[row, col] += 1
25+
end
26+
27+
def energize
28+
@size.times do |row|
29+
@size.times do |col|
30+
increase(row, col)
31+
end
32+
end
33+
end
34+
35+
def flash
36+
flashed = false
37+
@size.times do |row|
38+
@size.times do |col|
39+
next unless @map[row, col] > 9 and
40+
@flashed[row, col].zero?
41+
42+
@num_flashes += 1
43+
flashed = true
44+
@flashed[row, col] = 1
45+
increase(row - 1, col - 1)
46+
increase(row - 1, col)
47+
increase(row - 1, col + 1)
48+
increase(row, col - 1)
49+
increase(row, col + 1)
50+
increase(row + 1, col - 1)
51+
increase(row + 1, col)
52+
increase(row + 1, col + 1)
53+
end
54+
end
55+
flashed
56+
end
57+
58+
def reset
59+
@size.times do |row|
60+
@size.times do |col|
61+
@map[row, col] = 0 if @flashed[row, col] == 1
62+
end
63+
end
64+
@flashed.fill 0
65+
end
66+
67+
def step
68+
energize
69+
loop do
70+
break unless flash
71+
end
72+
reset
73+
end
74+
75+
def to_s
76+
s = "<#{self.class}:\n"
77+
@size.times do |row|
78+
@size.times do |col|
79+
s += "#{@map[row, col]} "
80+
end
81+
s += "\n"
82+
end
83+
s += '>'
84+
s
85+
end
86+
87+
def inspect
88+
to_s
89+
end
90+
end
91+
92+
input = File.read('11.input').lines.map(&:strip).map { |row| (row.split '').map &:to_i }
93+
94+
map = Map.new(input)
95+
96+
100.times { |_| map.step }
97+
98+
print map.num_flashes, "\n"

11-2.rb

+109
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
#!/usr/bin/env ruby
2+
3+
require 'numo/narray'
4+
5+
class Map
6+
attr_accessor :num_flashes
7+
8+
def initialize(input)
9+
@size = 10
10+
@map = Numo::UInt8.zeros(@size, @size)
11+
input.each_index do |row|
12+
input[row].each_index do |col|
13+
@map[row, col] = input[row][col]
14+
end
15+
end
16+
@flashed = Numo::UInt8.zeros(@size, @size)
17+
@num_flashes = 0
18+
end
19+
20+
def increase(row, col)
21+
return unless row.between?(0, @size - 1)
22+
return unless col.between?(0, @size - 1)
23+
24+
@map[row, col] += 1
25+
end
26+
27+
def energize
28+
@size.times do |row|
29+
@size.times do |col|
30+
increase(row, col)
31+
end
32+
end
33+
end
34+
35+
def flash
36+
flashed = false
37+
@size.times do |row|
38+
@size.times do |col|
39+
next unless @map[row, col] > 9 and
40+
@flashed[row, col].zero?
41+
42+
@num_flashes += 1
43+
flashed = true
44+
@flashed[row, col] = 1
45+
increase(row - 1, col - 1)
46+
increase(row - 1, col)
47+
increase(row - 1, col + 1)
48+
increase(row, col - 1)
49+
increase(row, col + 1)
50+
increase(row + 1, col - 1)
51+
increase(row + 1, col)
52+
increase(row + 1, col + 1)
53+
end
54+
end
55+
flashed
56+
end
57+
58+
def reset
59+
@size.times do |row|
60+
@size.times do |col|
61+
@map[row, col] = 0 if @flashed[row, col] == 1
62+
end
63+
end
64+
@flashed.fill 0
65+
end
66+
67+
def step
68+
energize
69+
loop do
70+
break unless flash
71+
end
72+
all_flashed = true
73+
@size.times do |row|
74+
@size.times do |col|
75+
all_flashed = false if @flashed[row, col].zero?
76+
end
77+
end
78+
reset
79+
all_flashed
80+
end
81+
82+
def to_s
83+
s = "<#{self.class}:\n"
84+
@size.times do |row|
85+
@size.times do |col|
86+
s += "#{@map[row, col]} "
87+
end
88+
s += "\n"
89+
end
90+
s += '>'
91+
s
92+
end
93+
94+
def inspect
95+
to_s
96+
end
97+
end
98+
99+
input = File.read('11.input').lines.map(&:strip).map { |row| (row.split '').map &:to_i }
100+
101+
map = Map.new(input)
102+
103+
step = 0
104+
loop do
105+
step += 1
106+
break if map.step
107+
end
108+
109+
print step, "\n"

0 commit comments

Comments
 (0)