We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
First full score Python 3 solution for this problem:
According to the leaderboard no other Python3 solution was fast enough for all test cases (test case 3 with 10 boards of size 30x30 IS tough).
After some tests and optimization I finally succeeded with it and now take the liberty of throwing that code at you ;-)
# document globalsN=None# board size B=None# board in 0/1 (is prime?)G=None# grundyRINT=None# rows in B as IntCINT=None# columns in B as Int## Complete the 'squareBoard' function below.## The function is expected to return a STRING.# The function accepts 2D_INTEGER_ARRAY board as parameter.#defsquareBoard(board):analyze(board)return'First'ifG[0][N-1][0][N-1]else'Second'defanalyze(board):globalN,B,G,RINT,CINTN=len(board)primes={2,3,5,7}B=[[int(nnotinprimes)forninrow]forrowinboard]RINT=[int(''.join(str(d)fordinreversed(row)),2)forrowinB]CINT=[int(''.join(str(d)fordinreversed(row)),2)forrowinzip(*B)]G=[[[[0]*Nfori2inrange(N)]fori3inrange(N)]fori4inrange(N)]GT=[[[[0]*Nfori2inrange(N)]fori3inrange(N)]fori4inrange(N)]ifnotany(rintforrintinRINT):G[0][N-1][0][N-1]=0return# fill in grundy numbers for all rectangles [rlo,rhi] x [clo,chi]# grundy is ...# 0 if rectangle is size 1 or doesn't contain any nonprime,# else mex of all (cut into two pieces and xor them)forrsizeinrange(1,N+1):forcsizeinrange(1,N+1):ifrsize==1==csize:continueforrloinrange(N-rsize+1):rhi=rlo+rsize-1forcloinrange(N-csize+1):chi=clo+csize-1# look for nonprimeifrsize<csize:mask=get_mask(clo,chi)ifnotany(RINT[ri]&maskforriinrange(rlo,rhi+1)):continueelse:mask=get_mask(rlo,rhi)ifnotany(CINT[ci]&maskforciinrange(clo,chi+1)):continue# okay, there is some nonprime, so must do cuts ...grundy=[0]*40# ... by row [rlo,rcut] + [rcut+1,rhi]GT_slice=GT[clo][chi]forrcutinrange(rlo,rhi):grundy[GT_slice[rlo][rcut]^GT_slice[rcut+1][rhi]]=1# ... by col [clo,ccut] + [ccut+1,chi]G_slice=G[rlo][rhi]forccutinrange(clo,chi):grundy[G_slice[clo][ccut]^G_slice[ccut+1][chi]]=1mex=grundy.index(0)G_slice[clo][chi]=mexGT_slice[rlo][rhi]=mexdefget_mask(i,j):return(1<<j+1)-(1<<i)
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Digits Square Board
You are viewing a single comment's thread. Return to all comments →
First full score Python 3 solution for this problem:
According to the leaderboard no other Python3 solution was fast enough for all test cases (test case 3 with 10 boards of size 30x30 IS tough).
After some tests and optimization I finally succeeded with it and now take the liberty of throwing that code at you ;-)