Skip to content

Instantly share code, notes, and snippets.

@Announcement
Created December 13, 2018 06:32
Show Gist options
  • Save Announcement/0d7c7049409727d12af9915975b365f2 to your computer and use it in GitHub Desktop.
Save Announcement/0d7c7049409727d12af9915975b365f2 to your computer and use it in GitHub Desktop.
substr pivot
function format_large_number (it) {
it = it.toString()
while ((/(?<=\d)(\d{3})($|(?=[,\.]))/gim).test(it)) {
it = it.replace(/(?<=\d)(\d{3})($|(?=[,\.]))/gim, ',$1')
}
return it;
}
function * dd3 (a, b, ax, bx, ay, by, n) {
if (ax === null || ax === undefined) ax = 0;
if (bx === null || bx === undefined) bx = 0;
if (ay === null || ay === undefined) ay = a.length;
if (by === null || by === undefined) by = b.length;
const as = new Set;
const bs = new Set;
const cs = new Set;
for (let ah = ~~ax; ah <= ay; ah++)
for (let ak = -~ah; ak <= ay; ak++)
as.add([ ah, ak, a.substring(ah, ak) ])
for (let bh = ~~bx; bh <= by; bh++)
for (let bk = -~bh; bk <= by; bk++)
bs.add([ bh, bk, b.substring(bh, bk) ])
for (const [ ah, ak, at ] of as)
for (const [ bh, bk, bt ] of bs)
if (at === bt) cs.add([ bk - bh, ah, bh, at])
const f = x => x > 0 ? 2 ** x + f(x - 1) : 1
const y = f(cs.size);
let s = y.toString();
s = format_large_number(s)
console.log(cs)
console.log(`Array of ${cs.size} has ${s} possibilities.`)
let i = 0;
let previous = 0;
let time = Date.now();
for (const unique_combination of unique_combinations(...cs)) {
i++;
const percent = Math.floor(i / y * 100)
if (percent > previous) {
const now = Date.now()
const elapsed = now - time;
const estimate = elapsed * y / i;
const remaining = estimate - elapsed;
const remaining_milliseconds = Math.floor(remaining) % 1000;
const remaining_seconds = Math.floor(remaining / 1000) % 60;
const remaining_minutes = Math.floor(remaining / 1000 / 60) % 60;
const remaining_hours = Math.floor(remaining / 1000 / 60 / 60) % 24;
const remaining_days = Math.floor(remaining / 1000 / 60 / 60 / 24);
const remaining_object = { days: remaining_days, hours: remaining_hours, minutes: remaining_minutes, seconds: remaining_seconds, milliseconds: remaining_milliseconds };
const object = Object.entries(remaining_object).filter(it => it[1] !== 0).reduce((object, [ key, value ]) => { object[key] = value; return object; }, {})
const array = [
object.days > 1 ? `${ object.days.toString().padStart(2) } days` : object.days === 1 ? ' 1 day ' : '',
object.hours > 1 ? `${ object.hours.toString().padStart(2) } hours` : object.hours === 1 ? ' 1 hour ' : '',
object.minutes > 1 ? `${ object.minutes.toString().padStart(2) } minutes` : object.minutes === 1 ? ' 1 minute ' : '',
object.seconds > 1 ? `${ object.seconds.toString().padStart(2, '0') } seconds` : object.seconds === 1 ? ' 1 second ' : '',
object.milliseconds > 1 ? `${ object.milliseconds.toString().padStart(3, '0') } milliseconds` : object.milliseconds === 1 ? '001 millisecond ' : '']
previous = percent;
console.log(`${percent.toString().padStart(3)}%`, format_large_number(i).toString().padStart(Math.log10(y) + 1 + Math.floor(Math.log10(y) / 3)), ...array); }
}
}
const a = 'cheeseburger';
const b = 'whataburger';
const solutions = new Map;
for (const c of dd3(a, b, 0, 0, a.length, b.length, 0)) //console.log(c.map(d => [ a.substr(d[1], d[0]), b.substr(d[2], d[0])]))
{
console.log(...c)
}
function * unique_combinations (...them) {
for (let h = 0; h < them.length; h++) {
yield [ them[h] ]
if (them.length - h > 1)
for (const $ of unique_combinations(...them.slice(h + 1))) {
yield [ them[h], ...$ ]
}
}
}
@Announcement
Copy link
Author

