Advertisement
Guest User

Untitled

a guest
Dec 20th, 2023
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 4.10 KB | Source Code | 0 0
  1. class BroadCaster(configuration: List<String>) : Module(configuration, mutableMapOf(), "broadcaster", "") {
  2.     fun hitTheButton(times: Int): Long {
  3.         var countHigh = 0L
  4.         var countLow = 0L
  5.         repeat(times) {
  6.             //println("Pulse ${it + 1}")
  7.             var events = sendPulse(false)
  8.             while (events.isNotEmpty()) {
  9.                 val highs = events.count(Event::pulse)
  10.                 countHigh += highs
  11.                 countLow += events.size - highs
  12.                 events = events.flatMap { event ->
  13.                     //println(event)
  14.                     event.target.receivePulse(event.pulse, event.source)
  15.                 }
  16.             }
  17.             //println()
  18.         }
  19.         return countHigh * (countLow + times)
  20.     }
  21.  
  22.     fun hitTheButtonUntilRx(): Long {
  23.         var countPresses = 0L
  24.         var rxLowPulse = false
  25.         while (!rxLowPulse) {
  26.             countPresses++
  27.             //println("Pulse $countPresses")
  28.             var events = sendPulse(false)
  29.             while (events.isNotEmpty()) {
  30.                 rxLowPulse = events.any { event -> !event.pulse && event.target.name == "rx" }
  31.                 events = events.flatMap { event ->
  32.                     if (event.target.name == "kl" && event.pulse) {
  33.                         println("Button press $countPresses")
  34.                         println(event)
  35.                     }
  36.                     event.target.receivePulse(event.pulse, event.source)
  37.                 }
  38.             }
  39.             //println()
  40.         }
  41.         return countPresses
  42.     }
  43. }
  44.  
  45. class FlipFlop(configuration: List<String>, modules: MutableMap<String, Module>, name: String) :
  46.     Module(configuration, modules, name, "%") {
  47.     var on = false
  48.  
  49.     override fun receivePulse(pulse: Boolean, name: String): List<Event> {
  50.         return if (!pulse) {
  51.             on = !on
  52.             sendPulse(on)
  53.         } else {
  54.             emptyList()
  55.         }
  56.  
  57.     }
  58. }
  59.  
  60. class Conjunction(configuration: List<String>, modules: MutableMap<String, Module>, name: String) :
  61.     Module(configuration, modules, name, "&") {
  62.     var state: MutableMap<String, Boolean>? = null
  63.  
  64.     override fun connect(name: String) {
  65.         if (state == null) state = mutableMapOf()
  66.         state!![name] = false
  67.     }
  68.  
  69.     override fun receivePulse(pulse: Boolean, name: String): List<Event> {
  70.         state!![name] = pulse
  71.         return sendPulse(!state!!.all { it.value })
  72.     }
  73. }
  74.  
  75. class TheOminousRxMachine(name:String) : Module(emptyList(), mutableMapOf(), name, "Merry X-mas everybody!")
  76.  
  77. abstract class Module(
  78.     configuration: List<String>,
  79.     modules: MutableMap<String, Module>,
  80.     val name: String,
  81.     typePrefix: String
  82. ) {
  83.     companion object {
  84.         fun createModule(
  85.             configuration: List<String>,
  86.             modules: MutableMap<String, Module>,
  87.             name: String
  88.         ) = configuration.find { it.contains("$name -> ") }?.let { configLine ->
  89.             if (configLine.startsWith("%")) FlipFlop(configuration, modules, name)
  90.             else Conjunction(configuration, modules, name)
  91.         } ?: TheOminousRxMachine(name)
  92.     }
  93.  
  94.     init {
  95.         modules[name] = this
  96.     }
  97.  
  98.     private val connectedModules = configuration.find { it.startsWith("$typePrefix$name -> ") }?.let { configLine ->
  99.         val moduleNames = configLine.removePrefix("$typePrefix$name -> ").split(", ")
  100.         moduleNames.map { moduleName ->
  101.             //println("$name -> $moduleName")
  102.             val module = modules[moduleName] ?: createModule(configuration, modules, moduleName)
  103.             module.connect(name)
  104.             module
  105.         }
  106.     } ?: emptyList()
  107.  
  108.     fun sendPulse(pulse: Boolean): List<Event> {
  109.         return connectedModules.map {
  110.             Event(name, it, pulse)
  111.         }
  112.     }
  113.  
  114.     open fun connect(name: String) {}
  115.  
  116.     open fun receivePulse(pulse: Boolean, name: String): List<Event> = emptyList()
  117. }
  118.  
  119. data class Event(val source: String, val target: Module, val pulse: Boolean) {
  120.     override fun toString() = "$source -> ${if (pulse) "HIGH" else "LOW" } -> ${target.name}"
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement