[2020][ruby][24.x] use axial coordinates

pull/2/head
Alpha Chen 4 years ago
parent 319dd0129b
commit 1a30bb23a1

@ -7,17 +7,17 @@ def parse(line)
ss = StringScanner.new(line) ss = StringScanner.new(line)
until ss.eos? until ss.eos?
deltas << case deltas << case
when ss.scan(/e/) then [ 1, -1, 0] when ss.scan(/e/) then [ 1, 0]
when ss.scan(/se/) then [ 0, -1, 1] when ss.scan(/se/) then [ 0, 1]
when ss.scan(/sw/) then [-1, 0, 1] when ss.scan(/sw/) then [-1, 1]
when ss.scan(/w/) then [-1, 1, 0] when ss.scan(/w/) then [-1, 0]
when ss.scan(/nw/) then [ 0, 1, -1] when ss.scan(/nw/) then [ 0, -1]
when ss.scan(/ne/) then [ 1, 0, -1] when ss.scan(/ne/) then [ 1, -1]
else fail else fail
end end
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 end
NEIGHBORS = %w[ e se sw w nw ne ].map {|dir| parse(dir) } NEIGHBORS = %w[ e se sw w nw ne ].map {|dir| parse(dir) }

Loading…
Cancel
Save