Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const fs = require('fs');
- const readline = require('readline');
- async function processLineByLine() {
- const fileStream = fs.createReadStream('login'); // data is copied and pasted into a file called 'login'
- const rl = readline.createInterface({
- input: fileStream,
- crlfDelay: Infinity
- });
- let charOrders = {};
- for await (const line of rl) {
- const chars = [...line.trim()]
- if (chars.length !== 3) {
- throw new Error('chars not of size 3');
- }
- // build charOrders such that each key is a letter of the password that maps to a set of all the letters after it
- addMapping(charOrders, chars[0], chars[1]);
- addMapping(charOrders, chars[0], chars[2]);
- addMapping(charOrders, chars[1], chars[2]);
- addMapping(charOrders, chars[2], null);
- }
- // the size of the list corresponds (inversely) to the index of the the password
- const pass = new Array(1000)
- for (let [key, val] of Object.entries(charOrders)) {
- if (val.has(null)) {
- val.delete(null)
- }
- pass[1000-val.size-1] = key;
- }
- console.log(pass.join(""));
- }
- processLineByLine()
- function addMapping(dest, key, val) {
- if (!dest.hasOwnProperty(key)) {
- dest[key] = new Set();
- }
- dest[key].add(val);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement