Advertisement
Guest User

Untitled

a guest
Apr 8th, 2020
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const fs = require('fs');
  2. const readline = require('readline');
  3.  
  4. async function processLineByLine() {
  5.     const fileStream = fs.createReadStream('login'); // data is copied and pasted into a file called 'login'
  6.  
  7.     const rl = readline.createInterface({
  8.       input: fileStream,
  9.       crlfDelay: Infinity
  10.     });
  11.  
  12.     let charOrders = {};
  13.  
  14.     for await (const line of rl) {
  15.       const chars = [...line.trim()]
  16.       if (chars.length !== 3) {
  17.           throw new Error('chars not of size 3');
  18.       }
  19.  
  20.       // build charOrders such that each key is a letter of the password that maps to a set of all the letters after it
  21.  
  22.       addMapping(charOrders, chars[0], chars[1]);
  23.       addMapping(charOrders, chars[0], chars[2]);
  24.       addMapping(charOrders, chars[1], chars[2]);
  25.       addMapping(charOrders, chars[2], null);
  26.     }
  27.  
  28.     // the size of the list corresponds (inversely) to the index of the the password
  29.  
  30.     const pass = new Array(1000)
  31.     for (let [key, val] of Object.entries(charOrders)) {
  32.         if (val.has(null)) {
  33.             val.delete(null)
  34.         }
  35.  
  36.         pass[1000-val.size-1] = key;
  37.     }
  38.     console.log(pass.join(""));
  39. }
  40. processLineByLine()
  41.  
  42. function addMapping(dest, key, val) {
  43.     if (!dest.hasOwnProperty(key)) {
  44.         dest[key] = new Set();
  45.     }
  46.     dest[key].add(val);
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement