-
Notifications
You must be signed in to change notification settings - Fork 0
/
day1.js
36 lines (30 loc) · 935 Bytes
/
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
'use strict';
// make sure to pass input as a string because the real puzzle input is a large enough number that JS treats it as Infinity
function aoc2017d1p1(input) {
var chars = [...String(input)];
if (chars.length === 0) {
return 0;
}
chars.push(chars[0]);
return chars.reduce(function reducer(sum, current, index, array) {
var next = array[index + 1];
if (next && current === next) {
return sum + Number(current);
}
return sum;
}, 0);
}
function aoc2017d1p2(input) {
var chars = [...String(input)];
if (chars.length === 0) {
return 0;
}
var steps = chars.length / 2;
return chars.reduce(function reducer(sum, current, index, array) {
var next = array[(index + steps) % array.length];
if (next && current === next) {
return sum + Number(current);
}
return sum;
}, 0);
}