Last active
November 8, 2019 20:44
-
-
Save Tom-Kuhn/8c47b6b17565b336e10071e93c159dbd to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/** | |
* Problem definition: https://github.com/mikehadlow/Journeys | |
* Edit and run this code here: https://pl.kotl.in/_TytttU28 | |
*/ | |
package com.tomkuhn.journey | |
import com.tomkuhn.journey.Direction.* | |
enum class Direction { North, East, South, West } | |
data class Position(val x: Int, val y: Int, val direction: Direction) | |
data class Problem(val initialPosition: Position, val instructions: String, val expectedFinalPosition: Position) | |
fun main() { | |
val newLine = System.getProperty("line.separator") | |
fun parseDirection(direction:String) = when(direction){ | |
"N" -> North | |
"E" -> East | |
"S" -> South | |
else -> West | |
} | |
fun createPositionFromInput(input: List<String>) = Position(input[0].toInt(), input[1].toInt(), parseDirection(input[2])) | |
fun inputToProblem(inputLines: List<String>) = Problem(createPositionFromInput(inputLines[0].split(" ")), | |
inputLines[1], | |
createPositionFromInput(inputLines[2].split(" "))) | |
val problems = """1 1 E | |
RFRFRFRF | |
1 1 E | |
3 2 N | |
FRRFLLFFRRFLL | |
3 3 N | |
0 3 W | |
LLFFFLFLFL | |
2 4 S""".split("$newLine$newLine").map{ inputToProblem(it.split(newLine)) } | |
problems.forEach { executeInstructions(it) } | |
} | |
fun executeInstructions(problem: Problem) { | |
println("Testing problem: $problem") | |
fun moveForward(position: Position) = when(position.direction){ | |
North -> position.copy(y = position.y + 1) | |
East -> position.copy(x = position.x + 1) | |
South -> position.copy(y = position.y - 1) | |
West -> position.copy(x = position.x - 1) | |
} | |
fun turnRight(position: Position) = when(position.direction){ | |
North -> position.copy(direction = East) | |
East -> position.copy(direction = South) | |
South -> position.copy(direction = West) | |
West -> position.copy(direction = North) | |
} | |
fun turnLeft(position: Position) = when(position.direction){ | |
North -> position.copy(direction = West) | |
East -> position.copy(direction = North) | |
South -> position.copy(direction = East) | |
West -> position.copy(direction = South) | |
} | |
fun executeInstruction(instruction: Char, position: Position) = when(instruction) { | |
'L' -> turnLeft(position) | |
'R' -> turnRight(position) | |
'F' -> moveForward(position) | |
else -> position | |
} | |
val finalPosition = problem.instructions.fold(problem.initialPosition) { | |
position: Position, instruction -> executeInstruction(instruction, position) | |
} | |
when(finalPosition == problem.expectedFinalPosition) { | |
true -> println(" The final position matches the expected position ${problem.expectedFinalPosition} :)") | |
false -> println(" Uh Oh! The final position ${finalPosition} does not match the expected position ${problem.expectedFinalPosition} :(") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment