Improve bruteforce output
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Andrey Sitnik · committed by Andrey Sitnik · parent efae81c · Browse files · 1 file·+55 added−18 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+55 added−18 removed
- tests:+55 added−18 removed
Provenance
- Commit
ebe028c- Tree
96997b5- Author
Andrey Sitnik <andrey@sitnik.ru>- Committer
Andrey Sitnik <andrey@sitnik.ru>- Parent
efae81c
1 file changed+55 added−18 removed
modified:test/bruteforce+55 added-18 removed
4 unmodified lines5 var path = require('path')6 var fs = require('fs')7 8 var filePath = path.join(process.cwd(), process.argv[2])9-var best = {10- value: '',11- size: Infinity12-}13-var lastString9+var used = { }10 15-function shuffle (a) {11+function shuffle (str) {12 var j, x, i17- for (i = a.length - 1; i > 0; i--) {13+ for (i = str.length - 1; i > 0; i--) {14 j = Math.floor(Math.random() * (i + 1))19- x = a[i]20- a[i] = a[j]21- a[j] = x15+ x = str[i]16+ str[i] = str[j]17+ str[j] = x18 }23- return a19+ return str20 }21 26-function saveFirstString () {22+function shuffleUnique (str) {23+ var mixed24+ while (true) {25+ mixed = shuffle(str)26+ if (!used[mixed]) {27+ used[mixed] = true28+ return mixed29+ }30+ }31+}32+33+function factorial (number) {34+ var total = 135+ while (number > 0) {36+ total *= number37+ number = number - 138+ }39+ return total40+}41+42+var best = {43+ value: '',44+ size: Infinity45+}46+var lastString47+var step = 048+var prevPercent = 049+var maxSteps = factorial(64)50+51+function findInitialString () {52 var data = fs.readFileSync(filePath, 'utf8')28- data.replace(/[A-Za-z0-9~_]{58}/g, function (match) {53+ data.replace(/[A-Za-z0-9~_]{64}/, function (match) {54 lastString = match55 })56 }57 58 function shuffleStringInFile () {59 var data = fs.readFileSync(filePath, 'utf8')60 36- var result = data.replace(/[A-Za-z0-9~_]{64}/g, function (match) {37- lastString = shuffle(match.split('')).join('')61+ var result = data.replace(/[A-Za-z0-9~_]{64}/, function (match) {62+ lastString = shuffleUnique(match.split('')).join('')63 return lastString64 })65 66 fs.writeFileSync(filePath + '', result, 'utf8')67 }68 69 function checkSize () {70+ step += 171 sizeLimit(filePath).then(function (size) {72 if (size.gzip < best.size) {73 best.value = lastString74 best.size = size.gzip75 process.stdout.write(76 chalk.green.bold(best.size + 'B') + ' ' + best.value + '\n')77+ } else {78+ var percent = Math.floor(100 * step / maxSteps)79+ if (percent > prevPercent) {80+ prevPercent = percent81+ process.stdout.write(chalk.gray(percent + '%') + '\n')82+ }83 }84+ if (step === maxSteps) {85+ process.exit(0)86+ } else {52- shuffleStringInFile()53- checkSize()87+ shuffleStringInFile()88+ checkSize()89+ }90 })91 }92 57-saveFirstString()93+findInitialString()94 if (typeof lastString === 'undefined') {95 process.stderr.write(chalk.red('Alphabet was not found') + '\n')96 process.exit(1)97 }98+99 checkSize()