Day23 part1
This commit is contained in:
parent
59dfc17ee1
commit
ff798eaffb
62
days/src/main/kotlin/Day23.kt
Normal file
62
days/src/main/kotlin/Day23.kt
Normal file
@ -0,0 +1,62 @@
|
||||
package be.vandewalleh.aoc.days
|
||||
|
||||
import be.vandewalleh.aoc.utils.input.Day
|
||||
import be.vandewalleh.aoc.utils.input.Input
|
||||
import be.vandewalleh.aoc.utils.input.Text
|
||||
import be.vandewalleh.aoc.utils.input.createDay
|
||||
|
||||
@Day(23)
|
||||
class Day23(@Text val input: Input<String>) {
|
||||
private val cups = input.value.toCharArray().map { it.toString().toInt() }
|
||||
|
||||
fun part1() {
|
||||
var cups = this.cups
|
||||
var current = cups.first()
|
||||
|
||||
repeat(100) {
|
||||
println("-- move ${it + 1} --")
|
||||
val (newCurrent, newCups) = move(current, cups)
|
||||
current = newCurrent
|
||||
cups = newCups
|
||||
}
|
||||
|
||||
println(cups.asSequence().infinite().dropWhile { it != 1 }.drop(1).take(cups.size - 1).joinToString(""))
|
||||
}
|
||||
|
||||
private fun Sequence<Int>.infinite() = sequence { while (true) yieldAll(this@infinite) }
|
||||
|
||||
private fun move(current: Int, cups: List<Int>): Pair<Int, List<Int>> {
|
||||
println("cups: ${cups.joinToString(" ").replace(current.toString(), "($current)")}")
|
||||
val mutableCups = cups.toMutableList()
|
||||
|
||||
val picked = cups.asSequence().infinite().dropWhile { it != current }.drop(1).take(3).toList()
|
||||
mutableCups.removeAll(picked)
|
||||
println("pick up: ${picked.joinToString(", ")}")
|
||||
|
||||
fun chooseDestination(cups: List<Int>): Int {
|
||||
val reordered = cups.asSequence().infinite().dropWhile { it != current }.take(mutableCups.size).toMutableList()
|
||||
return reordered.filter { it < current }.maxOrNull() ?: reordered.maxOrNull()!!
|
||||
}
|
||||
|
||||
val destinationCup = chooseDestination(mutableCups)
|
||||
|
||||
val out2 = mutableListOf<Int>()
|
||||
for (cup in mutableCups) {
|
||||
out2.add(cup)
|
||||
if (cup == destinationCup) {
|
||||
out2.addAll(picked)
|
||||
}
|
||||
}
|
||||
val newCurrentCup = out2.asSequence().infinite().dropWhile { it != current }.drop(1).first()
|
||||
return newCurrentCup to out2
|
||||
}
|
||||
|
||||
fun part2() {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
fun main() = with(createDay<Day23>("871369452")) {
|
||||
println(part1())
|
||||
println(part2())
|
||||
}
|
||||
1
days/src/main/resources/day23.txt
Normal file
1
days/src/main/resources/day23.txt
Normal file
@ -0,0 +1 @@
|
||||
871369452
|
||||
Loading…
x
Reference in New Issue
Block a user