-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday2.js
61 lines (50 loc) · 1.32 KB
/
day2.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
51
52
53
54
55
56
57
58
59
60
61
'use strict';
function getRows(str) {
return str.split(/\n/);
}
function cleanRow(row) {
return row.split(/\s+/).map(Number);
}
// wrap multi-line input in ``
function aoc2017d2p1(input) {
function getRowChecksum(row) {
var max, min;
row.forEach(function updateMaxMin(value) {
if (!max || value > max) {
max = value;
}
if (!min || value < min) {
min = value;
}
});
return max - min;
}
return getRows(input)
.map(cleanRow)
.reduce(function reducer(sum, row) {
return sum + getRowChecksum(row);
}, 0);
}
// wrap multi-line input in ``
function aoc2017d2p2(input) {
function sortHighestToLowest(row) {
return row.sort(function sorter(a, b) {
return b - a;
});
}
function getRowChecksum(input) {
var row = [...input];
var numerator = row.shift();
for (let i = 0; i < row.length; i++) {
if (numerator % row[i] === 0) {
return numerator / row[i];
}
}
return getRowChecksum(row);
}
return getRows(input)
.map(cleanRow)
.reduce(function reducer(sum, row) {
return sum + getRowChecksum(sortHighestToLowest(row));
}, 0);
}