-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday18.rb
215 lines (202 loc) · 4.83 KB
/
day18.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
# coding: utf-8
# http://AdventOfCode.com/
# --- Day 18: Like a GIF For Your Yard ---
#
# After the million lights incident, the fire code has gotten stricter: now, at most ten thousand
# lights are allowed. You arrange them in a 100x100 grid.
#
# Never one to let you down, Santa again mails you instructions on the ideal lighting
# configuration. With so few lights, he says, you'll have to resort to animation.
#
# Start by setting your lights to the included initial configuration (your puzzle input). A #
# means "on", and a . means "off".
#
# Then, animate your grid in steps, where each step decides the next configuration based on the
# current one. Each light's next state (either on or off) depends on its current state and the
# current states of the eight lights adjacent to it (including diagonals). Lights on the edge of
# the grid might have fewer than eight neighbors; the missing ones always count as "off".
#
# For example, in a simplified 6x6 grid, the light marked A has the neighbors numbered 1 through
# 8, and the light marked B, which is on an edge, only has the neighbors marked 1 through 5:
#
# 1B5...
# 234...
# ......
# ..123.
# ..8A4.
# ..765.
#
# The state a light should have next is based on its current state (on or off) plus the number of
# neighbors that are on:
#
# A light which is on stays on when 2 or 3 neighbors are on, and turns off otherwise.
# A light which is off turns on if exactly 3 neighbors are on, and stays off otherwise.
# All of the lights update simultaneously; they all consider the same current state before moving to the next.
#
# Here's a few steps from an example configuration of another 6x6 grid:
#
# Initial state:
# .#.#.#
# ...##.
# #....#
# ..#...
# #.#..#
# ####..
#
# After 1 step:
# ..##..
# ..##.#
# ...##.
# ......
# #.....
# #.##..
#
# After 2 steps:
# ..###.
# ......
# ..###.
# ......
# .#....
# .#....
#
# After 3 steps:
# ...#..
# ......
# ...#..
# ..##..
# ......
# ......
#
# After 4 steps:
# ......
# ......
# ..##..
# ..##..
# ......
# ......
# After 4 steps, this example has four lights on.
#
# In your grid of 100x100 lights, given your initial configuration, how many lights are on after 100 steps?
#
# --- Part Two ---
#
# You flip the instructions over; Santa goes on to point out that this is all just an implementation of Conway's Game of Life. At least, it was, until you notice that something's wrong with the grid of lights you bought: four lights, one in each corner, are stuck on and can't be turned off. The example above will actually run like this:
#
# Initial state:
# ##.#.#
# ...##.
# #....#
# ..#...
# #.#..#
# ####.#
#
# After 1 step:
# #.##.#
# ####.#
# ...##.
# ......
# #...#.
# #.####
#
# After 2 steps:
# #..#.#
# #....#
# .#.##.
# ...##.
# .#..##
# ##.###
#
# After 3 steps:
# #...##
# ####.#
# ..##.#
# ......
# ##....
# ####.#
#
# After 4 steps:
# #.####
# #....#
# ...#..
# .##...
# #.....
# #.#..#
#
# After 5 steps:
# ##.###
# .##..#
# .##...
# .##...
# #.#...
# ##...#
# After 5 steps, this example now has 17 lights on.
#
# In your grid of 100x100 lights, given your initial configuration, but with the four corners
# always in the on state, how many lights are on after 100 steps?
#
require_relative 'input'
day = /day(\d+)\.rb/.match(__FILE__)[1].to_i
input = <<-EOF
.#.#.#
...##.
#....#
..#...
#.#..#
####..
EOF
input = Input.for_day(day)
steps = 1000
class Grid
def initialize(content)
@grid = content.split.map{|_|_.split(//)}
end
def neighbors(row_index, cell_index)
rows, cols = self.size
cells = 0
if row_index > 0
row = @grid[row_index-1]
cells += row[[0,cell_index-1].max .. [cols-1,cell_index+1].min].join.count('#-')
end
cells += @grid[row_index][cell_index-1].count('#-') if cell_index > 0
cells += @grid[row_index][cell_index+1].count('#-') if cell_index < cols-1
if row_index < rows-1
row = @grid[row_index+1]
cells += row[[0,cell_index-1].max .. [cols-1,cell_index+1].min].join.count('#-')
end
cells
end
private :neighbors
def step
@grid.each.with_index do |row, row_index|
row.each.with_index do |cell, cell_index|
cell.replace cell.tr('.#',
['.-','.-',
'.#',
'+#',
'.-','.-','.-','.-','.-',
][neighbors(row_index, cell_index)])
end
end
@grid = @grid.map{|row| row.map{|cell| cell.tr('#.+-','#.#.')} }
end
def fritz
@grid[0][0] = '#'
@grid[0][-1] = '#'
@grid[-1][0] = '#'
@grid[-1][-1] = '#'
end
def to_s
@grid.map{|_|_.join}.join("\n")
end
def size
[@grid.size, @grid.map{|_|_.length}.max]
end
def lights_on
@grid.map{|_|_.join.count('#')}.reduce(:+)
end
end
grid = Grid.new(input)
grid.fritz
top = `tput ho`
steps.times { grid.step; grid.fritz ; print top; print grid.to_s }
puts grid.lights_on