32
\$\begingroup\$

Almost six years ago, fellow PPCG member steenslag posted the following challenge:

In a standard dice (die) the numbers are arranged so that opposite faces add to seven. Write the shortest possible program in your preferred language which outputs a random throw followed by 9 random tippings. A tipping is a quarter turn of the dice, e.g. if the dice is facing 5, all possible tippings are 1,3,4 and 6.

Example of desired output:

1532131356

So, now that everybody has completely forgotten about it and the winning answer has long since been accepted, we'll be writing a program to validate the die tipping sequences generated by the submitted solutions. (This makes sense. Just pretend it does.)

Challenge

Your program or function is given a sequence such as 1532131356. Validate that each consecutive digit is:

  • Not equal to the previous digit
  • Not equal to 7 minus the previous digit

(You don't have to validate the first digit.)

Rules

  • Your program must return a truthy value if the input is valid and a falsey value otherwise.
  • You can assume that the input consists of only the digits 1-6 and is at least 1 character long. Sequences won't have a fixed length like in steenslag's challenge.
  • You can take the input as a string ("324324"), an array or array-like datastructure ([1,3,5]) or as multiple arguments (yourFunction(1,2,4)).

Standard I/O and loophole rules apply.

Test cases

Truthy

1353531414
3132124215
4142124136
46
4264626313135414154
6
2642156451212623232354621262412315654626212421451351563264123656353126413154124151545145146535351323
5414142

Falsey

  • Repeated digit

    11
    3132124225
    6423126354214136312144245354241324231415135454535141512135141323542451231236354513265426114231536245
    553141454631
    14265411
    
  • Opposing side of die

    16
    42123523545426464236231321
    61362462636351
    62362462636361
    
\$\endgroup\$

38 Answers 38

1
2
1
\$\begingroup\$

Clojure, 55 49 46 bytes

edit 1: using (mapcat ...) instead of (flatten(map ...))

edit 2: using (juxt - +) instead of (juxt = +), checking for values 0 or 7

#(not(some #{0 7}(mapcat(juxt - +)(rest %)%)))

Consecutive values are not allowed to be equal (difference = 0) or sum up to 7. Got to use juxt again :)

\$\endgroup\$
1
\$\begingroup\$

JavaScript (ES6), 46 Bytes

a=>r=1&&a.reduce((c,p)=>r=c==p||c==7-p?0:1)&&r

f=a=>r=1&&a.reduce((c,p)=>r=c==p||c==7-p?0:1)&&r;

console.log(f([4,6]));
console.log(f([1,6]));

\$\endgroup\$
1
\$\begingroup\$

Prolog (SWI), 45 bytes

p([A,B|T]):-A+B=\=7,A\=B,p([B|T]).
p([_|[]]).

Online interpreter

\$\endgroup\$
1
\$\begingroup\$

Math++, 111 bytes

?>b
0>n
n+1>n
b%10>{n}
_(b/1)>b
3+4*!b>$
0>n
n+1>n
10+4*!({n}-{n+1})>$
11+3*!(7-{n}-{n+1})>$
8+4*!{n}>$
1
0>$
0
\$\endgroup\$
1
\$\begingroup\$

Mathematica, 32 bytes

FreeQ[Most@#~BitXor~Rest@#,0|7]&

Unnamed function taking a list of integers as input and outputting True or False. Like many existing answers, it computes the bitwise XOR of consecutive elements of the input, and checks that the answer is never 0 nor 7.

\$\endgroup\$
1
\$\begingroup\$

Bash, 60 bytes

f(){ 
while [ $2 ]
do
(( ($1^$2)%7 ))||return 1
shift
done
}

Takes separate arguments as input.

Explanation

while-do-shift-done loops though the arguments giving each pair a turn as ($1,$2) if the loop completes truth is returned.

The complicated expression (( ($1^$2)%7 )) exclusive-ors the the arguments being tested together and mod(7)s them.

Valid pairs will result in non-zero, invalid ones in zero.

((...)) yields true when the arithmetic returns non-zero.

|| runs the next command if the previous was false (in this context that means if the arithmetic above returned 0).

Unix shell treats zero as true (except as above) so return 1 yields a false result from this function when an invalid pair is found.

\$\endgroup\$
1
\$\begingroup\$

PHP, 41 bytes

<?=!similar_text(aa^af,$s^s.$s=$argv[1]);

Takes input as a string. Prints 1 if the input is valid, an empty string otherwise.

Calculates XOR by pairs of adjacent characters, using shifted version of the input. Then searches for 0x00 or 0x07 characters in it.

\$\endgroup\$
1
\$\begingroup\$

SmileBASIC, 65 bytes

DEF T A
WHILE LEN(A)V=POP(A)IF!(O-V&&7-O-V)THEN?0A
O=V
WEND?1
END

This can definitely be made a little smaller

\$\endgroup\$
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.