-
Notifications
You must be signed in to change notification settings - Fork 0
/
day1.js
50 lines (45 loc) · 1.41 KB
/
day1.js
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
import { parseLinesFromFile } from './helpers.js';
function findMax(input) {
const elves = input.reduce((state, val) => {
if (val === '') {
state.max = Math.max(state.max, state.current);
state.current = 0;
return state;
}
state.current = state.current + parseInt(val, 10);
return state;
}, { max: 0, current: 0 });
return Math.max(elves.max, elves.current);
}
function adjustMax3(state) {
if (state.current > state.max[2]) {
state.max.push(state.current);
state.max.sort((a, b) => b - a);
state.max.splice(3);
}
state.current = 0;
}
function findMax3(input) {
const elves = input.reduce((state, val) => {
if (val === '') {
adjustMax3(state);
return state;
}
state.current = state.current + parseInt(val, 10);
return state;
}, { max: [0,0,0], current: 0 });
adjustMax3(elves);
return elves.max[0] + elves.max[1] + elves.max[2];
}
function solveProblem1(filename) {
const input = parseLinesFromFile(filename);
const calories = findMax(input);
console.log('max calories part 1', calories);
}
function solveProblem2(filename) {
const input = parseLinesFromFile(filename);
const calories = findMax3(input);
console.log('max calories part 2', calories);
}
solveProblem1('./day1.puzzle.txt');
solveProblem2('./day1.puzzle.txt');