-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday17.rb
50 lines (45 loc) · 1.19 KB
/
day17.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
require 'set'
input = File.read('day17_input.txt')
test_input = <<EOT
.#.
..#
###
EOT
grid = {}
input.lines.each.with_index do |line, x|
line.strip.chars.each.with_index do |c, y|
grid[[x, y, 0, 0]] = (c == '#')
end
end
def relative_neighbour_coords
@relative_coords ||= [-1,-1,-1,-1,0,0,0,0,1,1,1,1].permutation(4).uniq - [[0,0,0,0]]
end
def neighbours(grid, coords)
results = {}
relative_neighbour_coords.map do |dx,dy,dz,dw|
x,y,z,w = [coords[0] + dx, coords[1] + dy, coords[2] + dz, coords[3] + dw]
results[[x,y,z,w]] = grid[[x,y,z,w]] || false
end
results
end
6.times do |cycle|
new_grid = {}
grid.each do |coords, value|
# determine state for all neighbours
neighbours(grid, coords).each do |neighbour_coords, value2|
active_count = 0
neighbours(grid, neighbour_coords).each do |_, value3|
break if active_count > 3
active_count += 1 if value3
end
# add neighbours to new_grid
if (value2 && [2,3].include?(active_count)) ||
(!value2 && active_count == 3)
new_grid[neighbour_coords] = true
else # skip
end
end
end
puts "After cycle #{cycle + 1}: #{ new_grid.size }"
grid = new_grid
end