File tree Expand file tree Collapse file tree 2 files changed +49
-3
lines changed
day06/src/main/kotlin/de/havox_design/aoc2023/day06 Expand file tree Collapse file tree 2 files changed +49
-3
lines changed Original file line number Diff line number Diff line change @@ -4,7 +4,7 @@ plugins {
44
55// project meta data
66group ' de.havox_design.aoc2023'
7- version ' 0.5.1 '
7+ version ' 0.5.2 '
88
99// Switch to gradle "all" distribution.
1010wrapper {
Original file line number Diff line number Diff line change 11package de.havox_design.aoc2023.day06
22
33class 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}
You can’t perform that action at this time.
0 commit comments