Created
December 27, 2016 03:42
-
-
Save justinhj/05c6480899e18eb04dd666365d233f78 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object Xmas1 { | |
object Move { | |
def fromString(arg: String): Option[Move] = arg match { | |
case s if arg.startsWith("R") || arg.startsWith("L") => | |
try { | |
val turn = if(arg.charAt(0) == 'R') Right else Left | |
Some(Move(arg.substring(1).toInt, turn)) | |
} | |
catch { | |
case e: Exception => | |
None | |
} | |
case _ => None | |
} | |
} | |
case class Move(times: Int, direction: Turn) | |
val testInput = "R4, R5, L5, L5, L3, R2, R1, R1, L5, R5, R2, L1, L3, L4, R3, L1, L1, R2, R3, R3, R1, L3, L5, R3, R1, L1, R1, R2, L1, L4, L5, R4, R2, L192, R5, L2, R53, R1, L5, R73, R5, L5, R186, L3, L2, R1, R3, L3, L3, R1, L4, L2, R3, L5, R4, R3, R1, L1, R5, R2, R1, R1, R1, R3, R2, L1, R5, R1, L5, R2, L2, L4, R3, L1, R4, L5, R4, R3, L5, L3, R4, R2, L5, L5, R2, R3, R5, R4, R2, R1, L1, L5, L2, L3, L4, L5, L4, L5, L1, R3, R4, R5, R3, L5, L4, L3, L1, L4, R2, R5, R5, R4, L2, L4, R3, R1, L2, R5, L5, R1, R1, L1, L5, L5, L2, L1, R5, R2, L4, L1, R4, R3, L3, R1, R5, L1, L4, R2, L3, R5, R3, R1, L3" | |
val turnsFromInput = testInput.split(", ").flatMap { Move.fromString } | |
sealed trait Facing | |
object North extends Facing | |
object South extends Facing | |
object West extends Facing | |
object East extends Facing | |
sealed trait Turn | |
object Left extends Turn | |
object Right extends Turn | |
// assume an x,y grid where x runs west to east left to right | |
// and y is south to north bottom to top | |
case class Position(x: Int, y: Int, facing: Facing) { | |
def blocksAway = Math.abs(x) + Math.abs(y) | |
} | |
def executeTurn(currentFace: Facing, direction: Turn): Facing = direction match { | |
case Right => | |
currentFace match { | |
case North => East | |
case East => South | |
case South => West | |
case West => North | |
} | |
case Left => | |
currentFace match { | |
case North => West | |
case West => South | |
case South => East | |
case East => North | |
} | |
} | |
def executeMove(currentPos: Position, turn: Move): Position = { | |
val turned = currentPos.copy(facing = executeTurn(currentPos.facing, turn.direction)) | |
turned.facing match { | |
case North => | |
turned.copy(y = turned.y + turn.times) | |
case South => | |
turned.copy(y = turned.y - turn.times) | |
case West => | |
turned.copy(x = turned.x - turn.times) | |
case East => | |
turned.copy(x = turned.x + turn.times) | |
} | |
} | |
val sampleTurns = "R2, L3".split(", ").flatMap { Move.fromString } | |
def blocksAwayFromTurns(moves: Seq[Move]): Int = { | |
val finalPos = moves.foldLeft(Position(0,0,North)){ | |
(acc,move) => | |
executeMove(acc,move) | |
} | |
finalPos.blocksAway | |
} | |
assert(blocksAwayFromTurns(sampleTurns) == 5) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment