Day 1 part 2 complete
This commit is contained in:
parent
07594c5d9a
commit
1b8919b283
3 changed files with 24 additions and 11 deletions
4
justfile
4
justfile
|
@ -1,5 +1,5 @@
|
|||
run day inputFile: build
|
||||
java -jar ./build/libs/adventOfCode2019-1.0-SNAPSHOT-all.jar --day-to-run={{day}} --input-file={{inputFile}}
|
||||
run day part inputFile: build
|
||||
java -jar ./build/libs/adventOfCode2019-1.0-SNAPSHOT-all.jar --day-to-run={{day}} --part-to-run={{part}} --input-file={{inputFile}}
|
||||
|
||||
build:
|
||||
./gradlew clean shadowJar
|
||||
|
|
|
@ -8,14 +8,14 @@ import kotlin.RuntimeException
|
|||
|
||||
class CliParser: CliktCommand() {
|
||||
private val dayToRun: String by option(help = "Day to Run. Can be either an int (1) or a String (Day1)").required()
|
||||
private val partToRun: String by option(help = "Part to run. Usually 1 or 2").required()
|
||||
private val inputFile: Path by option().path().required()
|
||||
|
||||
override fun run() {
|
||||
println("dayToRun = $dayToRun")
|
||||
println("inputFile = $inputFile")
|
||||
when(dayToRun.replace("Day", "")) {
|
||||
"1" -> println(Day1().run(inputFile))
|
||||
else -> throw RuntimeException("Day not implementation yet")
|
||||
when(val it = "${dayToRun.replace("Day", "")}.${partToRun}") {
|
||||
"1.1" -> println(Day1().runPart1(inputFile))
|
||||
"1.2" -> println(Day1().runPart2(inputFile))
|
||||
else -> throw RuntimeException("Day or part (${it}) not implementation yet")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,14 +5,27 @@ import java.nio.file.Path
|
|||
import java.util.stream.Collectors
|
||||
|
||||
class Day1 {
|
||||
fun run(inputFile: Path): Long {
|
||||
fun runPart1(inputFile: Path): Long {
|
||||
return loadFile(inputFile).parallelStream()
|
||||
.map {calculateCost(it)}
|
||||
.map {calculateCost(it.toLong())}
|
||||
.collect(Collectors.summingLong { it } )
|
||||
}
|
||||
|
||||
private fun calculateCost(x: String): Long {
|
||||
return x.toLong().div(3).minus(2)
|
||||
fun runPart2(inputFile: Path): Long {
|
||||
return loadFile(inputFile).parallelStream()
|
||||
.map {calculateCostPart2(it.toLong())}
|
||||
.collect(Collectors.summingLong { it } )
|
||||
}
|
||||
|
||||
private fun calculateCost(x: Long): Long {
|
||||
return x.div(3).minus(2)
|
||||
}
|
||||
|
||||
private fun calculateCostPart2(x: Long): Long {
|
||||
return when (x > 6) {
|
||||
false -> 0
|
||||
true -> { val temp = calculateCost(x); temp + calculateCostPart2(temp) }
|
||||
}
|
||||
}
|
||||
|
||||
private fun loadFile(inputFile: Path): List<String> {
|
||||
|
|
Loading…
Add table
Reference in a new issue