Day23 part2
This commit is contained in:
parent
ff798eaffb
commit
d793a47801
@ -9,54 +9,110 @@ import be.vandewalleh.aoc.utils.input.createDay
|
|||||||
class Day23(@Text val input: Input<String>) {
|
class Day23(@Text val input: Input<String>) {
|
||||||
private val cups = input.value.toCharArray().map { it.toString().toInt() }
|
private val cups = input.value.toCharArray().map { it.toString().toInt() }
|
||||||
|
|
||||||
fun part1() {
|
private fun <T> ringSequence(head: Ring.Node<T>) = generateSequence(head) { it.next }
|
||||||
var cups = this.cups
|
|
||||||
var current = cups.first()
|
|
||||||
|
|
||||||
repeat(100) {
|
class Ring<T>(items: Iterable<T>) {
|
||||||
println("-- move ${it + 1} --")
|
|
||||||
val (newCurrent, newCups) = move(current, cups)
|
class Node<T>(var value: T) {
|
||||||
current = newCurrent
|
lateinit var next: Node<T>
|
||||||
cups = newCups
|
override fun toString() = "Node($value -> ${next.value})"
|
||||||
}
|
}
|
||||||
|
|
||||||
println(cups.asSequence().infinite().dropWhile { it != 1 }.drop(1).take(cups.size - 1).joinToString(""))
|
private var last: Node<T>? = null
|
||||||
|
private var first: Node<T>? = null
|
||||||
|
|
||||||
|
init {
|
||||||
|
items.forEach { item ->
|
||||||
|
val node = Node(item)
|
||||||
|
if (first == null) first = node
|
||||||
|
last?.next = node
|
||||||
|
last = node
|
||||||
|
}
|
||||||
|
check(first != null && last != null)
|
||||||
|
last?.next = first!!
|
||||||
|
}
|
||||||
|
|
||||||
|
fun first() = first!!
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun Sequence<Int>.infinite() = sequence { while (true) yieldAll(this@infinite) }
|
private fun Ring.Node<Int>.cached(size: Int): Array<Ring.Node<Int>> =
|
||||||
|
arrayOfNulls<Ring.Node<Int>>(size + 1).also { array ->
|
||||||
|
array[0] = Ring.Node(-1)
|
||||||
|
ringSequence(this).take(size).forEach { array[it.value] = it }
|
||||||
|
} as Array<Ring.Node<Int>>
|
||||||
|
|
||||||
private fun move(current: Int, cups: List<Int>): Pair<Int, List<Int>> {
|
fun part1(): String {
|
||||||
println("cups: ${cups.joinToString(" ").replace(current.toString(), "($current)")}")
|
var currentNode = Ring(cups).first()
|
||||||
val mutableCups = cups.toMutableList()
|
val cache = currentNode.cached(cups.size)
|
||||||
|
val max = cups.maxOrNull()!!
|
||||||
|
|
||||||
val picked = cups.asSequence().infinite().dropWhile { it != current }.drop(1).take(3).toList()
|
repeat(100) { currentNode = move(max, cache, currentNode) }
|
||||||
mutableCups.removeAll(picked)
|
|
||||||
println("pick up: ${picked.joinToString(", ")}")
|
|
||||||
|
|
||||||
fun chooseDestination(cups: List<Int>): Int {
|
return ringSequence(currentNode)
|
||||||
val reordered = cups.asSequence().infinite().dropWhile { it != current }.take(mutableCups.size).toMutableList()
|
.dropWhile { it.value != 1 }
|
||||||
return reordered.filter { it < current }.maxOrNull() ?: reordered.maxOrNull()!!
|
.drop(1)
|
||||||
|
.take(cups.size - 1)
|
||||||
|
.map { it.value }
|
||||||
|
.joinToString("")
|
||||||
|
}
|
||||||
|
|
||||||
|
fun part2(): Long {
|
||||||
|
fun fillCups(): List<Int> {
|
||||||
|
val cups = ArrayList<Int>(1_000_000)
|
||||||
|
cups.addAll(this.cups)
|
||||||
|
val highest = this.cups.maxOrNull()!!
|
||||||
|
|
||||||
|
for (i in 1..1_000_000 - cups.size) {
|
||||||
|
cups.add(highest + i)
|
||||||
|
}
|
||||||
|
|
||||||
|
return cups
|
||||||
}
|
}
|
||||||
|
|
||||||
val destinationCup = chooseDestination(mutableCups)
|
val size = 1_000_000
|
||||||
|
var currentNode = Ring(fillCups()).first()
|
||||||
|
val cache = currentNode.cached(size)
|
||||||
|
|
||||||
val out2 = mutableListOf<Int>()
|
repeat(10_000_000) { currentNode = move(max = size, cache, currentNode) }
|
||||||
for (cup in mutableCups) {
|
|
||||||
out2.add(cup)
|
val one = cache[1]
|
||||||
if (cup == destinationCup) {
|
val a = one.next
|
||||||
out2.addAll(picked)
|
val b = a.next
|
||||||
|
return a.value.toLong() * b.value.toLong()
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun move(max: Int, cache: Array<Ring.Node<Int>>, current: Ring.Node<Int>): Ring.Node<Int> {
|
||||||
|
val a = current.next
|
||||||
|
val b = a.next
|
||||||
|
val c = b.next
|
||||||
|
|
||||||
|
current.next = c.next
|
||||||
|
|
||||||
|
val aa = a.value
|
||||||
|
val bb = b.value
|
||||||
|
val cc = c.value
|
||||||
|
|
||||||
|
val destinationNode: Ring.Node<Int>
|
||||||
|
var i = current.value - 1
|
||||||
|
while (true) {
|
||||||
|
if (i < 1) i = max
|
||||||
|
val value = cache[i].value
|
||||||
|
if (value == aa || value == bb || value == cc) {
|
||||||
|
i--
|
||||||
|
} else {
|
||||||
|
destinationNode = cache[i]
|
||||||
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
val newCurrentCup = out2.asSequence().infinite().dropWhile { it != current }.drop(1).first()
|
|
||||||
return newCurrentCup to out2
|
|
||||||
}
|
|
||||||
|
|
||||||
fun part2() {
|
|
||||||
|
|
||||||
|
val destinationNextNode = destinationNode.next
|
||||||
|
destinationNode.next = a
|
||||||
|
c.next = destinationNextNode
|
||||||
|
return current.next
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun main() = with(createDay<Day23>("871369452")) {
|
fun main() = with(createDay<Day23>()) {
|
||||||
println(part1())
|
println(part1())
|
||||||
println(part2())
|
println(part2())
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user