const input = require('fs').readFileSync('d6.txt', 'utf8'); const solve = (input, mostCommon) => input.split('\n').reduce((res, line) => line.split('').reduce((res, c, index) => { let col = res[index] || {}; col[c] = (col[c] || 0) + 1; res[index] = col; return res; }, res), []).map(col => { let commonChar; for (let c in col) { if (!commonChar || (mostCommon && col[c] > col[commonChar]) || (!mostCommon && col[c] < col[commonChar])) { commonChar = c; } } return commonChar; }).join(''); console.log(`part1 answer: ${solve(input, true)}`); console.log(`part2 answer: ${solve(input, false)}`);