Hot answers tagged

56

Python 2, 41 38 bytes print'ldiagrhkt'[int(input(),35)%2::2] 3 bytes thanks to Mego for string interlacing Takes input like "g6". That's light and dark intertwined.


46

GS2, 17 15 bytes de♦dark•light♠5 The source code uses the CP437 encoding. Try it online! Verification $ xxd -r -ps <<< 6465046461726b076c696768740635 > chess.gs2 $ wc -c chess.gs2 15 chess.gs2 $ gs2 chess.gs2 <<< b1 light How it works d Add the code points of the input characters. e Compute the sum's ...


33

Whitespace, 45 bytes Try it online! By the way, here is a proof that the ⌈n²/2⌉ formula is correct. We can always place at least ⌈n²/2⌉ wazirs: just lay them out in a checkerboard pattern! Assuming the top-left tile is white, there are ⌈n²/2⌉ white ...


27

Min: 12 bitsMax: Avg: Had and thought last night that I could possibly make it even smaller. x Colour to play next (0 -> Black, 1-> White) 1 Only King left? 00000 Position of White King (0 -> A1 ... 63 -> H8) 00000 Position of Black King 01 00000 11111 WK:A1, BK:H2 (Black to play) 11 00000 11111 WK:A1, BK:H2 (White to play) The result ...


26

Hexagony, 34 32 bytes ,},";h;g;;d/;k;-'2{=%<i;\@;trl;a Unfolded and with annotated execution paths: Diagram generated with Timwi's amazing HexagonyColorer. The purple path is the initial path which reads two characters, computes their difference and takes it modulo 2. The < then acts as a branch, where the dark grey path (result 1) prints dark and ...


25

Python 2, 35 bytes lambda x,y:50/(8+x*x/7-x+y*y/7-y)-4 Try it online! Python 2, 39 bytes lambda x,y:50/(8-x*(7-x)/5-y*(7-y)/5)-4 Try it online! Takes inputs 0-indexed. The expression x*(7-x)/5 takes the coordinate values 0..7 to [0, 1, 2, 2, 2, 2, 1, 0] (min(x,7-x,2) does the same, but is longer.) Summing this for x and y gives the right pattern but ...


21

CJam, 18 bytes r:-)"lightdark"5/= Online demo Dissection r e# Read a token of input :- e# Fold -, giving the difference between the two codepoints ) e# Increment, changing the parity so that a1 is odd "lightdark"5/ e# Split the string to get an array ["light" "dark"] = e# Index with wrapping, so ...


21

JavaScript (Node.js),  191 ... 166  164 bytes Saved 2 bytes thanks to @grimy. Returns the \$N\$th term. n=>(g=(x,y)=>n--?g(Buffer('QPNP1O?O@242Q3C3').map(m=c=>g[i=4*((x+=c%6-2)*x>(y+=c%7-2)*y?x:y)**2,i-=(x>y||-1)*(i**.5+x+y)]|i>m||(H=x,V=y,m=i))&&H,V,g[m]=1):m+1)(1,2) Try it online! or See a formatted version How? ...


19

JavaScript (Node.js), 144 138 125 74 73 70 bytes f=(x,n=2,c=0)=>x%n?x-!c?f(x,n+1)/(n%4>2?n/=~c&1:n%4)**c:1:f(x/n,n,c+1) Try it online! -4 byte thanks @Arnauld! Original approach, 125 bytes a=>(F=(x,n=2)=>n*n>x?[x,0]:x%n?F(x,n+1):[n,...F(x/n,n)])(a).map(y=>r-y?(z*=[,1,.5,p%2?0:1/r][r%4]**p,r=y,p=1):p++,z=r=p=1)&&z Try it ...


18

Python 2, 52 bytes f=lambda n:n<6or`n%100`in'18349276167294381'*f(n/10) Checks that any two consecutive digits are in the string '18349276167294381'. To get consecutive digits, rather than doing zip(`n`,`n`[1:]), the function repeatedly checks the last two digits and removes the last digit.


