Day 1 part 2 complete

This commit is contained in:
anthonycicc 2019-12-02 17:29:49 -05:00
parent 07594c5d9a
commit 1b8919b283
3 changed files with 24 additions and 11 deletions

View file

@ -1,5 +1,5 @@
run day inputFile: build run day part inputFile: build
java -jar ./build/libs/adventOfCode2019-1.0-SNAPSHOT-all.jar --day-to-run={{day}} --input-file={{inputFile}} java -jar ./build/libs/adventOfCode2019-1.0-SNAPSHOT-all.jar --day-to-run={{day}} --part-to-run={{part}} --input-file={{inputFile}}
build: build:
./gradlew clean shadowJar ./gradlew clean shadowJar

View file

@ -8,14 +8,14 @@ import kotlin.RuntimeException
class CliParser: CliktCommand() { 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 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() private val inputFile: Path by option().path().required()
override fun run() { override fun run() {
println("dayToRun = $dayToRun") when(val it = "${dayToRun.replace("Day", "")}.${partToRun}") {
println("inputFile = $inputFile") "1.1" -> println(Day1().runPart1(inputFile))
when(dayToRun.replace("Day", "")) { "1.2" -> println(Day1().runPart2(inputFile))
"1" -> println(Day1().run(inputFile)) else -> throw RuntimeException("Day or part (${it}) not implementation yet")
else -> throw RuntimeException("Day not implementation yet")
} }
} }
} }

View file

@ -5,14 +5,27 @@ import java.nio.file.Path
import java.util.stream.Collectors import java.util.stream.Collectors
class Day1 { class Day1 {
fun run(inputFile: Path): Long { fun runPart1(inputFile: Path): Long {
return loadFile(inputFile).parallelStream() return loadFile(inputFile).parallelStream()
.map {calculateCost(it)} .map {calculateCost(it.toLong())}
.collect(Collectors.summingLong { it } ) .collect(Collectors.summingLong { it } )
} }
private fun calculateCost(x: String): Long { fun runPart2(inputFile: Path): Long {
return x.toLong().div(3).minus(2) 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> { private fun loadFile(inputFile: Path): List<String> {