From 2655b628c5065af1f16ffe11d96baae2cdb185d8 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Thu, 6 Sep 2018 19:11:28 +0800 Subject: [PATCH] update calculating of numbers --- .../domain/entities/MutablePsychomatrix.kt | 2 +- .../domain/entities/Psychomatrix.kt | 61 ++++++++++--------- 2 files changed, 33 insertions(+), 30 deletions(-) diff --git a/src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/domain/entities/MutablePsychomatrix.kt b/src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/domain/entities/MutablePsychomatrix.kt index ad5842b..bb418df 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/domain/entities/MutablePsychomatrix.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/domain/entities/MutablePsychomatrix.kt @@ -8,7 +8,7 @@ import kotlinx.coroutines.experimental.async import org.joda.time.DateTime class MutablePsychomatrix(date: DateTime) : Psychomatrix(date) { - private var mutableNumbers = super.numbers.toMutableList() + private var mutableNumbers = calculateNumbers(date).toMutableList() override val numbers: ByteArray get() = mutableNumbers.toByteArray() diff --git a/src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/domain/entities/Psychomatrix.kt b/src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/domain/entities/Psychomatrix.kt index 7a3b96d..f88c2ad 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/domain/entities/Psychomatrix.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/PsychomatrixBase/domain/entities/Psychomatrix.kt @@ -23,8 +23,39 @@ val THIRD_COLUMN_SUM_MATRIX_INDEX = 2 to 4 val EMPTY_CEILS_OF_MATRIX = (0 until 3).map { it to 0 } +fun calculateNumbers(dateTime: DateTime): ByteArray { + val dateDigits = dateFormat.print(dateTime).map { "$it".toByte() }.toMutableList() + + val firstNumber = dateDigits.sum() + val firstNumberDigits = firstNumber.toDigits() + + val secondNumber = firstNumberDigits.sum() + val secondNumberDigits = secondNumber.toDigits() + + val thirdNumber = firstNumber - dateDigits[0] * 2 + val thirdNumberDigits = thirdNumber.toDigits() + + val fourthNumber = thirdNumberDigits.sum() + val fourthNumberDigits = fourthNumber.toDigits() + + dateDigits.addAll(firstNumberDigits) + dateDigits.addAll(secondNumberDigits) + dateDigits.addAll(thirdNumberDigits) + dateDigits.addAll(fourthNumberDigits) + + return ByteArray(10).also { + numbers -> + (0 until numbers.size).forEach { + index -> + numbers[index] = dateDigits.count { + it == index.toByte() + }.toByte() + } + } +} + open class Psychomatrix(val date: DateTime) { - protected open val numbers: ByteArray = ByteArray(10) + protected open val numbers: ByteArray = calculateNumbers(date) /** * Always array 4*5 of values. In rows was put columns @@ -72,34 +103,6 @@ open class Psychomatrix(val date: DateTime) { }.toTypedArray() }.toTypedArray() - init { - val dateDigits = dateFormat.print(date).map { "$it".toByte() }.toMutableList() - - val firstNumber = dateDigits.sum() - val firstNumberDigits = firstNumber.toDigits() - - val secondNumber = firstNumberDigits.sum() - val secondNumberDigits = secondNumber.toDigits() - - val thirdNumber = firstNumber - dateDigits[0] * 2 - val thirdNumberDigits = thirdNumber.toDigits() - - val fourthNumber = thirdNumberDigits.sum() - val fourthNumberDigits = fourthNumber.toDigits() - - dateDigits.addAll(firstNumberDigits) - dateDigits.addAll(secondNumberDigits) - dateDigits.addAll(thirdNumberDigits) - dateDigits.addAll(fourthNumberDigits) - - (0 until numbers.size).forEach { - index -> - numbers[index] = dateDigits.count { - it == index.toByte() - }.toByte() - } - } - /** * @return count of numbers of `i` */