Compare commits

..

10 Commits

Author SHA1 Message Date
Grzegorz Kucmierz
b36804e8c8 Look and Write Sequence 2025-11-15 19:11:53 +01:00
9c6a2c4116 Potion Class 101 2025-08-18 11:12:02 +02:00
68d9990be6 558445a88826e1376b000011 2025-08-08 12:45:49 +02:00
00bb5ad0ec 647518391e258e80eedf6e06 2025-05-23 10:39:57 +02:00
6bf73772e8 558445a88826e1376b000011 2025-05-21 18:43:29 +02:00
09c4879a2a 55491e9e50f2fc92f3000074 2025-05-21 18:20:40 +02:00
4e24a777c1 Guess the (prime) number, digit by digit 2024-12-14 14:16:29 +00:00
7abf5b83af Last digits of N^2 == N 2022-02-01 19:42:36 +01:00
a5a18b917d Assembler interpreter (part II) 2020-05-16 19:15:56 +02:00
0720c8579d Most Frequent Weekdays 2020-05-15 08:40:05 +02:00
11 changed files with 506 additions and 0 deletions

View File

@@ -0,0 +1,22 @@
class Potion {
constructor(color, volume) {
this.color = color;
this.volume = volume;
}
mix(that) {
const volume = this.volume+that.volume;
const color = [];
for (let i = 0; i < this.color.length; ++i) {
const a = this.color[i] * this.volume;
const b = that.color[i] * that.volume;
color[i] = Math.ceil((a + b) / volume);
}
return new Potion(color, volume);
}
}
const p1 = new Potion([255, 255, 255], 7);
const p2 = new Potion([ 51, 102, 51], 12);
p1.mix(p2);

View File

@@ -0,0 +1,92 @@
// Look and Write Sequence
function mazeRunner(maze, directions) {
const [h, w] = [maze.length, maze[0].length];
const findStartingPos = () => {
for (let y = 0; y < h; ++y) {
for (let x = 0; x < w; ++x) {
if (maze[y][x] === 2) return [x, y];
}
}
};
const X = 0;
const Y = 1;
const pos = findStartingPos();
const dir2coord = {
N: [0, -1],
S: [0, 1],
E: [1, 0],
W: [-1, 0],
};
for (dir of directions) {
const coord = dir2coord[dir];
pos[X] += coord[X];
pos[Y] += coord[Y];
if (pos[X] < 0 || pos[X] >= w) return 'Dead';
if (pos[Y] < 0 || pos[Y] >= h) return 'Dead';
if (maze[pos[Y]][pos[X]] === 1) return 'Dead';
if (maze[pos[Y]][pos[X]] === 3) return 'Finish';
}
return 'Lost';
}
var maze = [[1,1,1,1,1,1,1],
[1,0,0,0,0,0,3],
[1,0,1,0,1,0,1],
[0,0,1,0,0,0,1],
[1,0,1,0,1,0,1],
[1,0,0,0,0,0,1],
[1,2,1,0,1,0,1]];
mazeRunner(maze,["N","N","N","N","N","E","E","E","E","E"]); // "Finish"
mazeRunner(maze,["N","N","N","N","N","E","E","S","S","E","E","N","N","E"]); // "Finish"
mazeRunner(maze,["N","N","N","N","N","E","E","E","E","E","W","W"]); // "Finish"
mazeRunner(maze,["N","N","N","W","W"]); // "Dead"
mazeRunner(maze,["N","N","N","N","N","E","E","S","S","S","S","S","S"]); // "Dead"
mazeRunner(maze,["N","E","E","E","E"]); // "Lost"
mazeRunner([
[
1, 1, 1, 1, 1,
1, 1, 1, 0, 1
],
[
1, 3, 1, 0, 1,
0, 0, 0, 0, 1
],
[
1, 0, 1, 0, 0,
0, 1, 1, 0, 1
],
[
1, 0, 1, 1, 1,
1, 1, 0, 0, 1
],
[
1, 0, 1, 0, 0,
0, 0, 0, 0, 1
],
[
1, 0, 1, 0, 1,
0, 1, 0, 0, 1
],
[
1, 0, 1, 0, 1,
0, 0, 0, 0, 0
],
[
1, 0, 1, 0, 1,
0, 1, 1, 0, 1
],
[
1, 0, 0, 0, 1,
0, 0, 0, 0, 1
],
[
1, 1, 1, 0, 1,
1, 1, 1, 2, 1
]
], [ 'N', 'W', 'W', 'W', 'W' ]);

View File

