diff --git a/combinators.nim b/combinators.nim index 903d1a7..cc8a09d 100644 --- a/combinators.nim +++ b/combinators.nim @@ -1,5 +1,5 @@ import algorithm, random, sugar -import fixedseq, game +import faststack, fixedseq, game proc nextPermutation(x: var FixedSeq): bool = diff --git a/faststack.nim b/faststack.nim index e6d5c63..12e2f04 100644 --- a/faststack.nim +++ b/faststack.nim @@ -6,12 +6,12 @@ proc show(i: SomeInteger, bitlength = 16) = type - Color = enum + Color* = enum cRed, cGreen, cBlue, cYellow, cPurple - ColorStack = object + ColorStack* = object data: uint16 - len: uint8 + len*: uint8 const @@ -23,6 +23,7 @@ const 0b0_000_111_111_111_111, 0b0_111_111_111_111_111, ] + allColors* = ColorStack(len: 5, data: 0b0_000_001_010_011_100) template offset(s: ColorStack, idx: Natural): uint8 = @@ -39,7 +40,7 @@ template offset(s: ColorStack, idx: BackwardsIndex): uint8 = (cast[uint8](idx) - 1) * 3 -proc add(s: var ColorStack, c: Color) = +proc add*(s: var ColorStack, c: Color) = # e.g. if stack is 0b0000000000000100: # and color is 0b00000011 # shift: 0b0000000000100000 @@ -49,50 +50,53 @@ proc add(s: var ColorStack, c: Color) = inc s.len -proc high(s: ColorStack): uint8 = s.len - 1 +proc high*(s: ColorStack): uint8 = s.len - 1 -proc low(s: ColorStack): uint8 = 0 # just... always 0, I guess +proc low*(s: ColorStack): uint8 = 0 # just... always 0, I guess -proc `[]`(s: ColorStack, i: uint8 | BackwardsIndex): Color = +proc `[]`*(s: ColorStack, i: uint8 | BackwardsIndex): Color = # shift, then mask everything but the three rightmost bits result = cast[Color]( (s.data shr s.offset(i)) and masks[1] ) -proc `[]=`(s: var ColorStack, i: uint8 | BackwardsIndex, c: Color) = +proc `[]=`*(s: var ColorStack, i: uint8 | BackwardsIndex, c: Color) = let offset = s.offset(i) s.data = (s.data and bitnot(masks[1] shl offset)) or (cast[uint16](c) shl offset) -iterator items(s: ColorStack): Color = +iterator items*(s: ColorStack): Color = # s.len is unsigned so it will wrap around if we do s.len - 1 in that case if s.len != 0: for i in countdown(s.len - 1, 0'u8): yield cast[Color]((s.data shr (i * 3)) and masks[1]) -iterator pairs(s: ColorStack): (int, Color) = - var count = 0 +iterator pairs*(s: ColorStack): (uint8, Color) = + var count = 0'u8 for color in s: yield (count, color) inc count -proc find(s: ColorStack, needle: Color): uint8 = +proc find*(s: ColorStack, needle: Color): uint8 = for i in 0'u8 .. s.high: if s[i] == needle: return i -proc moveSubstack(src, dst: var ColorStack, nToMove: uint8) = +proc moveSubstack*(src, dst: var ColorStack, startIdx: uint8) = # Moves a sub-stack from the top of src to the top of dst # shift the dst stack by the length of the substack to make room + let nToMove = src.len - startIdx let shift = nToMove * 3 dst.data = dst.data shl shift # then we mask the source data to present only the items # being moved, and OR that with the shifted dst data + if src.len > 5: + echo src.len dst.data = dst.data or (src.data and masks[src.len]) dst.len += nToMove # then we shift the source to get rid of the moved items @@ -100,8 +104,9 @@ proc moveSubstack(src, dst: var ColorStack, nToMove: uint8) = src.len -= nToMove -proc moveSubstackPre(src, dst: var ColorStack, nToMove: uint8) = +proc moveSubstackPre*(src, dst: var ColorStack, startIdx: uint8) = # Moves a sub-stack from the top of src to the bottom of dst + let nToMove = src.len - startIdx # shift src stack to position substack above its destination, # get rid of everything to the left of the substack, # and OR that with the existing dst data @@ -112,7 +117,7 @@ proc moveSubstackPre(src, dst: var ColorStack, nToMove: uint8) = src.len -= nToMove -proc swap(s: var ColorStack, i1, i2: uint8) = +proc swap*(s: var ColorStack, i1, i2: uint8) = # Swap the values at two indices in the stack if i1 == i2: return @@ -134,7 +139,7 @@ proc swap(s: var ColorStack, i1, i2: uint8) = s.data = (s.data and mask.bitnot) or swapped -proc shuffle(r: var Rand, s: var ColorStack) = +proc shuffle*(r: var Rand, s: var ColorStack) = # Fisher-Yates shuffle for i in countdown(s.high, 1'u8): let j = cast[uint8](r.rand(i)) @@ -142,7 +147,7 @@ proc shuffle(r: var Rand, s: var ColorStack) = s.swap(i, j) -proc reverse(s: var ColorStack, first, last: uint8) = +proc reverse*(s: var ColorStack, first, last: uint8) = var x = first var y = last while x < y: @@ -151,10 +156,12 @@ proc reverse(s: var ColorStack, first, last: uint8) = dec y -proc asInt(s: ColorStack): uint16 = s.data +iterator asInt*(s: ColorStack): int8 = + for i in 0'u8 .. s.high: + yield int8(s[i]) # now we do have to convert -proc `$`(s: ColorStack): string = +proc `$`*(s: ColorStack): string = result = "St@[" for c in s: if result[^1] != '[': @@ -171,27 +178,9 @@ one.add(cYellow) one.add(cPurple) var two: ColorStack -# two.add(cPurple) -two.add(cRed) -# two.add(cYellow) -echo "one: ", one -# echo "two: ", two +one.moveSubstack(two, 2) +two.moveSubstackPre(one, 1) -one.reverse(0, 4) -echo "one: ", one -# one[^2] = cPurple -# echo "one: ", one - -# echo "one 0: ", one[0] -# echo "one 2: ", one[2] -# echo "one 3: ", one[3] - -# echo "one ^1: ", one[^1] -# echo "one ^2: ", one[^2] -# echo "one ^4: ", one[^4] - -# echo "" -# moveSubstack(one, two, 2) -# echo "one: ", one -# echo "two: ", two \ No newline at end of file +echo one, " ", one.len +echo two, " ", two.len diff --git a/game.nim b/game.nim index f6e8d8a..dcb51ad 100644 --- a/game.nim +++ b/game.nim @@ -1,25 +1,26 @@ import hashes, options -import fixedseq +import fixedseq, faststack + +export faststack.Color, faststack.ColorStack, faststack.allColors -type - Color* = enum - cRed, cGreen, cBlue, cYellow, cPurple +# type +# Color* = enum +# cRed, cGreen, cBlue, cYellow, cPurple - ColorStack* = FixedSeq[5, Color, int8] +# ColorStack* = FixedSeq[5, Color, int8] -proc initColorStack*: ColorStack = - result.initFixedSeq +# proc initColorStack*: ColorStack = +# result.initFixedSeq -proc getAllColors: ColorStack = - var i = 0 - for c in Color.low .. Color.high: - result[i] = c +# proc getAllColors: ColorStack = +# var i = 0'u8 +# for c in Color.low .. Color.high: +# result[i] = c -const - allColors* = getAllColors() +const colorNames: array[Color, string] = ["Red", "Green", "Blue", "Yellow", "Purple"] colorAbbrevs: array[Color, char] = ['R', 'G', 'B', 'Y', 'P'] @@ -82,8 +83,8 @@ proc hash*(b: Board): Hash = proc init*(b: var Board) = - for sq in b.squares.mitems: - sq.camels.initFixedSeq + # for sq in b.squares.mitems: + # sq.camels.initFixedSeq b.initialized = true @@ -112,7 +113,7 @@ proc setState*(b: var Board; b.leader = some(leadCamel) -proc diceRemaining*(b: Board): ColorStack = +proc diceRemaining*(b: Board): FixedSeq[5, Color, int8] = result.initFixedSeq for color, isRolled in b.diceRolled: if not isRolled: result.add(color) @@ -144,7 +145,7 @@ proc advance*(b: var Board, die: Die) = if prepend: b[startPos].camels.moveSubstackPre(b[endPos].camels, stackStart) let stackLen = b[startPos].camels.len - stackStart - for i in 0 ..< stackLen: + for i in 0'u8 ..< stackLen: # we know how many camels we added to the bottom, so set the position for each of those b.camels[b[endPos].camels[i]] = endPos else: diff --git a/simulation.nim b/simulation.nim index 96d6fc1..51b78e8 100644 --- a/simulation.nim +++ b/simulation.nim @@ -38,7 +38,7 @@ proc percents*(scores: ScoreSet): WinPercents = # ====================== iterator legEndStates(b: Board): Board = - var diceRemaining: ColorStack + var diceRemaining: FixedSeq[5, Color, int8] diceRemaining.initFixedSeq for i, c in b.diceRolled: if not c: diceRemaining.add(i) diff --git a/test.nim b/test.nim index b317c6c..cceb761 100644 --- a/test.nim +++ b/test.nim @@ -94,7 +94,7 @@ proc testSpread(nTests, nSamples: Natural) = when isMainModule: - games(100, 1_000_000).summarize() + games(10, 1_000_000).summarize() # let start_states = 2_000 # let executionTime = testLegs(start_states) # echo "Execution time: ", executionTime diff --git a/ui.nim b/ui.nim index 5575d59..a1b044e 100644 --- a/ui.nim +++ b/ui.nim @@ -1,5 +1,5 @@ import os, math, strutils, strformat -import fixedseq, game, simulation +import faststack, fixedseq, game, simulation const help =