Advertisement
Guest User

Untitled

a guest
Dec 16th, 2020
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 2.55 KB | None | 0 0
  1. package fourteen
  2.  
  3. class DockingData(input: List<String>) {
  4.  
  5.     private val instructions = input.map { Instruction(it) }
  6.  
  7.     class Instruction(input: String) {
  8.         val mask = if (input.contains("mask"))
  9.             input.split(" ").last() else null
  10.         val address = if (input.contains("mem"))
  11.             input.split("[").last().split("]").first().toLong() else null
  12.         val value = if (input.contains("mem"))
  13.             input.split(" ").last().toLong() else null
  14.     }
  15.  
  16.     fun process(part2: Boolean = false): Long {
  17.         val memory: MutableMap<Long, Long> = mutableMapOf()
  18.         var mask = instructions.first().mask!!
  19.  
  20.         instructions.forEach { instruction ->
  21.             if (instruction.mask != null)
  22.                 mask = instruction.mask
  23.             else if (instruction.address != null && instruction.value != null) {
  24.                 if (part2) {
  25.                     applyBitmaskToAddress(mask, instruction.address).forEach {
  26.                         memory[it] = instruction.value
  27.                     }
  28.                 } else
  29.                     memory[instruction.address] = applyBitmaskToValue(mask, instruction.value)
  30.             } else {
  31.                 throw RuntimeException("Instruction not parsable! $instruction")
  32.             }
  33.         }
  34.         return memory.values.sum()
  35.     }
  36.  
  37.     private fun applyBitmaskToValue(mask: String, value: Long): Long {
  38.         val binaryValue = value.toString(2)
  39.         var binaryResult = ""
  40.         mask.forEachIndexed { index, bit ->
  41.             binaryResult += (
  42.                     if (bit == 'X')
  43.                         binaryValue.getOrElse(binaryValue.length - mask.length + index) { '0' }
  44.                     else
  45.                         bit
  46.                     )
  47.         }
  48.         return binaryResult.toLong(2)
  49.     }
  50.  
  51.     private fun applyBitmaskToAddress(mask: String, address: Long): List<Long> {
  52.         val givenBinaryAddress = address.toString(2)
  53.         var binaryAdresses = listOf("")
  54.        
  55.         mask.forEachIndexed { index, bit ->
  56.             binaryAdresses = if (bit == 'X') {
  57.                 binaryAdresses.flatMap {
  58.                     listOf(it + 0, it + 1)
  59.                 }
  60.             } else {
  61.                 binaryAdresses.map {
  62.                     if (bit == '0')
  63.                         it + givenBinaryAddress.getOrElse(givenBinaryAddress.length - mask.length + index) { '0' }
  64.                     else
  65.                         it + bit
  66.                 }
  67.             }
  68.         }
  69.         return binaryAdresses.map { it.toLong(2) }
  70.     }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement