Created
December 17, 2020 05:26
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
space = Hash.new('.') | |
data.lines.map(&:chomp).each.with_index do |row, x| | |
row.each_char.with_index { |c, y| space[[ x, y, 0, 0 ]] = c } | |
end | |
6.times do | |
copy = space.dup | |
# bounds | |
coords = space.keys | |
mx, nx = coords.map { |c| c[0] }.min - 1, coords.map { |c| c[0] }.max + 1 | |
my, ny = coords.map { |c| c[1] }.min - 1, coords.map { |c| c[1] }.max + 1 | |
mz, nz = coords.map { |c| c[2] }.min - 1, coords.map { |c| c[2] }.max + 1 | |
mw, nw = coords.map { |c| c[3] }.min - 1, coords.map { |c| c[3] }.max + 1 | |
# advance | |
mx.upto(nx).each do |x| | |
my.upto(ny).each do |y| | |
mz.upto(nz).each do |z| | |
mw.upto(nw).each do |w| | |
adj = 0 | |
-1.upto(1).each do |dx| | |
-1.upto(1).each do |dy| | |
-1.upto(1).each do |dz| | |
-1.upto(1).each do |dw| | |
next if dx == 0 && dy == 0 && dz == 0 && dw == 0 | |
adj += 1 if space[[ x + dx, y + dy, z + dz, w + dw ]] == '#' | |
end | |
end | |
end | |
end | |
copy[[ x, y, z, w ]] = case space[[ x, y, z, w ]] | |
when '#' then [ 2, 3 ].include?(adj) ? '#' : '.' | |
when '.' then adj == 3 ? '#' : '.' | |
end | |
end | |
end | |
end | |
end | |
space = copy | |
end | |
p space.values.count { |c| c == '#' } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment