Advertisement
Guest User

AoC2019-day7-kotlin

a guest
Dec 9th, 2019
404
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 3.34 KB | None | 0 0
  1. import java.io.File
  2.  
  3. import kotlinx.coroutines.*
  4. import kotlinx.coroutines.channels.*
  5.  
  6. fun <T> permute(input: List<T>): List<List<T>> {
  7.     if (input.size == 1) return listOf(input)
  8.     val perms = mutableListOf<List<T>>()
  9.     val toInsert = input[0]
  10.     for (perm in permute(input.drop(1))) {
  11.         for (i in 0..perm.size) {
  12.             val newPerm = perm.toMutableList()
  13.             newPerm.add(i, toInsert)
  14.             perms.add(newPerm)
  15.         }
  16.     }
  17.     return perms
  18. }
  19.  
  20. suspend fun compute(input: Channel<Int>?, output: Channel<Int>?, done: Channel<Int>?) {
  21.     while(true) {
  22.         val code = ArrayList<Int>()
  23.         File("input.txt").readLines().joinToString().split(",").forEach { code.add(it.toInt()) }
  24.         for (i in 0..4) code.add(0)
  25.  
  26.         fun get(mode: String,pos: Int) =
  27.             if (mode=="1") code.get(pos) else code.get(code.get(pos))
  28.  
  29.         var pc = 0
  30.         while (pc <= code.size ) {
  31.             var instr = (code.get(pc)+100000).toString()
  32.             var op= instr.substring(4,6)
  33.             var c = instr.substring(3,4)
  34.             var b = instr.substring(2,3)
  35.             //var a = instr.substring(1,2)
  36.             //println("OP=${instr}:${a}:${b}:${c}:${op}")
  37.             when(op) {
  38.                 "01" -> { code.set(code.get(pc+3), get(c,pc+1)+get(b,pc+2)); pc+=4; } // add
  39.                 "02" -> { code.set(code.get(pc+3), get(c,pc+1)*get(b,pc+2)); pc+=4; } // multiply
  40.                 "03" -> { code.set(code.get(pc+1), input!!.receive()); pc+=2; } // input
  41.                 "04" -> { output!!.send(get(c,pc+1)); pc+=2; } // output
  42.                 "05" -> { if (get(c,pc+1)!=0) pc=get(b,pc+2) else pc+=3; } // jump-if-true
  43.                 "06" -> { if (get(c,pc+1)==0) pc=get(b,pc+2) else pc+=3; } // jump-if-false
  44.                 "07" -> { if (get(c,pc+1)< get(b,pc+2)) code.set(code.get(pc+3),1) else code.set(code.get(pc+3),0); pc+=4; } // less than
  45.                 "08" -> { if (get(c,pc+1)==get(b,pc+2)) code.set(code.get(pc+3),1) else code.set(code.get(pc+3),0); pc+=4; } // equals
  46.             }
  47.             if (op=="99") {
  48.                 if (done!=null) { done!!.send(1); return; } // stop signal
  49.                 break // restart from scratch
  50.             }
  51.         }
  52.     }
  53.     return
  54. }
  55.  
  56. fun exec(sequence: List<Int>): Int {
  57.     var wire: Array<Channel<Int>?> = arrayOfNulls<Channel<Int>>(7)
  58.     wire.forEachIndexed { i, _ ->
  59.         wire[i] = Channel<Int>(10)
  60.     }
  61.     GlobalScope.launch {
  62.         sequence.forEachIndexed { i, phase ->
  63.             wire[i]!!.send(phase)
  64.             if (i==0) wire[i]!!.send(0)
  65.         }
  66.     }
  67.  
  68.     var ans = 0
  69.     sequence.forEachIndexed { i, _ ->
  70.         GlobalScope.launch {
  71.             compute( wire[i], wire[(i+1)%sequence.size],
  72.                      if(i==0) wire[wire.size-1] else null )
  73.         }
  74.     }
  75.     runBlocking {
  76.         var wait = wire[wire.size-1]!!.receive()
  77.         ans = wire[0]!!.receive()
  78.     }
  79.     return ans
  80. }
  81.  
  82. fun run(sequence: List<Int>): Int {
  83.     var max = 0
  84.     permute<Int>(sequence).forEach {
  85.         var thrust = exec(it)
  86.         if (thrust>max) {
  87.             max = thrust
  88.         }
  89.     }
  90.     return max
  91. }
  92.  
  93. fun main() {
  94.     println("Answer part 1: " + run(listOf<Int>(0, 1, 2, 3, 4))); // Answer part 1: 199988
  95.     println("Answer part 2: " + run(listOf<Int>(5, 6, 7, 8, 9))); // Answer part 2: 17519904
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement