8 coexisting queens
This is a fixed output challenge. Output a textual representation of a chessboard hosting 8 queens, none of which are attacking each other.
There are 92 ways of arranging them, 12 if rotations and reflections are discounted. You may choose any 1 of these arrangements.
You are not required to calculate or search for a valid arrangement. Hardcoding your choice of arrangement is one possible valid solution.
Input
- There is no input for this challenge
Output
- 8 newline separated lines of 8 characters, with an optional trailing newline
- Each character is either a queen or an empty square. You may choose any two distinct characters to represent a queen and an empty square
- No more than 1 queen appears in each row, column, and diagonal
- There are 8 queens in total
- Your output must be the same each time (your code must have deterministic output, even if it uses a probabilistic approach internally)
Examples
The examples show a queen as Q
and an empty square as #
. There are 12 examples, one for each of the fundamental solutions (before rotation and reflection). Any combination of rotating and reflecting one of these examples is also a valid solution.
###Q####
######Q#
##Q#####
#######Q
#Q######
####Q###
Q#######
#####Q##
####Q###
#Q######
###Q####
######Q#
##Q#####
#######Q
#####Q##
Q#######
###Q####
#Q######
######Q#
##Q#####
#####Q##
#######Q
####Q###
Q#######
###Q####
#####Q##
#######Q
##Q#####
Q#######
######Q#
####Q###
#Q######
##Q#####
#####Q##
#######Q
Q#######
###Q####
######Q#
####Q###
#Q######
####Q###
##Q#####
#######Q
###Q####
######Q#
Q#######
#####Q##
#Q######
####Q###
######Q#
###Q####
Q#######
##Q#####
#######Q
#####Q##
#Q######
###Q####
Q#######
####Q###
#######Q
#####Q##
##Q#####
######Q#
#Q######
##Q#####
#####Q##
###Q####
Q#######
#######Q
####Q###
######Q#
#Q######
#####Q##
#Q######
######Q#
Q#######
###Q####
#######Q
####Q###
##Q#####
###Q####
######Q#
Q#######
#######Q
####Q###
#Q######
#####Q##
##Q#####
#####Q##
###Q####
######Q#
Q#######
#######Q
#Q######
####Q###
##Q#####
The 12 fundamental solutions were taken from the Wikipedia page for the 8 queens puzzle which also provides them as images:
Scoring
Despite there being 92 valid outputs, this is a standard code golf challenge. Your score is the number of bytes in your code.
Explanations in answers are optional, but I'm more likely to upvote answers that have one.
JavaScript, 48 bytes …
2y ago
x86-64 machine code, 27 bytes …
2y ago
Japt `-R`, 16 15 bytes Uses …
2y ago
Vyxal, 9 bytes ``` ×»∇ḂẆ»f꘍§ …
2y ago
J, 20 bytes ``` '01'{#:2^2 …
2y ago
5 answers
Japt -R
, 16 15 bytes
Uses spaces for #
s and "
s for Q
s.
##Ë64ì £QùXÄÃy
Test it (footer reformats the output to use the characters from the spec)
This one uses 1
s for #
s.
##Ë64ì £#ÿ¤hXQ
Test it (footer as above)
##Ë64ì £QùXÄÃy
##Ë64 :15720364
ì :To digit array
£ :Map each X
Q : Quotation mark
ù : Left pad with spaces to length
XÄ : X+1
à :End map
y :Transpose (which pads with spaces)
:Implicit output joined with newlines
##Ë64ì £#ÿ¤hXQ
##Ë64ì £ :As above
#ÿ : 255
¤ : To binary string
hXQ : Replace the character at index X with "
"But, Shaggy, why ... no, how is that 15720364
?"
"Glad you asked! See this tip here"
0 comment threads
JavaScript, 48 bytes
_=>[..."41506372"].map(x=>1e8/9+10**x|0).join`
`
The string "41506372"
encodes the position of the queen in each row.
1e8/9
gives 11111111.1111..., and adding 10x changes a digit from 1 to 2 at position x
.
The bitwise operation is a short way to cause a conversion to integer, removing the fractional part, and finally, join
inserts line feeds in between the numbers.
0 comment threads
x86-64 machine code, 27 bytes
6A D7 58 99 B1 10 48 D3 C0 48 AB C6 07 0A AE 80 C1 48 73 F2 FF C2 74 EC 88 37 C3
Following the standard calling convention for Unix-like systems (from the System V AMD64 ABI), this takes in RDI a memory address at which to place the result, as a null-terminated byte string in ISO-8859-1.
In assembly:
f: push -41; pop rax # Set RAX to -41: low byte 0xD7, other bytes 0xFF.
cdq # Set EDX to -1 by sign-extending EAX.
r4: mov cl, 0b00010000 # Set CL to 2*8.
r: rol rax, cl # Rotate RAX left by the value of CL.
# CL will always be a multiple of 8; this moves the 0xD7 byte.
stosq # Write RAX to the string, advancing the pointer.
mov [rdi], '\n' # Add a line feed to the string.
scasb # Advance past it while comparing it with AL.
add cl, 0b01001000 # Add 1*8+64 to CL.
jnc r # Jump if no carry; iterate 4 times.
inc edx # Add 1 to EDX.
jz r4 # Jump (here resetting CL) if it's 0.
mov [rdi], dh # Add a 0 byte from DH to the string.
ret # Return.
The output is:
ÿÿ×ÿÿÿÿÿ
ÿÿÿÿÿ×ÿÿ
ÿ×ÿÿÿÿÿÿ
ÿÿÿÿÿÿ×ÿ
×ÿÿÿÿÿÿÿ
ÿÿÿ×ÿÿÿÿ
ÿÿÿÿÿÿÿ×
ÿÿÿÿ×ÿÿÿ
This solution was chosen for the pattern formed by the horizontal offsets between queen positions in adjacent rows, wrapping around and starting at 0; those offsets are 2, 3, 4, 5, 2, 3, 4, 5.
0 comment threads
Vyxal, 9 bytes
×»∇ḂẆ»f꘍§
Uses spaces for empty and asterisks for queen. Add a ›
at the end to replace spaces with zeros.
×»∇ḂẆ»f꘍§
»∇ḂẆ»f # 13572064 as a list of digits
× ꘍ # For each, prepend that many spaces to an asterisk
§ # Vertical join: transpose filling with spaces, and join on newlines
0 comment threads
J, 20 bytes
'01'{~#:2^2842 A.i.8
Non-tacit program that outputs implicitly in a REPL. Shoutout to Raul (Miller)#5220 in the APL farm discord for thinking of this cool idea.
2842 A.i.8 : Anagram 2842 of 0..8 -> 0 4 7 5 2 6 1 3
2^ : Raise 2 to each value
#: : Create matrix of each binary conversion
'01'{~ : Index '01' using each value of the result
0 comment threads