-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay6.kt
38 lines (26 loc) · 1.25 KB
/
Day6.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package adventofcode
import adventofcode.util.FileReader
import adventofcode.util.Input
object Day6 {
fun part1Day6(): Int = powerOfWaysToBeatTheRecord()
fun part2Day6(): Int = waysToBeatOneRecord()
private val input = FileReader.read(6, Input.PUZZLE)
private val timeToRecordList = mutableListOf<Pair<Long, Long>>()
private val numberRegex = Regex("\\d+")
init {
val times = numberRegex.findAll(input[0]).map{it.value.toLong()}.toList()
val records = numberRegex.findAll(input[1]).map{it.value.toLong()}.toList()
IntRange(0, times.size -1).forEach { timeToRecordList.add(times[it] to records[it]) }
}
private fun powerOfWaysToBeatTheRecord(): Int {
return timeToRecordList.map{ waysToBeatRecord(it) }.reduce{acc, i -> acc * i}
}
private fun waysToBeatOneRecord(): Int {
val time = timeToRecordList.joinToString("") { it.first.toString() }.toLong()
val record = timeToRecordList.joinToString("") { it.second.toString() }.toLong()
return waysToBeatRecord(time to record)
}
private fun waysToBeatRecord(record: Pair<Long, Long>): Int {
return LongRange(0, record.first).map { nr -> nr * (record.first - nr) }.count { time -> time > record.second }
}
}