85 lines
2.4 KiB
Kotlin
85 lines
2.4 KiB
Kotlin
import java.io.File
|
|
|
|
fun <T> MutableList<T>.shift(num: Int) {
|
|
var left = num;
|
|
while(left > 0) {
|
|
add(0, removeAt(size - 1))
|
|
left--
|
|
}
|
|
}
|
|
|
|
fun <T> MutableList<T>.switch(a: T, b: T) {
|
|
val indexA = indexOf(a)
|
|
val indexB = indexOf(b)
|
|
set(indexB, a)
|
|
set(indexA, b)
|
|
}
|
|
|
|
fun <T> MutableList<T>.switch(a: Int, b: Int){
|
|
val elA = elementAt(a)
|
|
val elB = elementAt(b)
|
|
set(a, elB)
|
|
set(b, elA)
|
|
}
|
|
|
|
fun main(args: Array<String>){
|
|
val inputFile = File("../puzzle_16.txt")
|
|
val inputLines = inputFile.readLines()
|
|
val firstLine = inputLines.first()
|
|
// "s1,x3/4,pe/b".split(",") //
|
|
val instructions = firstLine.split(",")
|
|
|
|
val programs = MutableList('p'-'a' + 1) {
|
|
('a' + it).toString()
|
|
}
|
|
val original = MutableList('p'-'a' + 1) {
|
|
('a' + it).toString()
|
|
}
|
|
val swapMap = mutableMapOf<Int, Int>()
|
|
var count = 0
|
|
instructions.forEach {
|
|
if(it.startsWith("s")) programs.shift(it.substring(1).toInt())
|
|
else {
|
|
val split = it.substring(1).split("/")
|
|
if(it.startsWith("x")) {
|
|
programs.switch(split[0].toInt(), split[1].toInt())
|
|
} else {
|
|
programs.switch(split[0], split[1])
|
|
}
|
|
}
|
|
}
|
|
println(programs.joinToString(""))
|
|
val output = programs.joinToString("")
|
|
while(true){
|
|
instructions.forEach {
|
|
if(it.startsWith("s")) programs.shift(it.substring(1).toInt())
|
|
else {
|
|
val split = it.substring(1).split("/")
|
|
if(it.startsWith("x")) {
|
|
programs.switch(split[0].toInt(), split[1].toInt())
|
|
} else {
|
|
programs.switch(split[0], split[1])
|
|
}
|
|
}
|
|
}
|
|
val newOutput = programs.joinToString("")
|
|
count++
|
|
if(newOutput == "abcdefghijklmnop") break
|
|
}
|
|
println(count)
|
|
println(programs.joinToString(""))
|
|
for(i in 0 until (1000000000 % (count + 1))){
|
|
instructions.forEach {
|
|
if(it.startsWith("s")) original.shift(it.substring(1).toInt())
|
|
else {
|
|
val split = it.substring(1).split("/")
|
|
if(it.startsWith("x")) {
|
|
original.switch(split[0].toInt(), split[1].toInt())
|
|
} else {
|
|
original.switch(split[0], split[1])
|
|
}
|
|
}
|
|
}
|
|
}
|
|
println(original.joinToString(""))
|
|
} |