17

192 bits (worst case) Here's a very simple storage scheme that should cope with arbitrary pawn promotions, and never requires more than 64 + 4 × 32 = 192 bits: The first 64 bits store a bitboard that tells where the pieces are (but not what they are). That is, we store one bit for each square of the chessboard (starting at square a1, then b1, c1, ...


17

C, 650 600 n=8,t=65,s,f,x,y,p,e,u=10,w=32,z=95;char a[95],b[95]="RNBKQBNR";v(){p=a[s]&z;y=f/u-s/u;x=f-s-y*u;e=x*x+y*y;n=s%u/8|f%u/8|a[s]/w-t/w|a[f]/w==t/w|!(p==75&e<3|p>80&x*y==0|p%5==1&x*x==y*y|p==78&e==5|p==80&x*(z-t)>0&(a[f]-w?e==2:e==1|e==4&s%5==1));if(!n&&p-78)for(e=(f-s)/abs(x*x>y*y?x:y),x=s;(x+=e)-f;...


17

sed, 37 s/[1357aceg]//g /^.$/{clight q} cdark Explanation s/[1357aceg]//g removes all odd-indexed coordinates. The resulting pattern buffer then has length of 1 for "light" or length of 0 or 2 for "dark". /^.$/ matches the 1-length patterns, changes the pattern to "light" and quits. Otherwise the pattern is changed to "dark".


17

Python 2, 53 40 lambda x,y,p,q:y-2<q>=abs(x-p)+q/7+y/8*5 The king has coordinates (x, y) and the pawn (p, q). There are three significant cases: The pawn is on rank 7 and the king on rank 8. To capture the pawn, the king must be on the same file or an adjacent one. Result: q = 7 ⋀ y = 8 → |x - p| ≤ 1 The pawn is on rank 7. To capture the pawn,...


17

MATL, 17 14 13 12 bytes Thanks to @Neil for 1 byte off! 8:HZ^ZP5X^=s Input is 1-based. Try it online! Explanation This computes the Euclidean distance from the input to each of the 64 positions in the chessboard, and finds how many of those values equal the square root of 5. Since the coordinates are integer values, we can be sure that the two ...


16

I'm not complaining about upvotes, but to be fair... my solution here isn't actually all that great. Ugoren's is better, apart from lacking unicode support. Be sure to look at all the answers before voting, if you've come across this question only now!Anyway. Haskell, 893 888 904 952 (without castling) 862 (without pawn double-moves) (You didn't specify ...


16

Jelly, 19 15 14 bytes Doȷ’d3ạ2\P€=2P Try it online! or verify all test cases. How it works Doȷ’d3ạ2\P€=2P Main link. Argument: n (integer) D Convert n to base 10 (digit array). ȷ Yield 1000. o Logical OR. This replaces each 0 with 1000. ’ Decrement each digit. d3 Divmod; replace each ...


16

Oasis, 3 bytes k>v Try it online! square - increment - integer halve


15

Pyth, 22 21 bytes -1 byte by @Sp3000 fn%Chz3%sCMT2sM*<G8S8 Under the function %Chz3, dark hashes to 1, light to 0, and both to 2. If we take the parity of the sum of the ords of a chess square (that is, a1 -> [97, 33] -> (97 + 33)%2 = 0, dark squares go to 0, and light to 1. This allows us to filter by inequality. fn%Chz3%sCMT2sM*<G8S8 ...


15

Mathematica 63 43 bytes With 20 bytes saved thanks to suggestions by Martin Ender! EdgeCount[8~KnightTourGraph~8,#+1+8#2/<->_]& The above finds the number of squares that are 1 hop away from the given cell on the complete knights tour graph. g=KnightTourGraph[8,8,VertexLabels->"Name",Axes->True] displays the complete knight's tour graph,...


14

160 bits worst case After posting my previous answer 22 bytes, I began to wonder if we could get down to 21 bytes. However when I saw Peter Taylor's amazing 166 bytes I thought "Hang on, it looks like five 32-bit words could be possible!" So after quite a lot of thought, I came up with this: 159.91936391 bytes (a pretty tight fit!) This level of ...


14

Pyth, 18 bytes @c2"lightdark"iz35 Interpret the input as a base 35 number, chop lightdark in half, print.


14

Retina, 13 bytes \d $* / ¶ | Try it online! Explanation The first part (note the trailing space): \d $* is to convert a to the specific number of spaces. Retina has a $* feature to repeat. The way it works is: <num>$*<char> if there is no <num>, Retina will assume $& or the matched string, in this case the matched number. The ...


14

Python 2, 105 bytes lambda b:sum(b[i+d::d][:(8,7-i%8,i%8)[d%8%5]].find('1')*int(c)>0for i,c in enumerate(b)for d in[1,7,8,9]) Try it online! Explanation We take the input as a string of 64 characters '0' or '1'. Using step slices, we cast four "lines of sight" from every queen we encounter. For example, when i = 10 and d = 7, marking the ...


13

177 bits worst case This algoritm, while hardly simple, gives a 177 bits worst case (184b=23B in practice), 13b (16b=2B) best case scenario when there's just 2 kings left. Bit Description 1 Turn (0=white 1=black) 2- 7 White king position (2-4=letter, 5-7=number) 8- 13 Black king position (8-10=letter, 11-13=number) 14- 75 Which squares ...


13

ShadyAsFuck, 91 bytes / BrainFuck, 181 bytes My first real BrainFuck program, thank Mego for the help and for pointing me to the algorithm archive. (That means I didn't really do it on my own, but copied some existing algorithms. Still an experience=) NKnmWs3mzhe5aAh=heLLp5uR3WPPPPagPPPPsuYnRsuYgGWRzPPPPlMlk_PPPPPP4LS5uBYR2MkPPPPPPPP_MMMkLG] This is of ...


13

Bash + GNU Utilities, 74 printf %s\\n {a..h}{1..9}|sed -n "`sed '/[db]/a1~2p /t/a2~2p c/9/d'<<<$1`" {a..h}{1..9} is a bash brace expansion that produces all the coordinates for an 8x8 board, plus an extra column 9. This is important because it makes the row length odd which allows the chequerboard effect. The printf simply formats each ...


13

Retina, 58 40 bytes Thanks to Sp3000 for suggesting this idea: M&!`.. O%`. A`16|18|27|29|34|38|49|67 ^$ Try it online! (Slightly modified to run the entire test suite at once.) Prints 1 for truthy and 0 for falsy results. Explanation M&!`.. Find all overlapping matches of .., i.e. all consecutive pairs of digits, and join them with linefeeds. ...


13

MATL, 17 16 bytes t&l[2K0]B2:&ZvZ+ Try it online! (-1 byte thanks to @Luis Mendo.) The main part of the code is creating a matrix \$ K \$ for convolution: $$ \mathbf{K} = \begin{pmatrix}0&1&0&1&0\\1&0&0&0&1\\0&0&0&0&0\\1&0&0&0&1\\0&1&0&1&0\end{pmatrix} $$ (Relative ...


12

Python, 217 212 220 217 213 characters Tied the 213-byte Mathematica solution R=range(8) def f((p,x,y)): for a in R: for b in R: A,B=abs(a-ord(x)+97),abs(b-ord(y)+49);C=max(A,B);r=(A+B==3and C<3,C<2,A*B<1,A==B,0) if(r['NKRBQ'.index(p)],any(r[1:]))[p=='Q']*C:print p+chr(a+97)+chr(b+49) I started off by generating all valid moves but ...


Only top voted, non community-wiki answers of a minimum length are eligible