@@ -0,0 +1,44 @@
const opt2 = () => {
const next = (n, c) => {
while (1) {
if (n[c] === '0') {
++c;
continue;
}
const str = n.slice(++c);
if (str.length === 0) return [null, null];
const bi = BigInt(str);
const mod = BigInt('1' + '0'.repeat(str.length));
return [bi, c];
}
};
// 3999
const n625 = '28882689374022112371252012273120606174751574751537381989074240665252066611148827637678947177773960015235903946894098140630385847547907319150251760258945788659491966429549908540773478285449868640866567399269578082162283572603026954569487924380165488488051064862760620827164159132523609790500938385405426324719893931802209823600162545177681029159396504506657809033052772198385286341879645511424748536307235457049044509125214234275955491843973984458712528694819826927029255264834903206526851272202961318699947776535481291519857640422968183091773445277723200737603825883172729279563657419014445235954319103063572496178988203175787761062137708080967811374931911766563031490205784352509572880668464121069252802275061298511616206384006778979402449023875111258689534549514888200678667702341002839549282970286447273625217535443197911855068157264858804852673871684804002188529473022223344541221328464844153593793663133604458940328723478401947357560361346212008675373346913314338717350880212600285752985386643931022326553454776845029957025561658143370236502074744856814787872902092412582905301249124668868351587677499891768678715728134940879276894529797097772305403356618828198702210630557967239806611190197744642421025136748701117131278125400133690086034889084364023875765936821979626181917833520492704199324875237825867148278905344897440142612317035699548419499444610608146207254036559998271588356035049327795540741961849280952093753026852390937562839148571612367351970609224242398777007574955787271559767413458997537695515862718887941516307569668816352155048898271704378508028434084412644126821848514157729916034497017892335796684991447389566001932545827678000618329854426232827257556110733160697015864984222291255485729879337147866323172405515756102352543994999345608083801190741530060056055744818709692785099775918050075416428527708162011350246806058163276171676765260937528056844214486193960499834472806721906670417240094234466197812426690787535944616698508064636137166384049029219341881909581659524477861846140912878298438431703248173428886572737663146519104988029447960814673760503957196893714671801375619055462996814764263903953007319108169802938509890062166509580863811000557423423230896109004106619977392256259918212890625';
// 4000
const n376 = '71117310625977887628747987726879393825248425248462618010925759334747933388851172362321052822226039984764096053105901859369614152452092680849748239741054211340508033570450091459226521714550131359133432600730421917837716427396973045430512075619834511511948935137239379172835840867476390209499061614594573675280106068197790176399837454822318970840603495493342190966947227801614713658120354488575251463692764542950955490874785765724044508156026015541287471305180173072970744735165096793473148727797038681300052223464518708480142359577031816908226554722276799262396174116827270720436342580985554764045680896936427503821011796824212238937862291919032188625068088233436968509794215647490427119331535878930747197724938701488383793615993221020597550976124888741310465450485111799321332297658997160450717029713552726374782464556802088144931842735141195147326128315195997811470526977776655458778671535155846406206336866395541059671276521598052642439638653787991324626653086685661282649119787399714247014613356068977673446545223154970042974438341856629763497925255143185212127097907587417094698750875331131648412322500108231321284271865059120723105470202902227694596643381171801297789369442032760193388809802255357578974863251298882868721874599866309913965110915635976124234063178020373818082166479507295800675124762174132851721094655102559857387682964300451580500555389391853792745963440001728411643964950672204459258038150719047906246973147609062437160851428387632648029390775757601222992425044212728440232586541002462304484137281112058483692430331183647844951101728295621491971565915587355873178151485842270083965502982107664203315008552610433998067454172321999381670145573767172742443889266839302984135015777708744514270120662852133676827594484243897647456005000654391916198809258469939943944255181290307214900224081949924583571472291837988649753193941836723828323234739062471943155785513806039500165527193278093329582759905765533802187573309212464055383301491935363862833615950970780658118090418340475522138153859087121701561568296751826571113427262336853480895011970552039185326239496042803106285328198624380944537003185235736096046992680891830197061490109937833490419136188999442576576769103890995893380022607743740081787109376';
const arr = [1n];
let [c625, c376] = [0, 0];
let cnt = 4000 - 1;
let n1 = BigInt(n625);
let n2 = BigInt(n376);
while (cnt > 0) {
if (n1 > n2) {
arr[cnt] = n1;
[n1, c625] = next(n625, c625);
} else {
arr[cnt] = n2;
[n2, c376] = next(n376, c376);
}
--cnt;
}
return arr;
};
const arr = opt2();
const green = n => arr[n-1];

