-2 since we are no longer required to handle an empty list
-1 switching from join, j@
, to concatenate, ;
(the missed item does not need to be encountered in the middle for the method employed, being at the start of the trio is fine)
-2 switching from P¬aSH
to oSH
(OK to have two results since we flatten, half of 1
is 0.5
which is filtered out anyway, and having multiple equal results has no affect on the method employed either)
-1 Thanks to Mr. Xcoder (0-indexed input is allowed)
d3ZIỊoSH;µƝFf9Ḷ¤Q⁼
A monadic link taking a list of integers in [0,8]
and returning a truthy value (1
) if legal and a falsey value (0
) if not.
Try it online! or see a test-suite.
How?
Looks at each adjacent pair of 0-indexed nodes in the input list. If the integer division by three of the two differs by 2 they are on the top and bottom rows, if the modulo by three of the two differs by 2 they are in the left and right columns. The sum of such pairs divided by two is either the 0-indexed mid-node of a three-node-line or a non-integer value -- so these values are first inserted in-front of the 0-indexed pair and then any bogus nodes (like 0.5
or 3.5
) are removed, the resulting list of lists is flattened and then de-duplicated (to yield order-preserved, unique entries) and finally compared to the input - for a legal swipe all of this will end up being a no-op while illegal ones will add missing mid-nodes and/or remove duplicate nodes (note that no special casing is required for an input list of length 1 since it has no adjacent pairs):
d3ZIỊoSH;µƝFf9Ḷ¤Q⁼ - left input is a list of integers e.g. [3,4,7,1,2,8,3]
µƝ - perform the chain to the left for adjacent pairs:
- e.g. for [a,b] in: [3,4] [4,7] [7,1] [1,2] [2,8] [8,3]
d3 - divmod by 3 [[1,0],[1,1]] [[1,1],[2,1]] [[2,1],[0,1]] [[0,1],[0,2]] [[0,2],[2,2]] [[2,2],[1,0]]
Z - transpose [[1,1],[0,1]] [[1,2],[1,1]] [[2,0],[1,1]] [[0,0],[1,2]] [[0,2],[2,2]] [[2,1],[2,0]]
I - differences [0,1] [1,0] [-2,0] [0,1] [2,0] [-1,-2]
Ị - abs(v)<=1 [1,1] [1,1] [0,1] [1,1] [0,1] [1,0]
S - sum (of [a,b]) 7 11 8 3 10 11
o - OR (vectorises) [1,1] [1,1] [8,1] [1,1] [10,1] [1,11]
H - halve (vectorises) [0.5,0.5] [0.5,0.5] [4,0.5] [0.5,0.5] [5,0.5] [0.5,5.5]
; - concatenate [0.5,0.5,3,4] [0.5,0.5,4,7] [4,0.5,7,1] [0.5,0.5,1,2] [5,0.5,2,8] [0.5,5.5,8,3]
F - flatten [0.5,0.5,3,4, 0.5,0.5,4,7, 4,0.5,7,1, 0.5,0.5,1,2, 5,0.5,2,8, 0.5,5.5,8,3]
¤ - nilad followed by link(s) as a nilad:
9 - literal nine
Ḷ - lowered range = [0,1,2,3,4,5,6,7,8]
f - filter keep [ 3,4, 4,7, 4, 7,1, 1,2, 5, 2,8, ,8,3]
Q - deduplicate [3,4,7,1,2,5,8]
⁼ - equal to the input? e.g. 0 (here because 5 was introduced AND because 3 was removed from the right)
Previous method
Jelly, 36 35 bytes
9s3;Z$;“Æ7a‘DZ¤;U$;©0m€2iị®oµƝFQ⁼ȧȦ
Try it online! or see a test-suite.
How?
Similar to the above but constructs all three-node-line possibilities and performs look-up (rather than checking as it goes using divmod to test and halving the sum for the mid-node).
Firstly the construction of the list of three-node-lines:
9s3;Z$;“Æ7a‘DZ¤;U$;©0
9s3 - nine (implicit range) split into threes = [[1,2,3],[4,5,6],[7,8,9]]
$ - last two links as a monad:
Z - transpose = [[1,4,7],[2,5,8],[6,7,9]]
; - concatenate = [[1,2,3],[4,5,6],[7,8,9],[1,4,7],[2,5,8],[3,6,9]]
¤ - nilad followed by link(s) as a nilad:
“Æ7a‘ - code-page index list = [13,55,97]
D - decimal (vectorises) = [[1,3],[5,5],[9,7]]
Z - transpose = [[1,5,9],[3,5,7]]
; - concatenate = [[1,2,3],[4,5,6],[7,8,9],[1,4,7],[2,5,8],[3,6,9],[1,5,9],[3,5,7]]
$ - last two links as a monad:
U - upend = [[3,2,1],[6,5,4],[9,8,7],[7,4,1],[8,5,2],[9,6,3],[9,5,1],[7,5,3]]
; - concatenate = [[1,2,3],[4,5,6],[7,8,9],[1,4,7],[2,5,8],[3,6,9],[1,5,9],[3,5,7],[3,2,1],[6,5,4],[9,8,7],[7,4,1],[8,5,2],[9,6,3],[9,5,1],[7,5,3]]
0 - literal zero (to cater for non-matches in the main link since ị, index into, is 1-based and modular the 0th index is the rightmost)
; - concatenate = [[1,2,3],[4,5,6],[7,8,9],[1,4,7],[2,5,8],[3,6,9],[1,5,9],[3,5,7],[3,2,1],[6,5,4],[9,8,7],[7,4,1],[8,5,2],[9,6,3],[9,5,1],[7,5,3],0]
© - copy the result to the register
Now the decision making:
...m€2iị®oµƝFQ⁼ȧȦ - left input is a list of integers e.g. [4,5,8,2,3,9,4]
µƝ - perform the chain to the left for adjacent pairs:
- i.e. for [a,b] in [[4,5],[5,8],[8,2],[2,3],[3,9],[9,4]]
... - perform the code described above = [[1,2,3],[4,5,6],[7,8,9],[1,4,7],[2,5,8],[3,6,9],[1,5,9],[3,5,7],[3,2,1],[6,5,4],[9,8,7],[7,4,1],[8,5,2],[9,6,3],[9,5,1],[7,5,3],0]
m€2 - modulo-2 slice €ach = [[1,3],[4,6],[3,9],[1,7],[2,8],[6,9],[1,9],[3,7],[3,1],[6,4],[9,7],[7,1],[8,2],[9,3],[9,1],[7,3],[0]]
i - index of [a,b] in that (or 0 if not there) e.g. [0,0,13,0,6,0]
® - recall from register = [[1,2,3],[4,5,6],[7,8,9],[1,4,7],[2,5,8],[3,6,9],[1,5,9],[3,5,7],[3,2,1],[6,5,4],[9,8,7],[7,4,1],[8,5,2],[9,6,3],[9,5,1],[7,5,3],0]
ị - index into (1-based & modular) e.g. [0,0,[8,5,2],0,[3,6,9],0]
o - OR [a,b] e.g. [[4,5],[5,8],[8,5,2],[2,3],[3,6,9],[9,4]]
F - flatten e.g. [4,5,5,8,8,5,2,2,3,3,6,9,9,4]
Q - deduplicate e.g. [4,5,8,2,3,6,9]
⁼ - equal to the input? e.g. 0 (here because 6 was introduced AND because 4 was removed from the right)
Ȧ - any and all? (0 if input is empty [or contains a falsey value when flattened - no such input], 1 otherwise)
ȧ - AND (to force an empty input to evaluate as 1 AND 0 = 0)
2Related. – Martin Ender – 2018-02-12T09:00:09.823
2Related. – 0 ' – 2018-02-12T09:15:27.207
Is the input list guaranteed to be nonempty? – Zgarb – 2018-02-13T20:55:40.373
@Zgarb yes. It'll be nonempty. – stanri – 2018-02-14T05:42:30.683
Related maths question: https://math.stackexchange.com/questions/205049/what-are-my-chances-in-a-most-excellent-adventure
– Pureferret – 2018-02-15T11:40:31.430