-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day22.kt
66 lines (59 loc) · 2.42 KB
/
Day22.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
59
60
61
62
63
64
65
66
package aoc2016.day22
import util.Coord
import util.readInputLineByLine
import kotlin.math.abs
fun readInputToMap(path: String): Map<Coord, Node> {
val map = mutableMapOf<Coord, Node>()
val regex = Regex("/dev/grid/node-x([0-9]+)-y([0-9]+)\\s+([0-9]+)T\\s+([0-9]+)T\\s+([0-9]+)T\\s+([0-9]+)%")
for ((index, line) in readInputLineByLine(path).withIndex())
if (index != 0 && index != 1) {
val match = regex.matchEntire(line) ?: throw RuntimeException()
val numericMatch = match.groupValues.subList(1, match.groupValues.size).map { it.toInt() }
map[Coord(numericMatch[0], numericMatch[1])] = Node(numericMatch[2], numericMatch[3], numericMatch[4])
}
return map
}
fun countViableNodePairs(map: Map<Coord, Node>): Int {
val viablePairs = mutableListOf<Pair<Node, Node>>()
for (nodeEntry in map.entries)
map.entries
.filter { nodeEntry.key != it.key && nodeEntry.value.used != 0 && nodeEntry.value.used <= it.value.available }
.mapTo(viablePairs) { Pair(nodeEntry.value, it.value) }
return viablePairs.count()
}
fun computeFewestStepsToMoveData(map: Map<Coord, Node>): Long {
val hiX = 31
val hiY = 29
var wallStart: Node? = null
var unusedNode: Node? = null
for (y in 0 until hiY) {
for (x in 0 until hiX) {
val node = map[Coord(x, y)]
if (node!!.used == 0) {
unusedNode = node
unusedNode.x = x
unusedNode.y = y
} else if (node.size > 250) {
if (wallStart == null) {
wallStart = map[Coord(x - 1, y)]
wallStart!!.x = x - 1
wallStart.y = y
}
}
}
}
// Manhattan distance from the empty disk to the edge of the 'wall', from there to the goal, and then shifting the empty node around.
var result = abs(unusedNode!!.x - wallStart!!.x) + abs(unusedNode.y - wallStart.y)
result += abs(wallStart.x - hiX) + wallStart.y
return (result + 5 * (hiX - 1)).toLong()
}
fun printNodes(map: Map<Coord, Node>) {
for (y in 0..29) {
for (x in 0..31) {
val node = map[Coord(x, y)]!!
print(node.used.toString().padStart(3) + "/" + node.size.toString().padStart(3) + " -- ")
}
println()
}
}
data class Node(val size: Int, val used: Int, val available: Int, var x: Int = 0, var y: Int = 0)