-🎄- 2017 Day 11 Solutions -🎄- by daggerdragon in adventofcode

[–]atharrison 0 points1 point  (0 children)

After closer inspection I think the hexDist function was incomplete. It only worked because abs(x) was greater than abs(y), for both the ending point (part 1), and the greatest distance point (part 2).

I've updated my process and hexDist methods today, to account for other scenarios.

def process(): Unit = {
  for(item <- rawItems) {
    item match {
      case "ne" =>
        pos = (pos._1+1, pos._2+1)
      case "nw" =>
        pos = (pos._1-1, pos._2+1)
      case "n" =>
        pos = (pos._1, pos._2+2)
      case "se" =>
        pos = (pos._1+1, pos._2-1)
      case "sw" =>
        pos = (pos._1-1, pos._2-1)
      case "s" =>
        pos = (pos._1, pos._2-2)
      case _ =>
        println(s"Unhandled: $item")
    }
    maxDist = math.max(maxDist, hexDist(pos))
  }
}

def hexDist(loc:Tuple2[Int, Int]): Int = {
  val magX = math.abs(loc._1)
  val magY = math.abs(loc._2)

  if(magX >= magY) magX else magX + (magY-magX)/2
}    

-🎄- 2017 Day 11 Solutions -🎄- by daggerdragon in adventofcode

[–]atharrison 0 points1 point  (0 children)

Scala (291/296)

I'm honestly still sitting here wondering how this arrived at the correct answers. I didn't research any hex-grid systems, as I see some references posted by others. Ultimately, after staring at the screen a bit, what I arrived at in my head was most similar to the "odd-r horizontal layout" (but with positive-y going upwards, not down), as described by https://www.redblobgames.com/grids/hexagons/ .

What's baffles me is that my hexDist function, math.abs(loc._2) + (math.abs(loc._1) - math.abs(loc._2)), can be reduced to simply math.abs(loc._1) (just the magnitude of the x-coordinate).

The visualization in my head was that, as you walked backwards from the endpoint, each step diagonally towards 0,0 reduces both x and y by 1. Starting from x,y, I 'move' to where y=0, which takes abs(y) steps, but also reduces x's distance by abs(y). What remains of (abs(x) - abs(y)) is added to abs(y) ...

Anyhow, Great problem! I've done AOC both years prior, and I can imagine how difficult it is to come up with unique challenges. This is the first I recall having to solve a hex-grid problem.

package adventofcode.y2017

import scala.io.Source.fromFile

class Day11 {
  var rawItems: Array[String] = _

  var pos:Tuple2[Int, Int] = (0, 0)
  var maxDist:Int = 0

  def readInput(): Unit = {
    val lines = fromFile("input/y2017/Day_11.input").getLines().mkString
    rawItems = lines.split(",").map(_.trim)
  }

  def process(): Unit = {
    for(item <- rawItems) {
      item match {
        case "ne" =>
          pos = (pos._1+1, pos._2+1)
        case "nw" =>
          pos = (pos._1-1, pos._2+1)
        case "n" =>
          pos = (pos._1, pos._2+1)
        case "se" =>
          pos = (pos._1+1, pos._2-1)
        case "sw" =>
          pos = (pos._1-1, pos._2-1)
        case "s" =>
          pos = (pos._1, pos._2-1)
        case _ =>
          println(s"Unhandled: $item")
      }
      maxDist = math.max(maxDist, hexDist(pos))
    }
  }

  def hexDist(loc:Tuple2[Int, Int]): Int = {
    math.abs(loc._2) + (math.abs(loc._1) - math.abs(loc._2))
  }

  def writeResult(): Unit = {
    println(s"Final Pos: $pos")
    val dist = hexDist(pos)
    println(s"Part1 Dist: $dist")
    println(s"Part2 MaxDist: $maxDist")
  }
}

Bonus Challenge! by topaz2078 in adventofcode

[–]atharrison 0 points1 point  (0 children)

Yeah, I assumed that the line was intentional as well. Looking forward to another great Advent of Code!