The ladybug clock puzzle
Created
January 19, 2026 16:42
-
-
Save gabrieljones/5691e6a6d8f41a229a9bd1b47cfbe0d8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| object LadybugClockPuzzleSim extends App { | |
| import scala.util.Random | |
| def mod(a: Int, b: Int) = (a % b + b) % b | |
| val ns = (0 until 11).toSet | |
| def rd: Int = Random.nextInt(2) * 2 - 1 | |
| val iterMoves = Iterator.continually(rd) | |
| println(iterMoves.take(10).toSeq) | |
| val debug = false | |
| def sim(): Int = { | |
| val iter = Iterator.iterate(11)(n => mod(n + rd,12)) | |
| val nSet = scala.collection.mutable.Set[Int]() | |
| val visits = iter.takeWhile(n => { nSet.add(n); !ns.subsetOf(nSet) } ).toSeq | |
| if (debug) { | |
| println(s"v = $visits") | |
| println(s"last = ${visits.last}") | |
| println(s"size ${visits.size}") | |
| println(s"$nSet, ${nSet.size}") | |
| } | |
| visits.last | |
| } | |
| val runs = 1000000 | |
| val counts = Iterator.continually(sim()).take(runs).toSeq.groupMapReduce(identity)(_ => 1)(_ + _) | |
| println(counts) | |
| val probabilities = counts.map{case(k, v)=> k -> v/runs.toDouble} | |
| println(probabilities) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment