-
Notifications
You must be signed in to change notification settings - Fork 0
/
21-22.ts
75 lines (61 loc) · 1.92 KB
/
21-22.ts
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
const unwantedReg = /[iol]/;
const incrementLetter = (l:string) : string => {
if (l === 'z') return 'a';
return String.fromCharCode(l.charCodeAt(0) + 1);
};
const replaceChar = (str: string, index: number, newChar: string): string => {
return str.substr(0, index) + newChar + str.substr(index + 1);
};
const uniqArr = (arr: any[]): any[] => {
return arr.filter((value, index, self) => {
return self.indexOf(value) === index;
});
};
const increment = (password: string) :string => {
if (unwantedReg.test(password)) {
let matches = password.match(unwantedReg);
if (matches !== null && matches.length) {
let leftmostUnwanted = password[matches.index!];
return password.substr(0, matches.index!) + incrementLetter(leftmostUnwanted) + 'a'.repeat(password.length - matches.index! - 1);
}
}
for (let i = password.length-1; i >= 0; i--) {
if (password[i] === 'z') {
let pArr = password.split('');
pArr[i] = 'a';
password = pArr.join('');
continue;
} else {
return replaceChar(password, i, incrementLetter(password[i]));
}
}
return '';
};
const isValid = (p: string): boolean => {
if (unwantedReg.test(p)) return false;
let twoSameMatches = p.match(/([a-z])\1/g);
if (twoSameMatches === null || uniqArr(twoSameMatches).length < 2) return false;
for (let i = 0; i < p.length - 2; i++) {
let currentStraight = 0;
let cc1 = p.charCodeAt(i);
let cc2 = p.charCodeAt(i+1);
if (cc2 - cc1 == 1) {
let cc3 = p.charCodeAt(i+2);
if (cc3 - cc2 == 1) {
return true;
}
}
}
return false;
};
const getAnswer = (password: string) => {
let newPassword = increment(password);
while (!isValid(newPassword)) {
newPassword = increment(newPassword);
}
return newPassword;
};
let answer1 = getAnswer('cqjxjnds');
console.log(`Answer 1: ${answer1}`);
let answer2 = getAnswer(answer1);
console.log(`Answer 2: ${answer2}`);