The 3x3 Hexa Prime Square Puzzle
05AB1E, 23 21 bytes
Uses CP-1252 encoding.
œJvy3ôD€SøJìHDps2›*P—
Too slow for TIO.
Explanation
œJ # all permutations of input as strings
v # for each permutation
# EXAMPLE: 2E3815DD5
y3ô # split in pieces of 3
# EXAMPLE: ['2E3','815','DD5']
D # duplicate
# EXAMPLE: ['2E3','815','DD5'], ['2E3','815','DD5']
€SøJ # zip the copy to swap rows and columns
# EXAMPLE: ['2E3','815','DD5'], ['28D','E1D','355']
ì # attach them to the same list
# EXAMPLE: ['2E3','815','DD5','28D','E1D','355']
H # convert from base 16 to base 10
# EXAMPLE: [739, 2069, 3541, 653, 3613, 853]
D # duplicate
# EXAMPLE: [739, 2069, 3541, 653, 3613, 853],[739, 2069, 3541, 653, 3613, 853]
p # check first copy for primality
# EXAMPLE: [739, 2069, 3541, 653, 3613, 853],[1,1,1,1,1,1]
s2› # check that each in second copy is larger than 2
# EXAMPLE: [1,1,1,1,1,1],[1,1,1,1,1,1]
* # pairwise multiplication
# EXAMPLE: [1,1,1,1,1,1]
P # product (1 if all were primes larger than 2, else 0)
# EXAMPLE: 1
— # if 1, print y
# EXAMPLE: 2E3815DD5
Python 2, 212 206 197 194 bytes
Requires input enclosed in quotes, like "123558dde"
from itertools import*
k,p,P=3,4,[]
while k<5e3:P+=[k][:p%k];p*=k;k+=1
print[s for s in map(''.join,permutations(input()))if all(int(s[3*i:][:3],16)in P and int(s[i::3],16)in P for i in(0,1,2))]
Saving 9 and 3 bytes thanks to Jonathan Allan
Found new prime filter from xnor (modified the square away, since we dont want 2 as prime here), old prime filter is from Bob
Jelly, 34 30 bytes
i@€ØH’
s3µ;ZÇ€ḅ⁴µ>2aÆPẠ
Œ!ÇÐfḢ
(I should be able to use an nfind to just fetch the first match, 1#
in place of ÐfḢ
, for less bytes and more speed, but I'm seeing errors when I try. EDIT: wrote some changes to possibly implement this in Jelly.)
Brute force search of all permutations, filtered for the criteria, returning first match.
Way too slow for TtyItOnline. Local output examples:
C:\Jelly\jelly-master>python jelly -fu test.txt "123558DDE"
28DE1D355
C:\Jelly\jelly-master>python jelly -fu test.txt "1155578AB"
11B8A5557
How?
i@€ØH’ - Link 1, convert from hexadecimal string to integer list: string
ØH - yield hexadecimal characters, "0123456789ABCDEF"
i@€ - index of €ach character of s in hex chars
’ - decrement (vectorises) (from 1 based jelly index to place value)
s3µ;ZÇ€ḅ⁴µ>2aÆPẠ - Link 2, check if a flattened square is "all prime": string
s3 - split into threes (rows)
µ - monadic chain separation
; - concatenate with
Z - transpose (columns)
Ç€ - call last link (1) as a monad for €ach string in the list
-> list of integer lists
ḅ⁴ - convert from base 16 (vectorises) -> list of decimals
µ - monadic chain separation
>2 - greater than 2
a - and
ÆP - isPrime? -> list of 1s and 0s
Ạ - all truthy?
Œ!ÇÐfḢ - Main link: string
Œ! - all permutations of the string
Ðf - filter keeping entries that evaluate to truthily for
Ç - last link (3) as a monad
Ḣ - head - return first entry