View File

@@ -0,0 +1,110 @@
const opt1 = () => {
let max = 0;
const next = n => {
let cnt = 1;
while (1) {
const str = cnt++ + (n + '');
const bi = BigInt(str);
const mod = BigInt('1' + '0'.repeat(str.length));
// const pow = (bi * bi) + '';
// const idx = pow.indexOf(str, str.length/2-2);
if (bi * bi % mod === bi) {
if (cnt > max) max = cnt;
return bi;
}
}
};
const arr = [1n];
let n1 = 5n;
let n2 = 6n;
const t = +new Date();
while (1) {
if (n1 < n2) {
arr.push(n1);
n1 = next(n1);
} else {
arr.push(n2);
n2 = next(n2);
}
if (arr.length >= 4e3) break;
const l = arr[arr.length-1];
// console.log(arr.length, l);
}
// console.log(+new Date() - t);
// console.log((arr[arr.length-1]+'').length);
// console.log('max inc', max);
// console.log(arr.slice(-10).map((n, i) => [i, (n+'')]));
const green = n => arr[n];
return arr;
};
const opt2 = () => {
const next = (n, c) => {
while (1) {
if (n[c] === '0') {
++c;
continue;
}
const str = n.slice(++c);
if (str.length === 0) return [null, null];
const bi = BigInt(str);
const mod = BigInt('1' + '0'.repeat(str.length));
// if (bi * bi % mod === bi) {
return [bi, c];
// }
}
};
// 3999
const n625 = '28882689374022112371252012273120606174751574751537381989074240665252066611148827637678947177773960015235903946894098140630385847547907319150251760258945788659491966429549908540773478285449868640866567399269578082162283572603026954569487924380165488488051064862760620827164159132523609790500938385405426324719893931802209823600162545177681029159396504506657809033052772198385286341879645511424748536307235457049044509125214234275955491843973984458712528694819826927029255264834903206526851272202961318699947776535481291519857640422968183091773445277723200737603825883172729279563657419014445235954319103063572496178988203175787761062137708080967811374931911766563031490205784352509572880668464121069252802275061298511616206384006778979402449023875111258689534549514888200678667702341002839549282970286447273625217535443197911855068157264858804852673871684804002188529473022223344541221328464844153593793663133604458940328723478401947357560361346212008675373346913314338717350880212600285752985386643931022326553454776845029957025561658143370236502074744856814787872902092412582905301249124668868351587677499891768678715728134940879276894529797097772305403356618828198702210630557967239806611190197744642421025136748701117131278125400133690086034889084364023875765936821979626181917833520492704199324875237825867148278905344897440142612317035699548419499444610608146207254036559998271588356035049327795540741961849280952093753026852390937562839148571612367351970609224242398777007574955787271559767413458997537695515862718887941516307569668816352155048898271704378508028434084412644126821848514157729916034497017892335796684991447389566001932545827678000618329854426232827257556110733160697015864984222291255485729879337147866323172405515756102352543994999345608083801190741530060056055744818709692785099775918050075416428527708162011350246806058163276171676765260937528056844214486193960499834472806721906670417240094234466197812426690787535944616698508064636137166384049029219341881909581659524477861846140912878298438431703248173428886572737663146519104988029447960814673760503957196893714671801375619055462996814764263903953007319108169802938509890062166509580863811000557423423230896109004106619977392256259918212890625';
// 4000
const n376 = '71117310625977887628747987726879393825248425248462618010925759334747933388851172362321052822226039984764096053105901859369614152452092680849748239741054211340508033570450091459226521714550131359133432600730421917837716427396973045430512075619834511511948935137239379172835840867476390209499061614594573675280106068197790176399837454822318970840603495493342190966947227801614713658120354488575251463692764542950955490874785765724044508156026015541287471305180173072970744735165096793473148727797038681300052223464518708480142359577031816908226554722276799262396174116827270720436342580985554764045680896936427503821011796824212238937862291919032188625068088233436968509794215647490427119331535878930747197724938701488383793615993221020597550976124888741310465450485111799321332297658997160450717029713552726374782464556802088144931842735141195147326128315195997811470526977776655458778671535155846406206336866395541059671276521598052642439638653787991324626653086685661282649119787399714247014613356068977673446545223154970042974438341856629763497925255143185212127097907587417094698750875331131648412322500108231321284271865059120723105470202902227694596643381171801297789369442032760193388809802255357578974863251298882868721874599866309913965110915635976124234063178020373818082166479507295800675124762174132851721094655102559857387682964300451580500555389391853792745963440001728411643964950672204459258038150719047906246973147609062437160851428387632648029390775757601222992425044212728440232586541002462304484137281112058483692430331183647844951101728295621491971565915587355873178151485842270083965502982107664203315008552610433998067454172321999381670145573767172742443889266839302984135015777708744514270120662852133676827594484243897647456005000654391916198809258469939943944255181290307214900224081949924583571472291837988649753193941836723828323234739062471943155785513806039500165527193278093329582759905765533802187573309212464055383301491935363862833615950970780658118090418340475522138153859087121701561568296751826571113427262336853480895011970552039185326239496042803106285328198624380944537003185235736096046992680891830197061490109937833490419136188999442576576769103890995893380022607743740081787109376';
const arr = [1n];
let [c625, c376] = [0, 0];
let cnt = 4000 - 1;
let n1 = BigInt(n625);
let n2 = BigInt(n376);
while (cnt > 0) {
if (n1 > n2) {
arr[cnt] = n1;
[n1, c625] = next(n625, c625);
} else {
arr[cnt] = n2;
[n2, c376] = next(n376, c376);
}
// console.log(c625, c376)
--cnt;
}
return arr;
};
const t1 = +new Date();
const arr2 = opt2();
const t2 = +new Date();
const arr1 = opt1();
const t3 = +new Date();
console.log('opt 1: ', t2-t1);
console.log('opt 2: ', t3-t2);
console.log(arr1.length, arr2.length);
let fcnt = 0;
for (let i = 0; i < arr1.length; ++i) {
if (arr1[i] !== arr2[i]) {
++fcnt;
console.log('FAILED');
}
}
console.log('done', fcnt, 'failed');

