-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay24.kt
58 lines (50 loc) · 1.76 KB
/
Day24.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package aoc2017.day24
import util.readInputLineByLine
fun readInputBridges(path: String): List<Bridge> {
return readInputLineByLine(path)
.map { it.split("/") }
.map { Bridge(it[0].toInt(), it[1].toInt()) }
}
var maxStrength = 0
var maxStrengthOfLongest = 0
var maxDepth = 0
fun findStrongestBridge(list: List<Bridge>): Int {
createBridgeTree(list, 0, Node(Bridge(0, 0), mutableListOf()))
return maxStrength
}
fun findStrengthOfLongestBridge(list: List<Bridge>): Int {
createBridgeTree(list, 0, Node(Bridge(0, 0), mutableListOf()))
return maxStrengthOfLongest
}
fun createBridgeTree(list: List<Bridge>, start: Int, parentNode: Node, sumSoFar: Int = 0, depth: Int = 0): Node {
val bridgesWithRightEnd = mutableListOf<Bridge>()
for (item in list) {
if (item.first == start || item.second == start) {
bridgesWithRightEnd.add(item)
}
}
if (bridgesWithRightEnd.isEmpty()) {
if (parentNode.sum > maxStrength)
maxStrength = parentNode.sum
if (depth > maxDepth) {
maxDepth = depth
maxStrengthOfLongest = maxStrength
}
return parentNode
}
for (bridge in bridgesWithRightEnd) {
val newStart = if (bridge.first == start) bridge.second else bridge.first
parentNode.children.add(
createBridgeTree(
list.minus(bridge),
newStart,
Node(bridge, mutableListOf(), sumSoFar + bridge.first + bridge.second),
sumSoFar + bridge.first + bridge.second,
depth + 1
)
)
}
return parentNode
}
data class Bridge(val first: Int, val second: Int)
data class Node(val bridge: Bridge, val children: MutableList<Node>, val sum: Int = 0)