-
Notifications
You must be signed in to change notification settings - Fork 0
/
04.ts
92 lines (80 loc) · 2.37 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
89
90
91
92
import {loadData} from '../shared/utils';
/*
https://adventofcode.com/2020/day/4
*/
type Passport = {[key: string]: string};
const validators: {[key: string]: (data: string) => boolean} = {
byr: (data: string) => {
const year = parseInt(data, 10);
return year >= 1920 && year <= 2002;
},
iyr: (data: string) => {
const year = parseInt(data, 10);
return year >= 2010 && year <= 2020;
},
eyr: (data: string) => {
const year = parseInt(data, 10);
return year >= 2020 && year <= 2030;
},
hgt: (data: string) => {
const height = /([0-9]*)(cm|in)/.exec(data);
/* istanbul ignore next */
if (!height) {
return false;
}
return (
(height[2] === 'cm' &&
parseInt(height[1]!, 10) >= 150 &&
parseInt(height[1]!, 10) <= 193) ||
(height[2] === 'in' &&
parseInt(height[1]!, 10) >= 59 &&
parseInt(height[1]!, 10) <= 76)
);
},
hcl: (data: string) => {
return /^\#[0-9a-f]{6}$/.test(data);
},
ecl: (data: string) => {
return ['amb', 'blu', 'brn', 'gry', 'grn', 'hzl', 'oth'].includes(data);
},
pid: (data: string) => {
return /^[0-9]{9}$/.test(data);
},
cid: () => {
return true;
},
};
const requiredFields = ['byr', 'iyr', 'eyr', 'hgt', 'hcl', 'ecl', 'pid'];
export const part1 = (input: Array<Passport>): number =>
input.filter(hasRequiredFields).length;
export const part2 = (input: Array<Passport>): any =>
input.filter(
passport => hasValidFields(passport) && hasRequiredFields(passport)
).length;
export const hasRequiredFields = (passport: Passport): boolean =>
requiredFields.every(f => Object.keys(passport).includes(f));
export const hasValidFields = (passport: Passport): boolean => {
return Object.keys(passport).every(field => {
return validators[field]!(passport[field]!);
});
};
/**
* Parse the puzzle input file ready for processing
*/
export const parsePassport = (input: string): Passport => {
const passport: Passport = {};
input
.trim()
.replace(/\n/g, ' ')
.split(' ')
.forEach(item => {
const bits = item.split(':');
passport[bits[0]!] = bits[1]!;
});
return passport;
};
export const parsePassports = (input: string): Array<Passport> =>
input.split('\n\n').map(parsePassport);
/* istanbul ignore next */
export const parse = async (): Promise<Array<Passport>> =>
parsePassports(await loadData(2020, 4));