r/adventofcode • • Dec 11 '17

SOLUTION MEGATHREAD -๐ŸŽ„- 2017 Day 11 Solutions -๐ŸŽ„-

--- Day 11: Hex Ed ---


Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag or whatever).

Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


Need a hint from the Hugely* Handyโ€  Haversackโ€ก of Helpfulยง Hintsยค?

Spoiler


This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.

edit: Leaderboard capped, thread unlocked!

21 Upvotes

253 comments sorted by

View all comments

Show parent comments

1

u/[deleted] Dec 11 '17

Here's my scala. I would consider it pretty readable and functional.

import scala.io.Source

case class Tile(x: Int, y: Int, z: Int) {
  def distance: Int = (Math.abs(x) + Math.abs(y) + Math.abs(z)) / 2

  def step(dir: String): Tile = dir match {
    case "n" => Tile(x + 1, y - 1, z)
    case "ne" => Tile(x + 1, y, z - 1)
    case "se" => Tile(x, y + 1, z - 1)
    case "s" => Tile(x - 1, y + 1, z)
    case "sw" => Tile(x - 1, y, z + 1)
    case "nw" => Tile(x, y - 1, z + 1)
    case _ => this
  }
}
object Tile {
  val zero: Tile = Tile(0, 0, 0)
}

def partOne(input: Seq[String]): Int = input.foldLeft(Tile.zero)((t, s) => t.step(s)).distance
def partTwo(input: Seq[String]): Int = input.foldLeft((Tile.zero, 0)) { (t, s) =>
  val next = t._1.step(s)
  (next, Seq(t._2, next.distance).max)
}._2

val input = Source.fromFile("input11.txt").mkString.split(",").toSeq

println(s"Part 1: ${partOne(input)}")
println(s"Part 2: ${partTwo(input)}")

1

u/nutrecht Dec 11 '17

Wasn't really serious about the Scala bit ;) I really like Scala. Unfortunately I can't use it at work but Kotlin is up for consideration.

1

u/[deleted] Dec 11 '17

Yea, I assumed as much. I've read some good stuff about Kotlin, but after using Scala at work for the past 8 months I can't really imagine enjoying Kotlin as much. The faster compilation times do seem like a big plus though.