Skip to content

Commit

Permalink
Use longs for swindlers, allowing even bigger swindlers (#791)
Browse files Browse the repository at this point in the history
  • Loading branch information
gamma-delta authored Nov 18, 2024
2 parents 9f2df70 + 034616f commit 21b16ed
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,18 @@ fun List<Iota>.getPositiveInt(idx: Int, argc: Int = 0): Int {
throw MishapInvalidIota.of(x, if (argc == 0) idx else argc - (idx + 1), "int.positive")
}

fun List<Iota>.getPositiveLong(idx: Int, argc: Int = 0): Long {
val x = this.getOrElse(idx) { throw MishapNotEnoughArgs(idx + 1, this.size) }
if (x is DoubleIota) {
val double = x.double
val rounded = double.roundToLong()
if (abs(double - rounded) <= DoubleIota.TOLERANCE && rounded >= 0) {
return rounded
}
}
throw MishapInvalidIota.of(x, if (argc == 0) idx else argc - (idx + 1), "int.positive")
}

fun List<Iota>.getPositiveIntUnder(idx: Int, max: Int, argc: Int = 0): Int {
val x = this.getOrElse(idx) { throw MishapNotEnoughArgs(idx + 1, this.size) }
if (x is DoubleIota) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import at.petrak.hexcasting.api.casting.eval.CastingEnvironment
import at.petrak.hexcasting.api.casting.eval.OperationResult
import at.petrak.hexcasting.api.casting.eval.vm.CastingImage
import at.petrak.hexcasting.api.casting.eval.vm.SpellContinuation
import at.petrak.hexcasting.api.casting.getPositiveInt
import at.petrak.hexcasting.api.casting.getPositiveLong
import at.petrak.hexcasting.api.casting.mishaps.MishapNotEnoughArgs
import at.petrak.hexcasting.common.lib.hex.HexEvalSounds
import it.unimi.dsi.fastutil.ints.IntArrayList
import it.unimi.dsi.fastutil.longs.LongArrayList

// "lehmer code"
object OpAlwinfyHasAscendedToABeingOfPureMath : Action {
Expand All @@ -18,10 +18,10 @@ object OpAlwinfyHasAscendedToABeingOfPureMath : Action {
if (stack.isEmpty())
throw MishapNotEnoughArgs(1, 0)

val code = stack.getPositiveInt(stack.lastIndex)
val code = stack.getPositiveLong(stack.lastIndex)
stack.removeLast()

val strides = IntArrayList()
val strides = LongArrayList()
for (f in FactorialIter()) {
if (f <= code)
strides.add(f)
Expand All @@ -37,7 +37,7 @@ object OpAlwinfyHasAscendedToABeingOfPureMath : Action {
for (divisor in strides.asReversed()) {
val index = radix / divisor
radix %= divisor
editTarget[0] = swap.removeAt(index)
editTarget[0] = swap.removeAt(index.toInt())
// i hope this isn't O(n)
editTarget = editTarget.subList(1, editTarget.size)
}
Expand All @@ -46,12 +46,12 @@ object OpAlwinfyHasAscendedToABeingOfPureMath : Action {
return OperationResult(image2, listOf(), continuation, HexEvalSounds.NORMAL_EXECUTE)
}

private class FactorialIter : Iterator<Int> {
var acc = 1
var n = 1
private class FactorialIter : Iterator<Long> {
var acc = 1L
var n = 1L
override fun hasNext(): Boolean = true

override fun next(): Int {
override fun next(): Long {
val out = this.acc
this.acc *= this.n
this.n++
Expand Down

0 comments on commit 21b16ed

Please sign in to comment.