Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use longs for swindlers, allowing even bigger swindlers #791

Merged
merged 1 commit into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading