From f668b8256d09b538ee170bc3e35cc9f3299eccfc Mon Sep 17 00:00:00 2001 From: Alpha Chen Date: Wed, 16 Dec 2020 21:34:38 -0800 Subject: [PATCH] [2020][ruby][17.x] --- 2020/ruby/day_17.rb | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 2020/ruby/day_17.rb diff --git a/2020/ruby/day_17.rb b/2020/ruby/day_17.rb new file mode 100644 index 0000000..a60de7b --- /dev/null +++ b/2020/ruby/day_17.rb @@ -0,0 +1,29 @@ +state = ARGF.read.split("\n").flat_map.with_index {|line,y| + line.chars.map.with_index {|c,x| [[0,0,y,x], c == "#"] } +}.to_h + +NEIGHBORS = (0...3**4).map {|n| n.digits(3) }.map {|n| (0..3).map {|x| n.fetch(x, 0) - 1 }} - [[0,0,0,0]] + +def tick(state) + actives = state.select {|_,c| c }.keys + ww, zz, yy, xx = actives.transpose.map(&:minmax).map {|min,max| [min-1, max+1] } + Range.new(*ww).flat_map {|w| + Range.new(*zz).flat_map {|z| + Range.new(*yy).flat_map {|y| + Range.new(*xx).map {|x| + active_neighbors = NEIGHBORS.map {|dw,dz,dy,dx| state[[w+dw,z+dz,y+dy, x+dx]] }.count(&:itself) + wzyx = [w,z,y,x] + [ wzyx, + state[wzyx] ? (2..3).cover?(active_neighbors) : active_neighbors == 3 + ] + } + } + } + }.to_h +end + +6.times do + state = tick(state) +end + +p state.values.count(&:itself)