Skip to content

Instantly share code, notes, and snippets.

@killroy42
Last active May 30, 2021 15:16
Show Gist options
  • Save killroy42/8994e4d7231f25cb1f7ff0b83811caf3 to your computer and use it in GitHub Desktop.
Save killroy42/8994e4d7231f25cb1f7ff0b83811caf3 to your computer and use it in GitHub Desktop.
The cracking The Cryptic Fan Discord Code Golf Club
/*
Basic rules for length measurements:
- Same function name as original, but reasonably short
- Terminate in ";"
- No "var", "const", "let", etc
- Same input/output
- May make certain assumptions, for example, input is only 0-9, etc
*/
// Encode a classic sudoku from puzzle81 into compact form
// Original (594 chars):
codoku = puzzle => {
const blankEncodes = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwx';
let charCode = puzzle[0].charCodeAt(0), digit = (charCode >= 49 && charCode <= 57) ? puzzle[0] : '0';
let res = '', blanks = 0;
for(let i = 1; i < puzzle.length; i++) {
let charCode = puzzle[i].charCodeAt(0), next = (charCode >= 49 && charCode <= 57) ? puzzle[i] : '0';
if(blanks === 5 || next !== '0') {
res += blankEncodes[Number(digit) + blanks * 10];
digit = next;
blanks = 0;
}
else blanks++;
}
res += blankEncodes[Number(digit) + blanks * 10];
return res;
};
// Golfed (99 chars):
codoku=p=>p.replace(/.0{0,5}/g,d=>String.fromCharCode((d=+d[0]+10*d.length)+(d<20?38:d<46?45:51)));
// Decode a classic sudoku from compact form to puzzle81
// Original (367 chars):
dedoku = zipped => {
const blankEncodes = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwx';
const blanksMap = ['', '0', '00', '000', '0000', '00000'];
let res = [];
for(let i = 0; i < zipped.length; i++) {
let dec = blankEncodes.indexOf(zipped[i]);
res.push(String(Number(dec % 10)), blanksMap[Math.floor(dec / 10)]);
}
return res.join('');
};
// Golfed (93 chars):
dedoku=p=>p.replace(/./g,d=>(d=d.charCodeAt()-(d>'Z'?61:d>'9'?55:48),d%10+'0'.repeat(d/10)));
// find all cell indices visible from given cell index by standard row/column/box rules
// Original (284 chars):
seencells = idx => {
let seen = [], row, col, box;
for(let i = 0; i < 9; i++) {
row = ~~(idx / 9) * 9 + i;
col = (idx % 9) + i * 9;
box = ~~(~~(idx / 9) / 3) * 3 * 9 + ~~((idx % 9) / 3) * 3 + ~~(i / 3) * 9 + i % 3;
seen.push(row, col, box);
}
return [...new Set(seen)];
};
// Golfed (100 chars):
seencells=>(x=p=>1<<p/9|1<<9+p%9|1<<18+~~(p/27)*3+p%9/3,[...Array(81).keys()].filter(q=>x(p)&x(q)));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment