-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathd11.rb
More file actions
87 lines (66 loc) · 1.67 KB
/
d11.rb
File metadata and controls
87 lines (66 loc) · 1.67 KB
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
80
81
82
83
84
85
86
87
# frozen_string_literal: true
require_relative '../day'
class Day11 < Day
def part_one
data = parse_input(read_input)
flashes = 0
100.times do
flashes += check_flashes(data).size
end
flashes.to_s
end
def part_two
data = parse_input(read_input)
i = 0
loop do
flashed = check_flashes(data)
i += 1
break if flashed.size == data.size * data[0].size
end
i.to_s
end
private
def parse_input(input)
input.split("\n").map { |line| line.chars.map(&:to_i) }
end
def check_flashes(data)
flashed = {}
(0...data.size).each do |y|
(0...data[0].size).each do |x|
next if flashed[pos_to_str(x, y)]
data[y][x] = data[y][x] + 1
next if data[y][x] <= 9
flash(data, x, y, flashed)
end
end
flashed
end
def flash(data, x, y, flashed)
data[y][x] = 0
flashed[pos_to_str(x, y)] = true
neighbors = [
[0, -1], # TOP
[1, -1], # TOP_RIGHT
[1, 0], # RIGHT
[1, 1], # BOTTOM_RIGHT
[0, 1], # BOTTOM
[-1, 1], # BOTTOM_LEFT
[-1, 0], # LEFT
[-1, -1] # TOP_LEFT
]
neighbors.each do |x_mod, y_mod|
neighbor_x = x + x_mod
neighbor_y = y + y_mod
next if flashed[pos_to_str(neighbor_x, neighbor_y)]
next if neighbor_x.negative? || neighbor_x >= data[0].size
next if neighbor_y.negative? || neighbor_y >= data.size
neighbor_value = data[neighbor_y][neighbor_x]
data[neighbor_y][neighbor_x] = neighbor_value + 1
next if data[neighbor_y][neighbor_x] <= 9
flash(data, neighbor_x, neighbor_y, flashed)
end
end
def pos_to_str(x, y)
"#{x},#{y}"
end
end