Skip to content

Commit a581d78

Browse files
committed
Solved day06 part 1
1 parent 16107ec commit a581d78

File tree

2 files changed

+49
-3
lines changed

2 files changed

+49
-3
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ plugins {
44

55
// project meta data
66
group 'de.havox_design.aoc2023'
7-
version '0.5.1'
7+
version '0.5.2'
88

99
// Switch to gradle "all" distribution.
1010
wrapper {
Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,58 @@
11
package de.havox_design.aoc2023.day06
22

33
class Day06(private var filename: String) {
4-
fun solvePart1(): Long =
5-
288L
4+
private val DISTANCE_INDEX = 1
5+
private val DISTANCE_PREFIX = "Distance: "
6+
private val REGEX_SPACE = "\\s+".toRegex()
7+
private val TIME_INDEX = 0
8+
private val TIME_PREFIX = "Time: "
9+
10+
fun solvePart1(): Long {
11+
val combinations = convertInput(getResourceAsText(filename)).map { game -> simulate(game.first, game.second) }
12+
var product = 1L
13+
14+
for(value in combinations) {
15+
product *= value
16+
}
17+
18+
return product
19+
}
620

721
fun solvePart2(): Long =
822
0L
923

24+
private fun convertInput(input: List<String>): List<Pair<Long, Long>> {
25+
val times = input[TIME_INDEX]
26+
.substringAfter(TIME_PREFIX)
27+
.trim()
28+
.split(REGEX_SPACE)
29+
.map { it.toLong() }
30+
val distances = input[DISTANCE_INDEX]
31+
.substringAfter(DISTANCE_PREFIX)
32+
.trim()
33+
.split(REGEX_SPACE)
34+
.map { it.toLong() }
35+
val result = ArrayList<Pair<Long, Long>>()
36+
37+
for(index in times.indices) {
38+
result.add(Pair(times[index], distances[index]))
39+
}
40+
41+
return result
42+
}
43+
44+
private fun simulate(time: Long, goal: Long): Int {
45+
var count = 0
46+
47+
for (i in 0..< time) {
48+
if ((time - i) * i > goal) {
49+
count++
50+
}
51+
}
52+
53+
return count
54+
}
55+
1056
private fun getResourceAsText(path: String): List<String> =
1157
this.javaClass.classLoader.getResourceAsStream(path)!!.bufferedReader().readLines()
1258
}

0 commit comments

Comments
 (0)