Repository navigation
Expand file tree
/
Copy path5.js
More file actions
107 lines (77 loc) · 2.34 KB
/
Copy path5.js
File metadata and controls
107 lines (77 loc) · 2.34 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
"use strict";
exports.solutions = [solveFirst, solveSecond];
exports.parse = function parse(rawData) {
var parsed = rawData.split("\n");
parsed.length -= 1;
return parsed;
}
function solveFirst(parsedData) {
return checkNiceness(parsedData, niceRulesPartOne);
}
function solveSecond(parsedData) {
return checkNiceness(parsedData, niceRulesPartTwo);
}
function checkNiceness(parsedData, niceRules) {
var totalNiceCount = 0;
for (let i = 0; i < parsedData.length; ++i) {
for (let j = 0; j < niceRules.length; ++j) {
if (!niceRules[j](parsedData[i])) {
break;
}
(j === (niceRules.length - 1)) && ++totalNiceCount;
}
}
return totalNiceCount;
}
var niceRulesPartOne = [
(str) => {
const threshold = 3;
var vowels = ["a", "e", "i", "o", "u"];
var vowelCount = 0;
for (let i = 0; i < str.length; ++i) {
vowels.indexOf(str[i]) > -1 && ++vowelCount;
if (vowelCount >= threshold) {
return true;
}
}
return false;
}, (str) => {
var lookBehind;
for (let i = 0; i < str.length; ++i) {
if (lookBehind && str[i] === lookBehind) {
return true;
}
lookBehind = str[i];
}
return false;
}, (str) => {
var bannedWords = ["ab", "cd", "pq", "xy"];
for (let i = 0; i < bannedWords.length; ++i) {
if (str.indexOf(bannedWords[i]) > -1) {
return false;
}
}
return true;
}];
var niceRulesPartTwo = [
(str) => {
const tokenLength = 2;
for (let i = 0; i < str.length - (tokenLength * 2 - 1); ++i) {
let token = str.substring(i, i + tokenLength);
let restOfString = str.substring(i + tokenLength);
if (restOfString.indexOf(token) > -1) {
return true;
}
}
return false;
}, (str) => {
const lookBehindCount = 2;
var lookBehind;
for (let i = 0; i < str.length; ++i) {
(i >= lookBehindCount) && (lookBehind = str.substring(i - lookBehindCount, i));
if (lookBehind && str[i] === lookBehind[0]) {
return true;
}
}
return false;
}];