-
Notifications
You must be signed in to change notification settings - Fork 0
/
01.ts
44 lines (40 loc) · 1.14 KB
/
01.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
import {loadData} from '../shared/utils';
/*
https://adventofcode.com/2020/day/1
*/
/**
* Given a list of numbers, find the two that sum to 2020 and return their product.
*/
export const part1 = (input: Array<number>): number => {
for (let a = 0; a < input.length; a += 1) {
for (let b = a; b < input.length; b += 1) {
if (input[a]! + input[b]! === 2020) {
return input[a]! * input[b]!;
}
}
}
/* istanbul ignore next */
return -1;
};
/**
* Given a list of numbers, find the three that sum to 2020 and return their product.
*/
export const part2 = (input: Array<number>): number => {
for (let a = 0; a < input.length; a += 1) {
for (let b = a; b < input.length; b += 1) {
for (let c = b; c < input.length; c += 1) {
if (input[a]! + input[b]! + input[c]! === 2020) {
return input[a]! * input[b]! * input[c]!;
}
}
}
}
/* istanbul ignore next */
return -1;
};
/**
* Parse the puzzle input file ready for processing
*/
/* istanbul ignore next */
export const parse = async (): Promise<Array<number>> =>
(await loadData(2020, 1)).split('\n').map(line => parseInt(line, 10));