|
| 1 | +package de.ronny_h.aoc.year2017.day16 |
| 2 | + |
| 3 | +import de.ronny_h.aoc.AdventOfCode |
| 4 | +import de.ronny_h.aoc.year2017.day16.DanceMove.* |
| 5 | +import de.ronny_h.aoc.year2017.day16.MutableRingList.Companion.mutableRingListOf |
| 6 | + |
| 7 | +fun main() = PermutationPromenade().run("kgdchlfniambejop", "fjpmholcibdgeakn") |
| 8 | + |
| 9 | +class PermutationPromenade : AdventOfCode<String>(2017, 16) { |
| 10 | + override fun part1(input: List<String>): String = doTheDance("abcdefghijklmnop", input) |
| 11 | + override fun part2(input: List<String>): String = doTheDance("abcdefghijklmnop", input, 1_000_000_000) |
| 12 | + |
| 13 | + fun doTheDance(programsLine: String, input: List<String>, times: Int = 1): String { |
| 14 | + val programs = mutableRingListOf(programsLine) |
| 15 | + val danceMoves = input.parseDanceMoves() |
| 16 | + |
| 17 | + var cycleTime = 0 |
| 18 | + while (cycleTime < times) { |
| 19 | + danceMoves.forEach { it.doTheMove(programs) } |
| 20 | + cycleTime++ |
| 21 | + if (programs.toJoinedString() == programsLine) { |
| 22 | + break |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + val remaining = times % cycleTime |
| 27 | + repeat(remaining) { |
| 28 | + danceMoves.forEach { it.doTheMove(programs) } |
| 29 | + } |
| 30 | + return programs.toJoinedString() |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +sealed interface DanceMove { |
| 35 | + fun doTheMove(programs: MutableRingList<Char>) |
| 36 | + |
| 37 | + data class Spin(val x: Int) : DanceMove { |
| 38 | + override fun doTheMove(programs: MutableRingList<Char>) { |
| 39 | + programs.shiftRight(x) |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + data class Exchange(val posA: Int, val posB: Int) : DanceMove { |
| 44 | + override fun doTheMove(programs: MutableRingList<Char>) { |
| 45 | + programs.swap(posA, posB) |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + data class Partner(val programA: Char, val programB: Char) : DanceMove { |
| 50 | + override fun doTheMove(programs: MutableRingList<Char>) { |
| 51 | + programs.swap(programA, programB) |
| 52 | + } |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +fun List<String>.parseDanceMoves() = single().split(",").map { |
| 57 | + when (it.first()) { |
| 58 | + 's' -> Spin(it.substringAfter("s").toInt()) |
| 59 | + 'x' -> { |
| 60 | + val (a, b) = it.substringAfter("x").split("/") |
| 61 | + Exchange(a.toInt(), b.toInt()) |
| 62 | + } |
| 63 | + |
| 64 | + 'p' -> { |
| 65 | + val (a, b) = it.substringAfter("p").split("/") |
| 66 | + Partner(a.first(), b.first()) |
| 67 | + } |
| 68 | + |
| 69 | + else -> error("unknown move: $it") |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +class MutableRingList<T>(initialList: List<T>) { |
| 74 | + private val list: MutableList<T> = initialList.toMutableList() |
| 75 | + private var offset = 0 |
| 76 | + |
| 77 | + companion object { |
| 78 | + fun <T> mutableRingListOf(vararg elements: T): MutableRingList<T> = MutableRingList(elements.toList()) |
| 79 | + fun mutableRingListOf(elements: String): MutableRingList<Char> = MutableRingList(elements.toList()) |
| 80 | + } |
| 81 | + |
| 82 | + operator fun get(index: Int) = list[(index + offset) % list.size] |
| 83 | + |
| 84 | + operator fun set(index: Int, value: T) { |
| 85 | + list[(index + offset) % list.size] = value |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Makes [n] elements move from the end to the front, but maintain their order otherwise. |
| 90 | + */ |
| 91 | + fun shiftRight(n: Int): MutableRingList<T> { |
| 92 | + check(n <= list.size) { "the number of items to spin must be smaller than or equal to the list's length" } |
| 93 | + offset = (offset + list.size - n) % list.size |
| 94 | + return this |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Swaps the elements at [indexA] and [indexB]. |
| 99 | + */ |
| 100 | + fun swap(indexA: Int, indexB: Int): MutableRingList<T> { |
| 101 | + val tmp = get(indexA) |
| 102 | + set(indexA, get(indexB)) |
| 103 | + set(indexB, tmp) |
| 104 | + return this |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Swaps the positions of the specified elements [elemA] and [elemB]. |
| 109 | + * If not unique, only their first occurrences are swapped. |
| 110 | + */ |
| 111 | + fun swap(elemA: T, elemB: T): MutableRingList<T> { |
| 112 | + val indexA = list.indexOf(elemA) |
| 113 | + val indexB = list.indexOf(elemB) |
| 114 | + require(indexA >= 0) { "$elemA is not in the list" } |
| 115 | + require(indexB >= 0) { "$elemB is not in the list" } |
| 116 | + list[indexA] = elemB |
| 117 | + list[indexB] = elemA |
| 118 | + return this |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * @return A snapshot of the current state of this [MutableRingList] |
| 123 | + */ |
| 124 | + fun toList(): List<T> = list.subList(offset, list.size) + list.subList(0, offset) |
| 125 | + |
| 126 | + override fun toString(): String = toList().toString() |
| 127 | + |
| 128 | + fun toJoinedString(): String = toList().joinToString("") |
| 129 | +} |
0 commit comments