-23 bytes thanks to @Doorknob.
+42 bytes accounting for backtracking.
p[m_]:=StringPartition[#,m]&;l=Range@8;f[n_]:=Check[w=(8#2+#1-8)&@@@({LetterNumber@#,FromDigits@#2}&@@@(p@1/@p[UpTo@2]@n));g=Graph[Sort/@UndirectedEdge@@@Position[Outer[EuclideanDistance@##&,#,#,1],N@Sqrt@2]&@GraphEmbedding@GridGraph@{8,8}//Union]~VertexDelete~w;c:=#~Complement~w&;m=0;Do[m+=Length@FindPath[g,i,j],{i,c@l},{j,c[l+56]}];m==0,0>1]
Try it online!
I rewrote most of this to account for backtracking, I think there may be an easier way to define the graph g
, Mathematica has GraphData[{"bishop",{8,8}}]
which is the graph of all moves a bishop can make on a chessboard (Bishop Graph), but this graph includes connections further than nearest diagonal neighbor. If anyone knows a shorter way to do that let me know. Credit for the graph construction goes to this MathematicaSE answer.
Returns True
for strong passwords, False
for weak/ill-formed passwords. Note that for most of the ill-formed passwords it will produce a bunch of error messages and then return False
. If this is not in line with the rules then they can be suppressed by changing f[n_]:=...
to f[n_]:=Quiet@...
costing 6 bytes.
Ungolfed:
p[m_] := StringPartition[#, m] &;
f[n_] :=
Check[
w = (8 #2 + #1 -
8) & @@@ ({LetterNumber@#, FromDigits@#2} & @@@ (p@1 /@
p[UpTo@2]@n));
r = GridGraph[{8, 8}];
g = Graph[Sort /@ UndirectedEdge @@@
Position[Outer[EuclideanDistance@## &, #, #, 1],N@Sqrt@2] &@
GraphEmbedding@r // Union]~VertexDelete~w;
s = Complement[{1,2,3,4,5,6,7,8},w];
e = Complement[{57,58,59,60,61,62,63,64},w];
m = 0;
Do[m += Length@FindPath[g, i, j], {i, s}, {j, e}];
If[m == 0,True,False]
, False]
Breakdown:
p[m_]:=StringPartition[#,m]&
Takes a string argument and splits it into a list of strings each of length m
.
Check[...,False]
Returns False
if any error messages are produced, which is how we catch the ill-formed strings (i.e. assume they are well-formed, inevitably producing an error down the line).
(8*#2 + #1 - 8) & @@@ ({LetterNumber@#, FromDigits@#2} & @@@ (p@1 /@
p[UpTo@2]@n));
Takes the string of pawn positions and splits it such that "a2h5b"
becomes {{"a","2"},{"h","5"},{"b"}}
, then LetterNumber
will convert the letter to a number (a -> 1
, etc) and FromDigits
converts the numeral into an integer. If the string is not well formed, this step will produce an error which will be caught by Check
, returning False
. These two numbers are then converted to an integer corresponding to a square on the board.
r = GridGraph[{8, 8}];
g = Graph[
Sort /@ UndirectedEdge @@@
Position[Outer[EuclideanDistance@## &, #, #, 1],
N@Sqrt@2] &@GraphEmbedding@r // Union]~VertexDelete~w;
Constructs the graph of all nearest-neighbor diagonal edges with the pawn positions deleted.
s = Complement[{1,2,3,4,5,6,7,8},w];
e = Complement[{57,58,59,60,61,62,63,64},w];
These are lists of unoccupied starting and ending vertices respectively
m=0
Do[m += Length@FindPath[g, i, j], {i, s}, {j, e}];
If[m == 0,True,False]
Loops over the starting and ending vertices, for each pair FindPath
will be a list of paths between them. If there are no paths between them, it will be an empty list, so Length@
returns 0
. If there are no paths at all, then m
will be zero, and we return True
, otherwise return False
.
1What color squares does the bishop go on? – Embodiment of Ignorance – 2019-02-13T21:00:01.393
@EmbodimentofIgnorance
from any square in the first (1) row to any square in the last (8) row.
The bishop tries a path from all squares in the first row. Question: is1a
equal toa1
for being parsed as a chess square? – Veskah – 2019-02-13T21:24:57.110Can the bishop take pawns? Can opposing pawns take the bishop? – Οurous – 2019-02-13T21:46:48.340
2Your 2nd last test case contradicts your spec. You also need to explain how "each pair of characters can be interpreted as a square on a chessboard". – Shaggy – 2019-02-13T21:58:16.450
1a1b2c3d4d5f5f4g3g4h2b5 is not strong against bishops, since a bishop can get to h5, then go down to d1 – Embodiment of Ignorance – 2019-02-13T23:05:08.447
suggested case:
a
andaa
– Luis felipe De jesus Munoz – 2019-02-14T11:44:42.110@EmbodimentofIgnorance: "a1b2c3d4d5f5f4g3g4h2b5 is not strong against bishops..." — No, the direct route from d1 to h5 is blocked by the pawn on g4. (I used chessboardjs.com to view the non-trivial examples.) – Quuxplusone – 2019-02-14T15:23:17.283
2@TRITICIMAGVS, Ourous: I've clarified that both the pawns and the bishop are white, so neither is allowed to capture (or land on, or move through, or jump over) the other. – Quuxplusone – 2019-02-14T15:25:42.150
@Shaggy: When you posted that comment, the "2nd last test case" was
correct horse battery staple
, which is obviously not strong against bishops according to the spec as I read it. Can you perhaps clarify what you meant by "Your 2nd last test case contradicts your spec"? (Assuming it still matters, that is.) – Quuxplusone – 2019-02-14T15:27:19.210@Veskah: The ill-formed/weak example
1a2a3a4a5a6a7a1h2h3h4h5h6h7h
already answers your question as to whether1a
is an acceptable variation ofa1
. (It's not.) – Quuxplusone – 2019-02-14T15:28:33.327Ah, sorry, I misread the spec - I read it as we would be given "a string consisting of alternating letters (in
a-h
) and digits (in1-8
) such that each pair of characters can be interpreted as a square on a chessboard" - i.e, we were guaranteed the input would match the RegEx[a-h][1-8]+
. – Shaggy – 2019-02-14T17:37:27.937This is still in need of an explanation of how each pair of characters corresponds to a square on a chessboard as well as a worked example or 2. – Shaggy – 2019-02-14T17:39:21.960
@Arnauld: Oops! I blame my instinctive zero-indexing. That
– Quuxplusone – 2019-02-14T21:05:49.410g2
should have beeng3
. Fixed now. @Shaggy: I think you'll just have to look at http://chessboardjs.com or the Wikipedia page for chess or something. A full description of how "d4" corresponds to a square, or how a bishop moves, or whether a white piece can capture another white piece, is supposed to be out-of-scope for this specific puzzle.Can the input contain capital letters, and if yes, do
[A-H]
also count? Or can we assume the input always consist of[a-z0-9 ]
(based on the test cases given)? – Kevin Cruijssen – 2019-02-15T07:51:36.6631Also, could you add an example for one of the truthy test cases. Because I understand that the squares of the password are filled with white pawns, but I don't understand where the white bishop is placed. And if any place is fine, why can't it travel to each row
1
through8
in the first test case? It cannot travel to each column, since thea
column is completely filled with pawns, but it can travel to each row without a problem, can it not? I have the feeling I'm missing something.. :S – Kevin Cruijssen – 2019-02-15T08:43:48.2271@KevinCruijssen: Yikes. For
a1a2a3a4a5a6a7a8
, reada1b1c1d1e1f1g1h1
! I don't blame you for being confused, now that I see that! Does that clarify? Also I'll look into adding some inline chess grids. As for[A-H]
, I'd say strictly speaking the puzzle says not to count them as letters. – Quuxplusone – 2019-02-16T14:47:42.2901Suggested test case
b1d1f1h1g5d6f6e3d2b2c5 => false
. It seems most answers fail, since the bishop is forced to go backwards to make it to the end – Jo King – 2019-02-18T05:47:04.090@Quuxplusone Ah, now it indeed makes a lot more sense. xD – Kevin Cruijssen – 2019-02-18T13:07:15.360