-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path01.ts
42 lines (38 loc) · 1.02 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
import {loadData} from '../shared/utils';
/*
https://adventofcode.com/2020/day/1
*/
/**
* Calculate the total fuel needed for a list of masses
*/
export const part1 = (input: Array<number>): number => {
return input.reduce((totalFuel, mass) => {
const fuel = Math.floor(mass / 3) - 2;
return totalFuel + fuel;
}, 0);
};
/**
* Calculate the total fuel needed for a list of masses,
* including the fuel needed for the mass of the fuel.
*/
export const part2 = (input: Array<number>): number => {
return input.reduce((sum, mass) => {
let totalFuel = 0;
let remainingMass = mass;
while (true) {
const fuel = Math.floor(remainingMass / 3) - 2;
if (fuel <= 0) {
break;
}
totalFuel += fuel;
remainingMass = fuel;
}
return sum + totalFuel;
}, 0);
};
/**
* Parse the puzzle input file ready for processing
*/
/* istanbul ignore next */
export const parse = async (): Promise<Array<number>> =>
(await loadData(2019, 1)).split('\n').map(line => parseInt(line, 10));