R: fix ktLint and Detekt issues
This commit is contained in:
parent
98c91a107a
commit
6585014a1c
4 changed files with 77 additions and 49 deletions
|
|
@ -1,5 +1,7 @@
|
||||||
# TODO
|
# TODO
|
||||||
- [ ] Make ktlint pass checks
|
|
||||||
|
|
||||||
# RPP
|
# RPP
|
||||||
- [ ] ...
|
- [ ] ...
|
||||||
|
|
||||||
|
# DONE
|
||||||
|
- [x] Make ktlint pass checks
|
||||||
|
|
|
||||||
|
|
@ -1,43 +0,0 @@
|
||||||
package org.example
|
|
||||||
|
|
||||||
class Rover {
|
|
||||||
constructor(p: String) {
|
|
||||||
val s = p.split(' ')
|
|
||||||
if (s.size >= 3) {
|
|
||||||
rs.xx = s[0].toInt()
|
|
||||||
rs.yy = s[1].toInt()
|
|
||||||
rs.dd = s[2][0]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun go(cms: String) {
|
|
||||||
for (c in cms) {
|
|
||||||
when (c) {
|
|
||||||
'L' -> { when (rs.dd) { 'E' -> rs.dd = 'N' 'N' -> rs.dd = 'W' 'W' -> rs.dd = 'S' 'S' -> rs.dd = 'E'}}
|
|
||||||
'R' -> { when (rs.dd) { 'E' -> rs.dd = 'S' 'S' -> rs.dd = 'W' 'W' -> rs.dd = 'N' 'N' -> rs.dd = 'E'}}
|
|
||||||
'M' -> { when (rs.dd) { 'E' -> rs.xx++ 'S' -> rs.yy-- 'W' -> rs.xx-- 'N' -> rs.yy++}}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun g(z: Char) {
|
|
||||||
go(z.toString())
|
|
||||||
}
|
|
||||||
|
|
||||||
val xyd: String
|
|
||||||
get() = "${rs.xx} ${rs.yy} ${rs.dd}"
|
|
||||||
|
|
||||||
fun pos(): String {
|
|
||||||
return xyd
|
|
||||||
}
|
|
||||||
|
|
||||||
constructor() : this("")
|
|
||||||
|
|
||||||
private var rs = RoverState()
|
|
||||||
}
|
|
||||||
|
|
||||||
class RoverState {
|
|
||||||
var xx: Int = 0
|
|
||||||
var yy: Int = 0
|
|
||||||
var dd: Char = 'N'
|
|
||||||
}
|
|
||||||
64
src/main/kotlin/org/example/Rover.kt
Normal file
64
src/main/kotlin/org/example/Rover.kt
Normal file
|
|
@ -0,0 +1,64 @@
|
||||||
|
package org.example
|
||||||
|
|
||||||
|
class Rover {
|
||||||
|
constructor(p: String) {
|
||||||
|
val s = p.split(' ')
|
||||||
|
if (s.size >= 3) {
|
||||||
|
rs.xx = s[0].toInt()
|
||||||
|
rs.yy = s[1].toInt()
|
||||||
|
rs.dd = s[2][0]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun go(cms: String) {
|
||||||
|
for (c in cms) {
|
||||||
|
when (c) {
|
||||||
|
'L' -> {
|
||||||
|
when (rs.dd) {
|
||||||
|
'E' -> rs.dd = 'N'
|
||||||
|
'N' -> rs.dd = 'W'
|
||||||
|
'W' -> rs.dd = 'S'
|
||||||
|
'S' -> rs.dd = 'E'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
'R' -> {
|
||||||
|
when (rs.dd) {
|
||||||
|
'E' -> rs.dd = 'S'
|
||||||
|
'S' -> rs.dd = 'W'
|
||||||
|
'W' -> rs.dd = 'N'
|
||||||
|
'N' -> rs.dd = 'E'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
'M' -> {
|
||||||
|
when (rs.dd) {
|
||||||
|
'E' -> rs.xx++
|
||||||
|
'S' -> rs.yy--
|
||||||
|
'W' -> rs.xx--
|
||||||
|
'N' -> rs.yy++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun g(z: Char) {
|
||||||
|
go(z.toString())
|
||||||
|
}
|
||||||
|
|
||||||
|
val xyd: String
|
||||||
|
get() = "${rs.xx} ${rs.yy} ${rs.dd}"
|
||||||
|
|
||||||
|
fun pos(): String {
|
||||||
|
return xyd
|
||||||
|
}
|
||||||
|
|
||||||
|
constructor() : this("")
|
||||||
|
|
||||||
|
private var rs = RoverState()
|
||||||
|
}
|
||||||
|
|
||||||
|
class RoverState {
|
||||||
|
var xx: Int = 0
|
||||||
|
var yy: Int = 0
|
||||||
|
var dd: Char = 'N'
|
||||||
|
}
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
import org.example.Rover
|
package org.example
|
||||||
|
|
||||||
import org.junit.jupiter.api.Assertions.assertEquals
|
import org.junit.jupiter.api.Assertions.assertEquals
|
||||||
import org.junit.jupiter.params.ParameterizedTest
|
import org.junit.jupiter.params.ParameterizedTest
|
||||||
import org.junit.jupiter.params.provider.CsvSource
|
import org.junit.jupiter.params.provider.CsvSource
|
||||||
|
|
@ -20,11 +21,15 @@ class MarsRoverTests {
|
||||||
"1 2 S, M, 1 1 S",
|
"1 2 S, M, 1 1 S",
|
||||||
"1 2 W, M, 0 2 W",
|
"1 2 W, M, 0 2 W",
|
||||||
"1 2 N, LMLMLMLMM, 1 3 N",
|
"1 2 N, LMLMLMLMM, 1 3 N",
|
||||||
"3 3 E, MMRMMRMRRM, 5 1 E"
|
"3 3 E, MMRMMRMRRM, 5 1 E",
|
||||||
)
|
)
|
||||||
fun `execute commands`(startingPosition: String, instructions: String, expectedOutput: String) {
|
fun `execute commands`(
|
||||||
|
startingPosition: String,
|
||||||
|
instructions: String,
|
||||||
|
expectedOutput: String,
|
||||||
|
) {
|
||||||
val rover = Rover(startingPosition)
|
val rover = Rover(startingPosition)
|
||||||
rover.go(instructions)
|
rover.go(instructions)
|
||||||
assertEquals(expectedOutput, rover.pos())
|
assertEquals(expectedOutput, rover.pos())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue