diff --git a/days/src/main/kotlin/Day23.kt b/days/src/main/kotlin/Day23.kt new file mode 100644 index 0000000..1b5e296 --- /dev/null +++ b/days/src/main/kotlin/Day23.kt @@ -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) { + 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.infinite() = sequence { while (true) yieldAll(this@infinite) } + + private fun move(current: Int, cups: List): Pair> { + 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 { + 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() + 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("871369452")) { + println(part1()) + println(part2()) +} diff --git a/days/src/main/resources/day23.txt b/days/src/main/resources/day23.txt new file mode 100644 index 0000000..57823fd --- /dev/null +++ b/days/src/main/resources/day23.txt @@ -0,0 +1 @@ +871369452