-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday-11.rb
57 lines (46 loc) · 1.14 KB
/
day-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
51
52
53
54
55
56
57
# https://adventofcode.com/2021/day/10
# Dumbo Octopus, a cellular automaton
require_relative 'common'
DIRS = [-1, 0, 1].repeated_permutation(2).to_a
class Day11 < AdventDay
def first_part
grid = input
100.times.sum do
cycle(grid)
count_flashes(grid)
end
end
def second_part
grid = input
(1..).find do
cycle(grid)
count_flashes(grid) == 100
end
end
private
def cycle(grid, step: nil)
grid.each_key { |coords| grid[coords] += 1 }
grid.each_key { |coords| flash(grid, *coords) }
return step + 1 if step
end
def flash(grid, x, y)
return 0 if grid[[x, y]].nil? || grid[[x, y]] <= 9
grid[[x, y]] = 0
DIRS.sum do |dx, dy|
adjacent = [x + dx, y + dy]
grid[adjacent] += 1 unless grid[adjacent].nil? || grid[adjacent].zero?
flash(grid, *adjacent)
end
end
def count_flashes(grid)
grid.reduce(0) { |acc, elem| acc + elem.count(0) }
end
def convert_data(data)
super.each_with_object({}).with_index do |(line, obj), j|
line.chars.each_with_index do |char, i|
obj[[j, i]] = char.to_i
end
end
end
end
Day11.solve