Advent of Code (Day 11)


Check out out day 11 here.

This helped make the challenge almost trivial

Run down


  \ n  /
nw +--+ ne
  /    \
-+      +-
  \    /
sw +--+ se
  / s  \

Basically, you move on a hexagonal grid north, north east, etc. You need to find your end distance from the start after so many moves in which ever direction. From the link above, each move can be represented by a certain vector, ie north would be [-1, 1, 0], and all you’d have to do is sum the moves and find the biggest absolute value of each vector component. This would theoretically be one of the straightest line from the start to the end I think.

Challenge One

I basically just eyeballed the resultant vector to find the distance to the start.


require 'matrix'

test_input = File.read('./Desktop/input.txt').gsub("\n", '').split(',')

conversion = { "se" => Vector[1, 0, -1], "n" => Vector[-1, 1, 0],
               "nw" => Vector[-1, 0, 1], "sw" => Vector[0, -1, 1],
               "s" => Vector[1, -1, 0],  "ne" => Vector[0, 1, -1] }

print test_input.map { |direction| conversion[direction] }.inject(&:+)

Challenge Two

For this one, you had to find the longest distance at any point in travel. I couldn’t just eyeball the distances after each move so I had to make a max_distance method.


require 'matrix'

test_input = File.read('./Desktop/input.txt').gsub("\n", '').split(',')

conversion = { "se" => Vector[1, 0, -1], "n" => Vector[-1, 1, 0],
               "nw" => Vector[-1, 0, 1], "sw" => Vector[0, -1, 1],
               "s" => Vector[1, -1, 0],  "ne" => Vector[0, 1, -1] }

furthest = 0
current_spot = Vector[0, 0, 0]

def max_distance(vector)
  [vector[0].abs, vector[1].abs, vector[2].abs].max
end

test_input.each do |direction|
  current_spot += conversion[direction]
  max = max_distance(current_spot)
  furthest = max if max > furthest
end

print furthest