generated from Jadarma/advent-of-code-kotlin-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathY2015D20.kt
37 lines (31 loc) · 1.48 KB
/
Y2015D20.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
package aockt.y2015
import io.github.jadarma.aockt.core.Solution
object Y2015D20 : Solution {
/**
* Finds the first house to get enough presents from the infinite elves.
*
* @param target The minimum amount of gifts per house.
* @param giftsPerVisit How many gifts each elf visit adds to the house total.
* @param maxVisitsPerElf Elves will stop delivering after this many stops. A negative value means infinite visits.
* @return The number of the first house to have received at least [target] gifts, or -1 if not possible.
*/
private fun houseGifts(target: Int, giftsPerVisit: Int, maxVisitsPerElf: Int = -1): Int {
if (target <= 0 || giftsPerVisit <= 0) return -1
val maximumHouses = target / giftsPerVisit
fun maxHouseFor(elf: Int) = when {
maxVisitsPerElf < 0 -> maximumHouses
else -> minOf(maximumHouses, elf * maxVisitsPerElf + 1)
}
return IntArray(maximumHouses) { 0 }
.apply {
for (elf in 1 until maximumHouses) {
for (number in elf until maxHouseFor(elf) step elf) {
this[number] += elf * giftsPerVisit
}
}
}
.indexOfFirst { it >= target }
}
override fun partOne(input: String) = houseGifts(target = input.toInt(), giftsPerVisit = 10)
override fun partTwo(input: String) = houseGifts(target = input.toInt(), giftsPerVisit = 11, maxVisitsPerElf = 50)
}