-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday05.ts
56 lines (44 loc) · 1.51 KB
/
day05.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
import { readFileSync } from "fs";
type Predicate = (input: string) => boolean;
function threeVowels(input: string): boolean {
const re = /a|e|i|o|u/g;
const result = [...input.matchAll(re)];
return result.length >= 3;
}
function twiceInARow(input: string): boolean {
// back reference to match to characters in a row
return input.match(/(.)\1/g) !== null;
}
function noForbiddenStrings(input: string): boolean {
return input.match(/ab|cd|pq|xy/g) === null;
}
function pairOfTwo(input: string): boolean {
return input.match(/(..).*\1/g) !== null;
}
function repeatSurround(input: string): boolean {
return input.match(/(.).\1/g) !== null;
}
function checkString(input: string, criteria: Predicate[]): boolean {
return criteria.map((p) => p(input)).reduce((a, b) => a && b, true);
}
function countNiceWords(words: string[], criteria: Predicate[]): number {
return words
.map((a) => (checkString(a, criteria) ? 1 : 0))
.reduce((x: number, y: number) => x + y, 0);
}
function readInput(input: string): string[] {
try {
const data = readFileSync(input, "utf8");
return data.split("\n");
} catch (err) {
console.log(err);
return [""];
}
}
export function day05(input: string): void {
const words = readInput(input);
const conditionsPart1 = [threeVowels, twiceInARow, noForbiddenStrings];
console.log("Part 1: " + countNiceWords(words, conditionsPart1));
const conditionsPart2 = [pairOfTwo, repeatSurround];
console.log("Part 2: " + countNiceWords(words, conditionsPart2));
}