-
Notifications
You must be signed in to change notification settings - Fork 5
/
day02.js
36 lines (25 loc) · 881 Bytes
/
day02.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
34
35
36
const fs = require('fs');
const lines = fs.readFileSync('day02.txt', {encoding: 'utf-8'}).split('\n').filter(x => x);
let validPasswords = 0;
lines.forEach(line => {
const {groups} = /^(?<from>\d+)-(?<to>\d+) (?<char>.): (?<password>.*)$/.exec(line);
const counter = {};
[...groups.password].forEach(char => {
if(!counter[char]) {
counter[char] = 0;
}
counter[char]++;
});
if(counter[groups.char] >= groups.from && counter[groups.char] <= groups.to) {
validPasswords++;
}
})
console.log(validPasswords);
validPasswords = 0;
lines.forEach(line => {
const {groups} = /^(?<from>\d+)-(?<to>\d+) (?<char>.): (?<password>.*)$/.exec(line);
if(groups.password[groups.from-1] == groups.char ^ groups.password[groups.to-1] == groups.char) {
validPasswords++;
}
})
console.log(validPasswords);