Created
September 8, 2023 13:34
-
-
Save MartelliEnrico/630eda9931a1f55c01d387456e11ff07 to your computer and use it in GitHub Desktop.
This file contains 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
import java.time.LocalTime | |
import java.time.temporal.ChronoUnit | |
fun main() { | |
val time = LocalTime.now().truncatedTo(ChronoUnit.SECONDS) | |
val clock = time.toBerlinClock() | |
println("$time => $clock") | |
} | |
const val Off = "O" | |
const val Red = "R" | |
const val Yellow = "Y" | |
fun LocalTime.toBerlinClock(): String { | |
val seconds = if (second % 2 == 0) Yellow else Off | |
val hours = (hour / 5 to hour % 5).let { (first, second) -> | |
Array(4) { if (it < first) Red else Off } + Array(4) { if (it < second) Red else Off } | |
} | |
val minutes = (minute / 5 to minute % 5).let { (first, second) -> | |
Array(11) { if (it < first) if (it % 4 == 3) Red else Yellow else Off } + Array(4) { if (it < second) Yellow else Off } | |
} | |
return (listOf(seconds) + hours + minutes).joinToString(separator = "") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment