-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay23CrabCups.kt
36 lines (26 loc) · 1.27 KB
/
Day23CrabCups.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
package adventofcode.year2020
import adventofcode.Puzzle
import adventofcode.PuzzleInput
class Day23CrabCups(customInput: PuzzleInput? = null) : Puzzle(customInput) {
override fun partOne(): Int {
val result = input.toCharArray().map(Char::toString).map(String::toInt).toMutableList().playCrabCups(100)
return (result.subList(result.indexOf(1) + 1, result.size) + result.subList(0, result.indexOf(1))).joinToString("").toInt()
}
companion object {
private fun List<Int>.destination(current: Int) = if (current - 1 < minOrNull()!!) maxOrNull()!! else current - 1
private fun MutableList<Int>.playCrabCups(rounds: Int): MutableList<Int> {
var currentCup = first()
repeat((1..rounds).count()) {
val pickUp = (1..3).map { this[(indexOf(currentCup) + it) % size] }
removeAll(pickUp)
val destination =
generateSequence(destination(currentCup)) { destination(it) }
.first { !pickUp.contains(it) }
val destinationIndex = indexOf(destination)
addAll(destinationIndex + 1, pickUp)
currentCup = this[(indexOf(currentCup) + 1) % size]
}
return this
}
}
}