Skip to content

Commit 3114e0f

Browse files
authored
Merge pull request #85 from tlarsen0/feature/day06.1-2024-06-25
Adding configuration and solution to Advent of Code 2023, Day 6 Part 1.
2 parents 271ad22 + 12b08eb commit 3114e0f

File tree

4 files changed

+85
-0
lines changed

4 files changed

+85
-0
lines changed

2023/day06_1/day06_1.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$/src/main/kotlin" isTestSource="false" />
7+
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
8+
<sourceFolder url="file://$MODULE_DIR$/src/test/kotlin" isTestSource="true" />
9+
<sourceFolder url="file://$MODULE_DIR$/src/test/resources" 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_1/src/main/kotlin/Main.kt

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

2023/day06_1/src/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

0 commit comments

Comments
 (0)