Biorhythms: Difference between revisions

Content added Content deleted
(Add Scala implementation)
(Add Kotlin implementation)
Line 1,535: Line 1,535:
Mental day 5: 81.5% (up and rising, next peak 1863-11-22)
Mental day 5: 81.5% (up and rising, next peak 1863-11-22)
</pre>
</pre>


=={{header|Kotlin}}==
{{trans|Java}}
<syntaxhighlight lang="Kotlin">
import java.time.LocalDate
import java.time.format.DateTimeFormatter
import java.time.temporal.ChronoUnit
import kotlin.math.roundToInt
import kotlin.math.sin

fun main() {
val datePairs = listOf(
listOf("1943-03-09", "1972-07-11"),
listOf("1809-01-12", "1863-11-19"),
listOf("1809-02-12", "1863-11-19")
)

for (datePair in datePairs) {
calculateBiorhythms(datePair)
}
}

fun calculateBiorhythms(datePair: List<String>) {
val formatter = DateTimeFormatter.ISO_LOCAL_DATE
val birthDate = LocalDate.parse(datePair[0], formatter)
val targetDate = LocalDate.parse(datePair[1], formatter)
val daysBetween = ChronoUnit.DAYS.between(birthDate, targetDate).toInt()
println("Birth date $birthDate, Target date $targetDate")
println("Days between: $daysBetween")

for (cycle in Cycle.values()) {
val cycleLength = cycle.getLength()
val positionInCycle = daysBetween % cycleLength
val quadrantIndex = 4 * positionInCycle / cycleLength
val percentage = (100 * sin(2 * Math.PI * positionInCycle / cycleLength)).roundToInt()

val description = when {
percentage > 95 -> "peak"
percentage < -95 -> "valley"
Math.abs(percentage) < 5 -> "critical transition"
else -> {
val daysToTransition = (cycleLength * (quadrantIndex + 1) / 4) - positionInCycle
val transitionDate = targetDate.plusDays(daysToTransition.toLong())
val (trend, nextTransition) = cycle.descriptions(quadrantIndex)
"$percentage% ($trend, next $nextTransition $transitionDate)"
}
}

println("${cycle.name} day $positionInCycle: $description")
}
println()
}

enum class Cycle(private val length: Int) {
PHYSICAL(23), EMOTIONAL(28), MENTAL(33);

fun getLength() = length

fun descriptions(index: Int): Pair<String, String> {
val descriptions = listOf(
listOf("up and rising", "peak"),
listOf("up but falling", "transition"),
listOf("down and falling", "valley"),
listOf("down but rising", "transition")
)
return descriptions[index][0] to descriptions[index][1]
}
}
</syntaxhighlight>
{{out}}
<pre>
Birth date 1943-03-09, Target date 1972-07-11
Days between: 10717
PHYSICAL day 22: -27% (down but rising, next transition 1972-07-12)
EMOTIONAL day 21: valley
MENTAL day 25: valley

Birth date 1809-01-12, Target date 1863-11-19
Days between: 20034
PHYSICAL day 1: 27% (up and rising, next peak 1863-11-23)
EMOTIONAL day 14: critical transition
MENTAL day 3: 54% (up and rising, next peak 1863-11-24)

Birth date 1809-02-12, Target date 1863-11-19
Days between: 20003
PHYSICAL day 16: -94% (down and falling, next valley 1863-11-20)
EMOTIONAL day 11: 62% (up but falling, next transition 1863-11-22)
MENTAL day 5: 81% (up and rising, next peak 1863-11-22)


</pre>


=={{header|Locomotive Basic}}==
=={{header|Locomotive Basic}}==