-
Notifications
You must be signed in to change notification settings - Fork 0
/
v2.rb
63 lines (49 loc) · 1.39 KB
/
v2.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
# https://adventofcode.com/2020/day/17
require "set"
sample_1 = "
.#.
..#
###
"
d = sample_1.split.map { |x| x.split("") }
actives = (0...d.length)
.flat_map { |y|
(0...d[y].length)
.filter { |x| d[y][x] == "#" }
.map { |x| [x, y, 0, 0] }
}.to_set
def recurse(actives, maxCycles, cycle)
if cycle > maxCycles
return actives
end
coordsToCheck = actives.flat_map { |a| [a, *getNeighbours(a)] }.to_set
puts "cycle=#{cycle}: checking #{coordsToCheck.length} relevant coords"
newActives = coordsToCheck.filter { |c| getNewState(c, actives) }.to_set
puts "cycle=#{cycle}: going from #{actives.length} to #{newActives.length} active cubes"
puts
return recurse(newActives, maxCycles, cycle + 1)
end
def getNewState(coord, actives)
activeNeighbours = getNeighbours(coord).filter { |n| actives.include?(n) }.count
if actives.include?(coord)
return [2, 3].include?(activeNeighbours)
else
return [3].include?(activeNeighbours)
end
end
def getNeighbours(coord)
res = ((coord[3] - 1)..(coord[3] + 1)).flat_map do |w|
((coord[2] - 1)..(coord[2] + 1)).flat_map do |z|
((coord[1] - 1)..(coord[1] + 1)).flat_map do |y|
((coord[0] - 1)..(coord[0] + 1)).map do |x|
[x, y, z, w]
end
end
end
end
res.delete(coord)
return res.to_set
end
numCycles = 6
res = recurse(actives, numCycles, 1).length
puts "should be 848" unless res == 848