-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path10.kts
More file actions
31 lines (26 loc) · 818 Bytes
/
Copy path10.kts
File metadata and controls
31 lines (26 loc) · 818 Bytes
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
import java.io.File
val adapters = File("10.input").readLines().map(String::toInt).sorted()
val maxJump = 3
// Part 1
val chain = listOf(0) + adapters + listOf(adapters.maxOrNull()!!.plus(maxJump))
chain.windowed(2)
.map { (a, b) -> b - a }
.groupingBy { it }
.eachCount()
.let { println(it[1]!! * it[3]!!) }
// Part 2
var memo = mutableMapOf<Pair<Int, Int>, Long>()
fun count(start: Int, used: Int): Long {
if (used == chain.count()) return 1
if (memo.containsKey(start to used)) return memo[start to used]!!
return (1..maxJump)
.map { skip ->
val head = chain.getOrNull(used + skip - 1)
if (head == null) 0
else if (head - start > maxJump) 0
else count(head, used + skip)
}
.sum()
.also { memo[start to used] = it }
}
count(chain.first(), 1).let(::println)