const crypto = require('crypto'); const md5Hex = str => crypto.createHash('md5').update(str).digest('hex'); const solve = doorId => { let pwd = []; for (let i = 0, j = 0; j < 8; i++) { let hex = md5Hex(doorId + i); if (hex.slice(0,5) === '00000') { j++; pwd.push(hex[5]); } } return pwd.join(''); } console.log(`Part1 answer: ${solve('uqwqemis')}`); const getPosition = c => { let n = parseInt(c, 10); if (n >= 0 && n <= 7) { return n; } else { return -1; } } const solve2 = doorId => { let pwd = [], mark = []; for (let i = 0, j = 0; j < 8; i++) { let hex = md5Hex(doorId + i), pos; if (hex.slice(0,5) === '00000' && (pos = getPosition(hex[5])) > -1 && !mark[pos]) { j++; pwd[pos] = hex[6]; mark[pos] = true; } } return pwd.join(''); } console.log(`Part2 answer: ${solve2('uqwqemis')}`);