-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday2.js
60 lines (60 loc) · 2.19 KB
/
day2.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
"use strict";
exports.__esModule = true;
var day2Data_1 = require("./day2Data");
var list = ["1-3 a: abcde",
"1-3 b: cdefg",
"2-9 c: ccccccccc",];
var pattern = /(\d+)-(\d+) (\w): (\S+)/;
console.log("reg check", list.map(function (pw) {
pw.match(pattern);
}));
var correctPasswordCount = function (input) {
var splitted = input.split("\n");
var arr = [];
var count = 0;
splitted.forEach(function (item) {
arr.push(item.split(":"));
});
arr.forEach(function (item) {
var passwordCriteria = item[0];
var password = item[1];
var characterMatch = passwordCriteria[passwordCriteria.length - 1];
//remove last 2 empty characters and divide array by - for low and high ranges
var numbersRange = passwordCriteria
.substring(0, passwordCriteria.length - 2)
.split("-");
var regexp = new RegExp(characterMatch, "gi");
var matches = 0;
password.match(regexp) ? (matches = password.match(regexp).length) : null;
matches >= parseFloat(numbersRange[0]) &&
matches <= parseFloat(numbersRange[1])
? count++
: (count = count);
});
return count;
};
correctPasswordCount(day2Data_1.textInput);
var modifiedPasswordCount = function (input) {
var splitted = input.split("\n");
var arr = [];
var count = 0;
//create an array of arrays with 2 items - 1st is the criteria; 2nd is the actual password
splitted.forEach(function (item) {
arr.push(item.split(":"));
});
arr.forEach(function (item) {
var passwordCriteria = item[0];
var password = item[1];
var characterMatch = passwordCriteria[passwordCriteria.length - 1];
//remove last 2 empty characters and divide array by - for low and high ranges
var numbersRange = passwordCriteria
.substring(0, passwordCriteria.length - 2)
.split("-");
var matches = 0;
password[numbersRange[0]] == characterMatch ? matches++ : null;
password[numbersRange[1]] == characterMatch ? matches++ : null;
matches == 1 ? count++ : (count = count);
});
return count;
};
modifiedPasswordCount(day2Data_1.textInput);