Jane Street Puzzle Solution
Created on: January 9, 2025
Created on: January 9, 2025
be careful think very hard this is a jane street puzzle
. . . | . . . | . 2 .
. . . | . . . | . . 5
. 2 . | . . . | . . .
--------+-------+--------
. . 0 | . . . | . . .
. . . | . . . | . . .
. . . | 2 . . | . . .
--------+-------+--------
. . . | . 0 . | . . .
. . . | . . 2 | . . .
. . . | . . . | 5 . .
Fill the empty cells in the grid above with digits such that each row, column, and outlined 3-by-3 box contains the same set of nine unique digit (that is, you’ll be using nine of the ten digits (0-9) in completing this grid possibly with a leading zero), and such that the nine 9-digit numbers (possibly with a leading 0) formed by the rows of the grid has the highest-possible GCD over any such grid.
Some of the cells have already been filled in. The answer to this puzzle is the 9-digit number formed by the middle row in the completed grid.
that is, you’ll be using nine of the ten digits (0-9) in completing this grid ↩
possibly with a leading 0 ↩
please please feel free to write code if u want but just do it in cpp use this template and no comments:
#include <bits/stdc++.h>
#define int long long
using namespace std;
signed main() {
cin.tie(0);
ios::sync_with_stdio(false);
///
}
cpp#include <bits/stdc++.h> #define int long long using namespace std; int grid[9][9], usedRow[9], usedCol[9], usedBox[9], bestG=0; int bestSol[9][9]; int boxId(int r, int c){return (r/3)*3 + c/3;} int gcd9(int a,int b){return b?gcd9(b,a%b):a;} int toNum(int r){ int x=0; for(int c=0;c<9;c++){ x=10*x+grid[r][c]; } return x; } void backtrack(int idx){ if(idx==81){ int g=0; for(int r=0;r<9;r++){ int val=toNum(r); g=(r==0?val:gcd9(g,val)); } if(g>bestG){ bestG=g; for(int r=0;r<9;r++){ for(int c=0;c<9;c++){ bestSol[r][c]=grid[r][c]; } } } return; } int r=idx/9,c=idx%9; if(grid[r][c]!=-1){ backtrack(idx+1); return; } for(int d=0; d<9; d++){ int mask=(1<<d); if(!(usedRow[r]&mask) && !(usedCol[c]&mask) && !(usedBox[boxId(r,c)]&mask)){ grid[r][c]=d; usedRow[r]|=mask; usedCol[c]|=mask; usedBox[boxId(r,c)]|=mask; backtrack(idx+1); usedRow[r]^=mask; usedCol[c]^=mask; usedBox[boxId(r,c)]^=mask; grid[r][c]=-1; } } } signed main() { cin.tie(0); ios::sync_with_stdio(false); // Using -1 for empty. The puzzle uses digits {0..8}, so we exclude 9 entirely. // Givens from the puzzle: // Row/Col are 0-indexed here. memset(grid,-1,sizeof(grid)); grid[0][7]=2; grid[1][8]=5; grid[2][1]=2; grid[3][2]=0; grid[5][3]=2; grid[6][4]=0; grid[7][5]=2; grid[8][6]=5; memset(usedRow,0,sizeof(usedRow)); memset(usedCol,0,sizeof(usedCol)); memset(usedBox,0,sizeof(usedBox)); for(int r=0;r<9;r++){ for(int c=0;c<9;c++){ if(grid[r][c]>=0){ int d=grid[r][c]; int mask=(1<<d); usedRow[r]|=mask; usedCol[c]|=mask; usedBox[boxId(r,c)]|=mask; } } } backtrack(0); // Print the middle row (row 4) of the best solution as a 9-digit number for(int c=0;c<9;c++){ cout<<bestSol[4][c]; } cout<<"\n"; }