-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path04.ts
88 lines (79 loc) · 2.17 KB
/
04.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
76
77
78
79
80
81
82
83
84
85
86
87
88
/**
* https://adventofcode.com/2019/day/4
*/
type DigitCount = {[digit: number]: number};
/**
* Given a range of numbers, return the total number of possible passwords given the rules.
*/
export const countValidPassswords = (
min: number,
max: number,
isValidPassword: (number: number) => boolean
): number => {
let numPasswords = 0;
for (let password = min; password <= max; password += 1) {
numPasswords += isValidPassword(password) ? 1 : 0;
}
return numPasswords;
};
/**
* Split a password into an array of digits
*/
const digitize = (password: number): Array<number> =>
password
.toString()
.split('')
.map(d => parseInt(d, 10));
/**
* Returns true if each digit in the array is equal to or greater than the previous digit.
*/
const neverDecreases = (digits: Array<number>): boolean => {
return digits.every((d: number, index: number) => {
return index <= 0 || d >= digits[index - 1]!;
});
};
/**
* Count the number of occurences of each digit in the password
*/
const countDigits = (digits: Array<number>): DigitCount => {
return digits.reduce((counts, digit) => {
return {
...counts,
[digit]: (counts[digit] || 0) + 1,
};
}, {} as DigitCount);
};
/**
* Returns true if there is at least one digit that occurs multiple times
*/
const hasSomeMatchingDigits = (counts: DigitCount): boolean => {
return Object.values(counts).some(d => d >= 2);
};
/**
* Returns true if there is at least one digit that occurs exactly twice
*/
const hasSomeDigitPairts = (counts: DigitCount): boolean => {
return Object.values(counts).some(d => d === 2);
};
/**
* Determine if the given password is valid for the rules of part 1
*/
export const isValidPasswordPart1 = (password: number) => {
const digits = digitize(password);
return (
digits.length === 6 &&
neverDecreases(digits) &&
hasSomeMatchingDigits(countDigits(digits))
);
};
/**
* Determine if the given password is valid for the rules of part 2
*/
export const isValidPasswordPart2 = (password: number) => {
const digits = digitize(password);
return (
digits.length === 6 &&
neverDecreases(digits) &&
hasSomeDigitPairts(countDigits(digits))
);
};