-
Notifications
You must be signed in to change notification settings - Fork 0
/
day4.js
33 lines (30 loc) · 772 Bytes
/
day4.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
const fs = require('fs');
const input = fs.readFileSync('./day4_data.txt', { encoding: 'utf8' })
.trim()
.split('\n');
function part1() {
let numValid = 0;
for (const line of input) {
const set = new Set();
const words = line.trim().split(/\s+/);
for (const word of words) {
set.add(word);
}
numValid += Number(set.size == words.length);
}
return numValid;
}
function part2() {
let numValid = 0;
for (const line of input) {
const set = new Set();
const words = line.trim().split(/\s+/);
for (const word of words) {
set.add(word.split('').sort().join(''));
}
numValid += Number(set.size == words.length);
}
return numValid;
}
console.log('Part 1: %d', part1());
console.log('Part 2: %d', part2());