From 4ff438de363c14510670727da366c35a0a3820bf Mon Sep 17 00:00:00 2001 From: Alexander Munch-Hansen Date: Tue, 19 Dec 2017 02:43:23 +0100 Subject: [PATCH] 11th day --- hexagons.rb | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 hexagons.rb diff --git a/hexagons.rb b/hexagons.rb new file mode 100644 index 0000000..ce056a1 --- /dev/null +++ b/hexagons.rb @@ -0,0 +1,21 @@ +require 'matrix' +input = File.read("input_11.rb").strip.split(",").map(&:to_sym) +HEX_DIRS = { + :n => Vector[0,1,-1], :ne => Vector[-1,1,0], + :se => Vector[-1,0,1], :s => Vector[0,-1,1], + :sw => Vector[1,-1,0], :nw => Vector[1,0,-1] +} + +def dist path + + path.reduce({:vec => Vector[0,0,0], :lol => 0}) do |mem,step| + mem[:vec]= new = mem[:vec] + HEX_DIRS[step] + mem[:lol] = [new.map(&:abs).sum / 2, mem[:lol]].max + mem + end +end + +out = dist input +p out[:vec].map(&:abs).sum / 2, out[:lol] + +