Skip to content

Commit

Permalink
Convert Groovy to Kotlin
Browse files Browse the repository at this point in the history
  • Loading branch information
amedee committed Dec 25, 2023
1 parent a05f291 commit 82a3142
Show file tree
Hide file tree
Showing 18 changed files with 178 additions and 228 deletions.
7 changes: 0 additions & 7 deletions .idea/misc.xml

This file was deleted.

43 changes: 0 additions & 43 deletions build.gradle

This file was deleted.

41 changes: 41 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
plugins {
kotlin("jvm") version "1.9.21"
id("org.jetbrains.dokka") version "1.9.10"
application
}

application {
mainClass.set("be.amedee.adventofcode.AppKt")
}

group = "be.amedee"
version = "1.0-SNAPSHOT"

repositories {
mavenCentral()
}

dependencies {
implementation(kotlin("stdlib"))
testImplementation("org.jetbrains.kotlin:kotlin-test")
testImplementation("io.kotest:kotest-core:4.1.3")
testImplementation("io.kotest:kotest-assertions-core:5.6.2")
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
}

tasks.test {
useJUnitPlatform()
testLogging {
events("skipped", "passed", "failed")
showStandardStreams = true
}
}

tasks.named("run") {
dependsOn(tasks.test)
group = "application"
}

kotlin {
jvmToolchain(21)
}
42 changes: 0 additions & 42 deletions src/main/groovy/be/amedee/adventofcode/aoc2015/day01/Day01.groovy

This file was deleted.

Empty file.
Empty file.
Empty file.
7 changes: 0 additions & 7 deletions src/main/kotlin/Main.kt

This file was deleted.

8 changes: 8 additions & 0 deletions src/main/kotlin/be/amedee/adventofcode/App.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package be.amedee.adventofcode

import be.amedee.adventofcode.aoc2015.day01.Day01

fun main() {
val day01 = Day01()
day01.main()
}
64 changes: 33 additions & 31 deletions src/main/kotlin/be/amedee/adventofcode/aoc2015/day01/Day01.kt
Original file line number Diff line number Diff line change
@@ -1,40 +1,42 @@
package be.amedee.adventofcode.aoc2015.day01

fun readStringFromResource(filePath: String) = {}.javaClass.classLoader.getResource(filePath)?.readText()

fun findFloorAndBasementPosition(instructions: String): Pair<Int, Int> {
var floor = 0
var basementPosition = -1

for ((index, char) in instructions.withIndex()) {
when (char) {
'(' -> floor++
')' -> {
floor--
if (floor == -1 && basementPosition == -1) {
basementPosition = index + 1 // Adjust index to start from 1
}
}
/**
* Santa is trying to deliver presents in a large apartment building,
* but he can't find the right floor - the directions he got are a little confusing.
*/
class Day01 {

/**
* Single move to the next floor, up or down.
*
* @param instruction a single elevator instruction
* @return do we go up or down
*/
val move: (String) -> Int = {
when (it) {
"(" -> 1
")" -> -1
else -> 0
}
}

return Pair(floor, basementPosition)
}

fun main() {
// Specify the file path
val filePath = "be/amedee/adventofcode/aoc2015/day01/input"

val puzzleInput: String = readStringFromResource(filePath)!!

val (floor, basementPosition) = findFloorAndBasementPosition(puzzleInput)
/**
* The entire list of elevator instructions.
*
* @param instructions the list of elevator instructions, Lisp-style
* @return at which floor Santa ends up
*/
val followInstructions: (String) -> Int = { instructionList ->
instructionList
.toList()
.sumOf { move(it.toString()) }
}

println("Santa ends up on floor $floor.")
fun main() {
val filePath = "be/amedee/adventofcode/aoc2015/day01/input"
val puzzleInput: String = readStringFromResource(filePath)!!

if (basementPosition != -1) {
println("Santa enters the basement at character position $basementPosition.")
} else {
println("Santa never enters the basement.")
val endFloor = followInstructions(puzzleInput)
println("Santa ends up on floor $endFloor.")
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package be.amedee.adventofcode.aoc2015.day01

fun readStringFromResource(filePath: String) = {}.javaClass.classLoader.getResource(filePath)?.readText()

fun findFloorAndBasementPosition(instructions: String): Pair<Int, Int> {
var floor = 0
var basementPosition = -1

for ((index, char) in instructions.withIndex()) {
when (char) {
'(' -> floor++
')' -> {
floor--
if (floor == -1 && basementPosition == -1) {
basementPosition = index + 1 // Adjust index to start from 1
}
}
}
}

return Pair(floor, basementPosition)
}

fun main() {
// Specify the file path
val filePath = "be/amedee/adventofcode/aoc2015/day01/input"

val puzzleInput: String = readStringFromResource(filePath)!!

val (floor, basementPosition) = findFloorAndBasementPosition(puzzleInput)

println("Santa ends up on floor $floor.")

if (basementPosition != -1) {
println("Santa enters the basement at character position $basementPosition.")
} else {
println("Santa never enters the basement.")
}

}

This file was deleted.

Empty file.
Empty file.
Empty file.
Empty file removed src/test/kotlin/.gitkeep
Empty file.
30 changes: 0 additions & 30 deletions src/test/kotlin/MainKtTest.kt

This file was deleted.

Loading

0 comments on commit 82a3142

Please sign in to comment.