Skip to content

Instantly share code, notes, and snippets.

@snarkbait
Created December 11, 2017 08:01
Show Gist options
  • Save snarkbait/2834ac933c161a212c28e70af98100e3 to your computer and use it in GitHub Desktop.
Save snarkbait/2834ac933c161a212c28e70af98100e3 to your computer and use it in GitHub Desktop.
Advent of Code - Day 11 Kotlin
package Advent2017
import util.FileIO
fun hexDistance(x: Int, y: Int): Int {
return maxOf(Math.abs(x), Math.abs(y), Math.abs(x + y))
}
fun main(args: Array<String>) {
val map = hashMapOf<String, Pair<Int, Int>>()
map.put("n", Pair(0, -1))
map.put("ne", Pair(1, -1))
map.put("se", Pair(1, 0))
map.put("s", Pair(0, 1))
map.put("sw", Pair(-1, 1))
map.put("nw", Pair(-1, 0))
val input = FileIO.getFileAsString("advent2017_day11.txt")
var x = 0
var y = 0
var dist = 0
var furthest = 0
for (each in input.split(",")) {
val move = map.get(each)
if (move != null) {
x += move.first
y += move.second
dist = hexDistance(x, y)
if (dist > furthest) furthest = dist
}
}
println("Part 1: $dist")
println("Part 2: $furthest")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment