Skip to content

Commit

Permalink
Refactor 2015/Day01
Browse files Browse the repository at this point in the history
  • Loading branch information
Amedee Van Gasse authored and Amedee Van Gasse committed Jan 8, 2024
1 parent 173fed7 commit 836de91
Showing 1 changed file with 18 additions and 12 deletions.
30 changes: 18 additions & 12 deletions src/main/kotlin/be/amedee/adventofcode/aoc2015/day01/Day01.kt
Original file line number Diff line number Diff line change
Expand Up @@ -46,27 +46,33 @@ fun findBasementPosition(instructions: String): Int {
}

class Day01 {
fun readElevatorInstructionsFromFile(fileName: String): String {
val packageName = javaClass.`package`.name.replace(".", "/")
val filePath = "/$packageName/$fileName"
val inputStream = javaClass.getResourceAsStream(filePath)
return BufferedReader(InputStreamReader(inputStream!!)).use { it.readText().trim() }
}
fun readElevatorInstructionsFromFile(fileName: String): String =
BufferedReader(
InputStreamReader(
this.javaClass.getResourceAsStream(
"/${
this.javaClass.`package`.name.replace(
".",
"/",
)
}/$fileName",
)!!,
),
).use { it.readText().trim() }
}

private const val INPUT = "input"

/**
* 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.
*/
fun main() {
val inputFile = "input"
val puzzleInput = Day01().readElevatorInstructionsFromFile(inputFile)
val puzzleInput = Day01().readElevatorInstructionsFromFile(INPUT)

val endFloor = followInstructions()(puzzleInput)
println("Santa ends up on floor $endFloor.")
println("Santa ends up on floor ${followInstructions()(puzzleInput)}.")

val basementPosition = findBasementPosition(puzzleInput)
when (basementPosition) {
when (val basementPosition = findBasementPosition(puzzleInput)) {
0 -> println("Santa never enters the basement.")
else -> println("Santa enters the basement at character position ${"%,d".format(basementPosition)}.")
}
Expand Down

0 comments on commit 836de91

Please sign in to comment.