From 1a30bb23a102b6a0c6267e8ce92b5a5ce4120fb4 Mon Sep 17 00:00:00 2001 From: Alpha Chen Date: Thu, 24 Dec 2020 22:13:48 -0800 Subject: [PATCH] [2020][ruby][24.x] use axial coordinates --- 2020/ruby/day_24.rb | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/2020/ruby/day_24.rb b/2020/ruby/day_24.rb index d017007..9a0bd74 100644 --- a/2020/ruby/day_24.rb +++ b/2020/ruby/day_24.rb @@ -7,17 +7,17 @@ def parse(line) ss = StringScanner.new(line) until ss.eos? deltas << case - when ss.scan(/e/) then [ 1, -1, 0] - when ss.scan(/se/) then [ 0, -1, 1] - when ss.scan(/sw/) then [-1, 0, 1] - when ss.scan(/w/) then [-1, 1, 0] - when ss.scan(/nw/) then [ 0, 1, -1] - when ss.scan(/ne/) then [ 1, 0, -1] + when ss.scan(/e/) then [ 1, 0] + when ss.scan(/se/) then [ 0, 1] + when ss.scan(/sw/) then [-1, 1] + when ss.scan(/w/) then [-1, 0] + when ss.scan(/nw/) then [ 0, -1] + when ss.scan(/ne/) then [ 1, -1] else fail end end - deltas.inject {|(x,y,z),(dx,dy,dz)| [x+dx, y+dy, z+dz] } + deltas.inject {|c,d| c.zip(d).map {|c,d| c+d }} end NEIGHBORS = %w[ e se sw w nw ne ].map {|dir| parse(dir) } @@ -25,11 +25,11 @@ def tick(tiles) coords = Set.new(tiles.flat_map {|coord| [ coord, - *NEIGHBORS.map {|n| coord.zip(n).map {|c,d| c + d }}, + *NEIGHBORS.map {|n| coord.zip(n).map {|c,d| c+d }}, ] }) Set.new(coords.select {|coord| - live_neighbors = NEIGHBORS.map {|n| coord.zip(n).map {|c,d| c + d }}.count {|c| tiles.include?(c) } + live_neighbors = NEIGHBORS.map {|n| coord.zip(n).map {|c,d| c+d }}.count {|c| tiles.include?(c) } if tiles.include?(coord) (1..2).cover?(live_neighbors) else