View File

@@ -0,0 +1,62 @@
// https://www.codewars.com/kata/58e61f3d8ff24f774400002c/javascript
function assemblerInterpreter(program) {
const lines = (program
.split(/\n/)
.map(line => line.replace(/;.*$/, '').trim())
.filter(line => line !== ''));
const parsed = [];
const labels = new Map();
let diff = 0;
for (i = 0; i < lines.length; ++i) {
const tokens = lines[i].split(/[,\s]+/);
const m = tokens[0].match(/(\w+)\:/);
if (m) {
labels.set(m[1], i-diff);
++diff;
} else if (tokens[0] === 'msg') {
parsed[i-diff] = lines[i].match(/('[^']*')|[a-z0-9_]+/g);
} else {
parsed[i-diff] = tokens;
}
}
let ip = 0;
let cmp;
const res = [];
const stack = [];
const regs = new Map();
const constOrVal = n => ((+n) + '') === n ? +n : regs.get(n);
const jmpIf = (lbl, cond) => ip = cond ? labels.get(lbl) - 1 : ip;
const instr = {
mov: (a, b) => regs.set(a, constOrVal(b)),
inc: a => regs.set(a, regs.get(a) + 1),
dec: a => regs.set(a, regs.get(a) - 1),
add: (a, b) => regs.set(a, regs.get(a) + constOrVal(b)),
sub: (a, b) => regs.set(a, regs.get(a) - constOrVal(b)),
mul: (a, b) => regs.set(a, regs.get(a) * constOrVal(b)),
div: (a, b) => regs.set(a, regs.get(a) / constOrVal(b) | 0),
jmp: lbl => jmpIf(lbl, true),
cmp: (a, b) => cmp = Math.sign(constOrVal(a) - constOrVal(b)),
jne: lbl => jmpIf(lbl, cmp !== 0),
je: lbl => jmpIf(lbl, cmp === 0),
jge: lbl => jmpIf(lbl, cmp >= 0),
jg: lbl => jmpIf(lbl, cmp > 0),
jle: lbl => jmpIf(lbl, cmp <= 0),
jl: lbl => jmpIf(lbl, cmp < 0),
call: lbl => (stack.push(ip), jmpIf(lbl, true)),
ret: () => ip = stack.pop(),
msg: (...args) => res.push(args.map(arg => {
const m = arg.match(/^'(.*)'$/);
if (m) return m[1];
return constOrVal(arg);
}).join``)
};
let cntdwn = 1e6;
while (ip < parsed.length && --cntdwn > 0) {
const [cmd, ...args] = parsed[ip];
if (cmd === 'end') return res.join``;
instr[cmd](...args);
++ip;
}
return -1;
}

View File

@@ -0,0 +1,73 @@
// https://www.codewars.com/kata/6320b05f269dbb001fb409f1
const Sieve = (max = 1e5) => {
const maxi = Math.sqrt(max);
const notPrime = new Int8Array(max);
notPrime[0] = notPrime[1] = 1;
for (let i = 2; i < maxi; ++i) {
if (notPrime[i] === 0) {
for (let j = 2*i; j < max; j += i) {
notPrime[j] = 1;
}
}
}
return {
isPrime: n => !notPrime[n],
getPrimes: n => {
const res = [];
let cnt = 0;
const limit = Math.min(max, n);
for (let i = 0; i < limit; ++i) {
if (!notPrime[i]) {
res[cnt++] = i;
}
}
return res;
}
};
};
const sieve = Sieve(1e5);
const getHelper = n => {
const target = n + '';
return m => {
const cmp = m + '';
return [...cmp].map((d, i) => {
if (+target[i] > +d) {
return '<';
} else if (+target[i] < +d) {
return '>';
}
return '=';
}).join('')
};
};
const findPrime = helper => {
let d5 = sieve.getPrimes(1e5).filter(n => n > 1e4);
let max = 10;
while (--max) {
const mid = d5.length >> 1;
let prime = d5[mid];
const hint = helper(prime);
d5 = d5.filter(n => {
const ps = prime + '';
const ns = n + '';
for (let i = 0; i < 5; ++i) {
if (hint[i] === '>') {
if (ps[i] < ns[i]) return false;
} else if (hint[i] === '<') {
if (ps[i] > ns[i]) return false;
} else {
if (ps[i] !== ns[i]) return false;
}
}
return true;
});
if (d5.length === 1) return d5[0];
}
};
findPrime(getHelper(21767));

View File

@@ -0,0 +1,17 @@
// https://www.codewars.com/kata/56eb16655250549e4b0013f4/javascript
function mostFrequentDays(year) {
const weekdays = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'];
const ds = new Date(year, 0, 1, 0, 0, 0, 0);
const de1 = new Date(year, 0, 365, 0, 0, 0, 0);
const de2 = new Date(year, 0, 366, 0, 0, 0, 0);
const de = de2.getFullYear() === year ? de2 : de1;
const freq = [];
for (let i = ds.getDay();1; ++i) {
freq.push((i+6)%7);
if (i % 7 === de.getDay()) {
break;
}
}
return freq.sort((a, b) => a-b).map(w => weekdays[w]);
}

View File

@@ -0,0 +1,25 @@
const sequence = fn => {
return {
take: n => {
const res = [];
for (let i = 0; i < n; ++i) {
res[i] = fn(i);
}
return res;
},
takeWhile: pred => {
const res = [];
for (let i = 0; 1; ++i) {
const val = fn(i);
if (!pred(val)) break;
res[i] = fn(i);
}
return res;
},
};
};
sequence(n => n).takeWhile(n => n < 10); // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
sequence(n => n * n).take(10); // [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

View File

@@ -0,0 +1,13 @@
const ride = (a, b) => {
const alph = 'abcdefghijklmnopqrstuvwxyz';
const map = new Map();
[...alph].map((c, i) => map.set(c.toUpperCase(), i + 1));
const cs = s => {
return [...s].reduce((a, b) => a * map.get(b), 1) % 47;
};
return cs(a) === cs(b) ? 'GO' : 'STAY';
};
ride('COMETQ', 'HVNGAT');

View File

@@ -0,0 +1,14 @@
const age = (x, y) => {
const s = Math.sign(x);
let n = 100;
for (let i = 0; i < 1000; ++i) {
n += x * s;
n /= y ** s;
}
return s > 0 ? x + n : n;
};
age(-15, 0.25);
age(6, 3);
age(30, 1.4918032786885247);

View File

@@ -0,0 +1,34 @@
const createTwoSetsOfEqualSum = n => {
const all = n * (n+1) / 2;
if (all % 2 === 1) return [];
const half = all / 2;
const a = [];
let sum = 0;
let skip;
let i = n;
for (; i > 0; --i) {
sum += i;
if (sum > half) {
sum -= i;
skip = half-sum;
a.push(skip);
break;
} else {
a.push(i);
skip = i;
if (sum === half) break;
}
}
const b = [];
for (let j = 1; j <= i; ++j) {
if (j === skip) continue;
b.push(j);
}
return [a, b];
};
createTwoSetsOfEqualSum(3);
createTwoSetsOfEqualSum(8);
createTwoSetsOfEqualSum(9);
createTwoSetsOfEqualSum(23);