Skip to content

Instantly share code, notes, and snippets.

@Friss
Created December 7, 2020 06:09
Show Gist options
  • Save Friss/d045f9c3f113d6554e2ea53028603140 to your computer and use it in GitHub Desktop.
Save Friss/d045f9c3f113d6554e2ea53028603140 to your computer and use it in GitHub Desktop.
const fs = require('fs').promises;
(async () => {
console.log(`Reading input from ${__dirname}/${process.argv[2]}.txt`);
const inputData = await fs.readFile(`${__dirname}/${process.argv[2]}.txt`);
const inputs = inputData
.toString()
.split('\n\n')
.filter((b) => b);
let count1 = 0;
let count2 = 0;
inputs.forEach((group) => {
const groupAnswers = {};
const groupNum = group.split('\n').filter((c) => c).length;
group
.replaceAll('\n', '')
.split('')
.forEach((ans) => {
if (groupAnswers[ans]) {
groupAnswers[ans]++;
} else {
groupAnswers[ans] = 1;
}
});
count2 += Object.keys(groupAnswers).reduce((count, ans) => {
return groupAnswers[ans] === groupNum ? count + 1 : count;
}, 0);
count1 += Object.keys(groupAnswers).length;
});
console.log('count1', count1);
console.log('count2', count2);
let refactor1 = 0;
let refactor2 = 0;
inputs.forEach((group) => {
const numGroup = group.split('\n').filter((c) => c).length;
const groupSet = group
.split('\n')
.filter((c) => c)
.map((p) => new Set([...p.split('')]))
.reduce((c, l) => {
return new Set([...c.values(), ...l.values()]);
}, new Set());
refactor1 += groupSet.size;
});
console.log('refactor1', refactor1);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment