User:Hakerh400/Random JavaScript snippets

From Esolang
Jump to navigation Jump to search

In this article I share some of JavaScript snippets I created in the past.

JavaScript snippets

Very inefficient SAT solver

const solve = cnf => {
  let loops = '';
  let result = '';
  let varsNum = 0;

  while(1){
    try{
      return Function(`
        ${loops}
        if(${cnf})
        return[${result}].slice(1)
      `)() || null;
    }catch(e){
      loops += `for(const x${varsNum} of[0,1])`;
      result += `,x${varsNum++}`;
    }
  }
};

console.log(solve(`
  (x0 | x1) &
  (!x0 | !x1) &
  x0
`));

Integer program generator

const base = 2n;

const xor = a => f1(base, 0n, BigInt(a));
const swap = a => f1(base, 1n, BigInt(a));
const push = a => f1(base, 2n, BigInt(a));
const pop = (a, b) => f1(base, 3n, f1(base, BigInt(a), BigInt(b)));

const arr = (e, a) => {
  let b = 0n;

  for(let i = a.length - 1; i !== -1; i--)
    b = f1(e, a[i], b);

  return b;
};

src = String(base * f1(base, 1n, arr(base, [
  swap(2),
  pop(20, 3),
  push(26),
  xor(4),
  swap(5),
  pop(10, 6),
  pop(10, 7),
  swap(8),
  push(9),
  swap(5),
  push(11),
  xor(12),
  push(13),
  xor(14),
  swap(15),
  pop(16, 2),
  swap(17),
  push(18),
  push(19),
  xor(15),
  swap(21),
  pop(0, 22),
  pop(21, 23),
  swap(24),
  push(25),
  xor(20),
  xor(),
])));

input = BigInt('5');

log(O.match(src, /.{100}|.+/g).join('\n'));
log();

Hidden messages

eval(atob`KGY9KGE9OSxiPWF0b2IoYEdhMTFhVDJzc0lITnZJR3RsWlhBZ1oyOXBibWNoYTExYWJhOWFVR3hsWVhObElISjFiaU
IwYUdVZ2IzVjBjSFYwSUc5bUlIUm9hWE1nY0hKdlozSmhiU0JoWjJGcGJpNGdXVzkxSjJ4c0lHZGxkQ0JoSUdScFptWmxjbVZ1ZE
NCdFpYTnpZV2RsTGdhOWFhMTdhUWxSWExDQjBhR2x6SUhOamNtbHdkQ0JwY3lCblpXNWxjbUYwWldRZ2RYTnBibWNnYUhSMGNITT
ZMeT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JHtmfSkoKWAucmVwbGFjZSgvXGQrLy
xhPT4oYz1+YSYxKSooYT4+YykrKGM9fmMrMikqLX4oKGE8PGMpK2EqYykpKX1cYClgLnJlcGxhY2UoLy57MTAwfS9nLGE9PmAke2
F9XG5gKX1cbi8qKlxuICpcbiAqICR7Yi5yZXBsYWNlKC9cbi9nLCdcbicrKGQ9JyAnKSsnKiAnK2QrJyAnKX1cbiAqXG4gKi9gOm
IpKSgp`)

A loop using a set

const loop = (a, b) =>
  new Set([a]).forEach((a, c, d) =>
    a(d.delete(a)) && b(d.add(a)));

Search for a string whose compressed length violates gzip compression level expectation

const find = () => {
  const bytes = O.ca(256, i => i);

  for(let i = 0n;; i++){
    if((i & 65535n) === 0n && i !== 0n)
      log(String(i));

    const buf = Buffer.from(arrOrder.arr(bytes, i));
    let len = null;

    for(let j = 1; j <= 9; j++){
      const len1 = zlib.gzipSync(buf, {level: j}).length;

      if(len !== null && len1 > len)
        O.exit(buf);

      len = len1;
    }
  }
}

const test = buf => {
  for(let level = 1; level <= 9; level++)
    console.log(zlib.gzipSync(buf, {level}).length);
  console.log('\n');
};

find();

brainfuck to Qarkegs converter

const a = 'C:/path/to/file.txt';

const src = `
+[-->-[>>+>-----<<]<--<---]>-.>>>+.>>..+++[.>]<<<<.+++.------.<<-.>>>>+.
`;

const b = O.sanl(`
<3(4)5(-545)25
>53(4)5(-545)2
+4
--
,0
.31
[(
])
`.trim()).map(a => a.trim());

const c = O.obj();

for(const a of b)
  c[a[0]] = a.slice(1).replace(/\-/g, '4'.repeat(255));

O.wfs(a, (a => {
  return a.replace(/./gs, a => {
    return a in c ? c[a] : '';
  });
})(src));