Ẏċ⁴¬
HĊ,$Ḟµ€1¦€F€;⁸Ḣ,$€
‘1¦€ṭ€⁹’¤
+1¦€⁹ṭ€0;ç
⁹Ȧ‘Ḥ¤ŀ
Ṫ;0ṙJ$çḢ
Wṭ0WÇ€Ẏ$ÑпL’
一个完整的程序,其中第一个输入(第三个参数)是当前堆栈,第二个输入(第四个参数)是所需的游标。
在线尝试!由于实施,这导致25, 17
测试用例的TIO超时达到60秒。这可以通过以下方法来解决:使用此84字节消除多余的打高尔夫球性(它过滤掉零尺寸的堆栈并对ḟ€Ṣ¥0¦€0
链接6末尾剩余的堆栈进行排序,并且仅Q$
在主菜单中使用时保持每个步骤的唯一状态)链接)。
怎么样?
该程序实现定义的状态机。
它创建原始状态,[0, [argument 1]]
然后重复执行所有可能的下一个状态,
直到找到一个匹配的状态[argument 2, [...]]
。
注意:程序条目位于最下方的“主链接”(Wṭ0WÇ€Ẏ$ÑпL’
)
Ẏċ⁴¬ - Link 1, test a list of states for not having the desired cursor
Ẏ - tighten by one
⁴ - program's fourth argument (second input) - desired cursor
ċ - count occurrences (the stack list will never match, so just inspecting the cursors)
¬ - logical negation
HĊ,$Ḟµ€1¦€F€;⁸Ḣ,$€ - Link 2, next states given a 0 cursor: list, rotatedStacks; number currentCursor (unused)
µ€1¦€ - for each rotation of rotatedStacks apply to the first element:
H - halve
$ - last two links as a monad
Ċ - ceiling
, - pair
Ḟ - floor (vectorises) -- i.e. n -> [floor(ceil(n/2)),floor(n/2)]
= [ceil(n/2),floor(n/2)]
F€ - flatten each -- i.e. each [[c1,f1],s2, s3,...] -> [c1,f1,s2,s3,...]
⁸ - chain's left argument, rotatedStacks
; - concatenate -- i.e. [[c1,f1,s2,s3,...],[c2,f2,s3,...,s1],...,[s1,s2,s3,...],[s2,s3,...,s1],...]
$€ - last two links as a monad for each:
Ḣ - head
, - pair -- i.e. [c1,f1,s2,s3,...] -> [c1,[f1,s2,s3,...]]
‘1¦€ṭ€⁹’¤ - Link 3, next states given a non-0 cursor and a right-click: list, rotatedStacks; number currentCursor
1¦€ - for each rotation of rotatedStacks apply to the first element:
‘ - increment -- i.e. place an item into the first stack of each rotation
¤ - nilad followed by link(s) as a nilad:
⁹ - chain's right argument -- currentCursor
’ - decrement
ṭ€ - tack each -- i.e. [s1-1,s2,s2,...] -> [currentCursor-1,[s1-1,s2,s2,...]]
+1¦€⁹ṭ€0;ç - Link 4, next states given a non-0 cursor: list, rotatedStacks; number currentCursor
1¦€ - for each rotation of rotatedStacks apply to the first element:
⁹ - chain's right argument -- currentCursor
+ - add
ṭ€0 - tack each to zero -- i.e. [s1+currentCursor,s2,s3,...] -> [0,[s1+currentCursor,s2,s3,...]]
ç - call the last link (3) as a dyad -- get the right-click states
; - concatenate
⁹Ȧ‘Ḥ¤ŀ - Link 5, next states: list, rotatedStacks; number currentCursor
ŀ - call link at the given index as a dyad...
¤ - nilad followed by link(s) as a nilad:
⁹ - chain's right argument -- currentCursor
Ȧ - any & all -- for our purposes zero if zero, one if not
‘ - increment
Ḥ - double
- -- i.e. call link 2 if currentCursor is zero else call link 4
Ṫ;0ṙJ$çḢ - Link 6, next states: currentState e.g. [cc, [s1, s2, s3, ...]]
Ṫ - tail -- get the stacks, [s1, s2, s3, ...]
;0 - concatenate a zero - add an empty stack to the options for use
$ - last two links as a monad for each:
J - range(length)
ṙ - rotate left by -- i.e. [[s2,s3,0,...,s1],[s3,0,...,s1,s2],[0,...,s1,s2,s3],[...,s1,s2,s3,0],...[s1,s2,s3,0,...]]
Ḣ - head -- get the currentCursor, cc
ç - call the last link (5) as a dyad
Wṭ0WÇ€Ẏ$ÑпL’ - Main link: initialStack, requiredCursor
W - wrap -- [initialStack]
ṭ0 - tack to zero -- [0, [initialStack]]
W - wrap -- [[0, [initialStack]]]
п - loop while, collecting the results:
Ñ - ...condition: call next link (1) as a monad -- cursor not found
$ - ...do: last two links as a monad:
Ç€ - call the last link (6) as a monad for each
Ẏ - flatten the resulting list by one level
L - length
’ - decremented (the collect while loop keeps the input too)