Set {
  [ 1, 1, 1, 'h' ],
  [ 1, 2, 9, 'e' ],
  [ 1, 3, 9, 'e' ],
  [ 1, 5, 9, 'e' ],
  [ 1, 6, 5, 'b' ],
  [ 2, 6, 5, 'bu' ],
  [ 3, 6, 5, 'bur' ],
  [ 4, 6, 5, 'burg' ],
  [ 5, 6, 5, 'burge' ],
  [ 6, 6, 5, 'burger' ],
  [ 1, 7, 6, 'u' ],
  [ 2, 7, 6, 'ur' ],
  [ 3, 7, 6, 'urg' ],
  [ 4, 7, 6, 'urge' ],
  [ 5, 7, 6, 'urger' ],
  [ 1, 8, 7, 'r' ],
  [ 1, 8, 10, 'r' ],
  [ 2, 8, 7, 'rg' ],
  [ 3, 8, 7, 'rge' ],
  [ 4, 8, 7, 'rger' ],
  [ 1, 9, 8, 'g' ],
  [ 2, 9, 8, 'ge' ],
  [ 3, 9, 8, 'ger' ],
  [ 1, 10, 9, 'e' ],
  [ 2, 10, 9, 'er' ],
  [ 1, 11, 7, 'r' ],
  [ 1, 11, 10, 'r' ] }
Array of 27 has 268,435,455 possibilities.
  1%   2,684,355   10 minutes 05 seconds 582 milliseconds
  2%   5,368,710   11 minutes 02 seconds 283 milliseconds

<--- Last few GCs --->

[26204:00000203F6C50110]    22580 ms: Scavenge 1383.0 (1422.7) -> 1382.1 (1423.2) MB, 1.9 / 0.0 ms  (average mu = 0.202, current mu = 0.188) allocation failure
[26204:00000203F6C50110]    22583 ms: Scavenge 1383.0 (1423.2) -> 1382.1 (1423.7) MB, 1.9 / 0.0 ms  (average mu = 0.202, current mu = 0.188) allocation failure
[26204:00000203F6C50110]    22586 ms: Scavenge 1383.1 (1423.7) -> 1382.2 (1424.2) MB, 1.9 / 0.0 ms  (average mu = 0.202, current mu = 0.188) allocation failure


<--- JS stacktrace --->

==== JS stack trace =========================================

    0: ExitFrame [pc: 000000BF28D5C5C1]
Security context: 0x0214a331e6e1 <JSObject>
    1: unique_combinations [0000026CF0F5EB19] [C:\Users\power\OneDrive\Documents\Development\CleanStream\dd3.js:~72] [pc=000000BF28DF5B3F](this=0x005be8a9ad49 <JSGlobal Object>)
    2: next [00000214A3319DE1](this=0x009d911a5969 <JSGenerator>)
    3: unique_combinations [0000026CF0F5EB19] [C:\Users\power\OneDrive\Documents\Development\CleanStream\dd3.js:~7...

FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory
 1: 00007FF6A9B9ECF5
 2: 00007FF6A9B781A6
 3: 00007FF6A9B78BB0
 4: 00007FF6A9E09AAE
 5: 00007FF6A9E099DF
 6: 00007FF6AA347724
 7: 00007FF6AA33DE87
 8: 00007FF6AA33C3FC
 9: 00007FF6AA345377
10: 00007FF6AA3453F6
11: 00007FF6A9EE84B7
12: 00007FF6A9F8019A

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment