-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday02.js
38 lines (27 loc) · 1.02 KB
/
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
37
38
const entries = require('./entries/day02.json');
const regex = /^(\d+)-(\d+) ([a-zA-Z]{1}): ([a-zA-Z]*)$/;
const formattedEntries = entries.map(s => {
const [, min, max, letter, password] = s.match(regex);
const parsedMin = parseInt(min, 10);
const parsedMax = parseInt(max, 10);
const foundLetters = password.match(new RegExp(letter, 'g'));
const firstPositionLetter = password.charAt(parsedMin - 1);
const lastPositionLetter = password.charAt(parsedMax - 1);
return {
min: parsedMin,
max: parsedMax,
letter,
letterCount: foundLetters ? foundLetters.length : 0,
firstPositionLetter,
lastPositionLetter,
password,
};
});
// STEP 1
const validEntriesBasic = formattedEntries.filter(obj => obj.min <= obj.letterCount && obj.letterCount <= obj.max);
console.log(validEntriesBasic.length);
// STEP 2
const validEntriesAdvanced = formattedEntries.filter(
obj => (obj.firstPositionLetter === obj.letter) ^ (obj.lastPositionLetter === obj.letter)
);
console.log(validEntriesAdvanced.length);