-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution.rb
236 lines (196 loc) · 4.37 KB
/
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
require './utils'
Point = Struct.new(:x, :y) do
def self.[](x, y)
Point.new(x, y)
end
end
class Integer
def max(i)
self > i ? self : i
end
end
class LightGrid
def initialize(val, rows: 1_000, cols: 1_000)
@grid = [val] * rows * cols
@cols = cols
end
def turn_on(origin, bound)
raise NotImplementedError
end
def turn_off(origin, bound)
raise NotImplementedError
end
def toggle(origin, bound)
raise NotImplementedError
end
private
def change_state(origin, bound)
for y in origin.y..bound.y
offset = @cols * y
for x in origin.x..bound.x
@grid[offset + x] = yield @grid[offset + x]
end
end
end
end
class SwitchLightGrid < LightGrid
def initialize(rows: 1_000, cols: 1_000)
super(false, rows: rows, cols: cols)
end
def count
@grid.filter(&:itself).count
end
def turn_on(origin, bound)
change_state(origin, bound) { true }
end
def turn_off(origin, bound)
change_state(origin, bound) { false }
end
def toggle(origin, bound)
change_state(origin, bound) { |b| !b }
end
end
class DimmableLightGrid < LightGrid
def initialize(rows: 1_000, cols: 1_000)
super(0, rows: rows, cols: cols)
end
def brightness
@grid.sum
end
def turn_on(origin, bound)
change_state(origin, bound) { |i| i + 1 }
end
def turn_off(origin, bound)
change_state(origin, bound) { |i| 0.max(i - 1) }
end
def toggle(origin, bound)
change_state(origin, bound) { |i| i + 2 }
end
end
class Solution
def tests
test_switchlightgrid
test_parse_line
test_solve_a
test_dimmablelightgrid
test_solve_b
:ok
end
def part_a
solve_a(SwitchLightGrid.new, File.read('input'))
end
def part_b
solve_b(DimmableLightGrid.new, File.read('input'))
end
private
def test_switchlightgrid
g = SwitchLightGrid.new(rows: 2, cols: 2)
# 0 1
# 0 - -
# 1 - -
assert g.count, 0
# 0 1
# 0 + -
# 1 + -
g.turn_on(Point[0, 0], Point[0, 1])
assert g.count, 2
# 0 1
# 0 + -
# 1 - -
g.turn_off(Point[0, 1], Point[1, 1])
assert g.count, 1
# 0 1
# 0 - +
# 1 + +
g.toggle(Point[0, 0], Point[1, 1])
assert g.count, 3
end
def test_dimmablelightgrid
g = DimmableLightGrid.new(rows: 2, cols: 2)
# 0 1
# 0 0 0
# 1 0 0
assert g.brightness, 0
# 0 1
# 0 1 0
# 1 1 0
g.turn_on(Point[0, 0], Point[0, 1])
assert g.brightness, 2
# 0 1
# 0 1 0
# 1 0 0
g.turn_off(Point[0, 1], Point[1, 1])
assert g.brightness, 1
# 0 1
# 0 3 2
# 1 2 2
g.toggle(Point[0, 0], Point[1, 1])
assert g.brightness, 9
end
def test_parse_line
assert parse_line('turn on 0,0 through 999,999'), [
:on, Point[0, 0], Point[999, 999]
]
assert parse_line('toggle 0,0 through 999,0'), [
:toggle, Point[0, 0], Point[999, 0]
]
assert parse_line('turn off 499,499 through 500,500'), [
:off, Point[499, 499], Point[500, 500]
]
end
def test_solve_a
g = SwitchLightGrid.new(rows: 2, cols: 2)
input = <<~data
turn on 0,0 through 0,1
turn off 0,1 through 1,1
toggle 0,0 through 1,1
data
assert solve_a(g, input), 3
end
def test_solve_b
g = DimmableLightGrid.new(rows: 2, cols: 2)
input = <<~data
turn on 0,0 through 0,1
turn off 0,1 through 1,1
toggle 0,0 through 1,1
data
assert solve_b(g, input), 9
end
MATCHERS = {
on: /turn on (\d+),(\d+) through (\d+),(\d+)/,
off: /turn off (\d+),(\d+) through (\d+),(\d+)/,
toggle: /toggle (\d+),(\d+) through (\d+),(\d+)/
}.freeze
def parse_line(line)
MATCHERS.each do |k, re|
m = line.match(re)
next unless m
ps = make_points(m.captures)
return ps.prepend(k)
end
end
def make_points(captures)
ps = captures.map(&:to_i)
[Point[ps[0], ps[1]], Point[ps[2], ps[3]]]
end
def solve_worker(grid, input)
input.split("\n").map { |l| parse_line(l) }.each do |action, origin, bound|
case action
when :on
grid.turn_on(origin, bound)
when :off
grid.turn_off(origin, bound)
when :toggle
grid.toggle(origin, bound)
end
end
end
def solve_a(grid, input)
solve_worker(grid, input)
grid.count
end
def solve_b(grid, input)
solve_worker(grid, input)
grid.brightness
end
end