Skip to content

Commit 84cb97e

Browse files
authored
Merge pull request #86 from tlarsen0/feature/day06.2-2024-10-03
Feature/day06.2 2024 10 03
2 parents 3114e0f + 2e2e38e commit 84cb97e

File tree

4 files changed

+95
-0
lines changed

4 files changed

+95
-0
lines changed

2023/day06_2/day06_2.iml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$/resources" type="java-resource" />
7+
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
8+
<sourceFolder url="file://$MODULE_DIR$/test" isTestSource="true" />
9+
<sourceFolder url="file://$MODULE_DIR$/testResources" type="java-test-resource" />
10+
</content>
11+
<orderEntry type="inheritedJdk" />
12+
<orderEntry type="sourceFolder" forTests="false" />
13+
<orderEntry type="library" name="KotlinJavaRuntime" level="project" />
14+
</component>
15+
</module>

2023/day06_2/main/resources/input

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Time: 54 70 82 75
2+
Distance: 239 1142 1295 1253
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Time: 7 15 30
2+
Distance: 9 40 200

2023/day06_2/src/Main.kt

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import java.io.File
2+
import java.math.BigInteger
3+
4+
fun main(args: Array<String>) {
5+
println("AOC 2023, Day 6, Part 2 starting!!!!")
6+
7+
val time = Time()
8+
val distance = Distance()
9+
File(args[0]).forEachLine {
10+
println("Data read-- $it")
11+
val theSplit = it.split("""\W+""".toRegex())
12+
13+
if(theSplit.first() == "Time") {
14+
val rebuildTime = ArrayList<Int>()
15+
16+
for(split in theSplit) {
17+
if(split == theSplit.first()) {
18+
// skip the first since it is "Time"
19+
continue
20+
}
21+
rebuildTime.add(split.toInt())
22+
}
23+
time.raceTime = rebuildValue(rebuildTime)
24+
} else {
25+
val rebuildDistance = ArrayList<Int>()
26+
for(split in theSplit) {
27+
if(split == theSplit.first()) {
28+
// skp the "Distance"
29+
continue
30+
}
31+
rebuildDistance.add(split.toInt())
32+
}
33+
distance.raceDistance = rebuildValue(rebuildDistance)
34+
}
35+
}
36+
val timeDistance = TimeDistance(time, distance)
37+
38+
val answer = timeDistance.calculateWaysToBeat()
39+
40+
println("The answer: $answer")
41+
42+
println("AOC 2023, Day 6, Part 2 completed!!!")
43+
}
44+
45+
fun rebuildValue(stringValues:ArrayList<Int>):BigInteger {
46+
val theString:StringBuilder = StringBuilder()
47+
stringValues.forEach { theString.append(it) }
48+
return theString.toString().toBigInteger()
49+
}
50+
51+
class Time {
52+
var raceTime:BigInteger = BigInteger.valueOf(0L)
53+
}
54+
55+
class Distance {
56+
var raceDistance:BigInteger = BigInteger.valueOf(0L)
57+
}
58+
59+
class TimeDistance(private val time: Time, private val distance: Distance) {
60+
fun calculateWaysToBeat():BigInteger {
61+
return runRace(time.raceTime, distance.raceDistance)
62+
}
63+
64+
private fun runRace(raceTime: BigInteger, raceDistance: BigInteger):BigInteger {
65+
var winCount = BigInteger.valueOf(0L)
66+
for(t:Long in 1.. raceTime.toLong()) {
67+
val runTime = raceTime.minus(BigInteger.valueOf(t))
68+
//val calculatedDistance = t * runTime
69+
val calculatedDistance = BigInteger.valueOf(t).multiply(runTime)
70+
if(calculatedDistance > raceDistance) {
71+
winCount++
72+
}
73+
}
74+
return winCount
75+
}
76+
}

0 commit comments

Comments
 (0)