-
Notifications
You must be signed in to change notification settings - Fork 0
/
day1.js
36 lines (32 loc) · 845 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
const fs = require('fs');
const input = fs.readFileSync('./day1_data.txt', { encoding: 'utf8' }).trim();
const length = input.length;
function part1() {
let i = 0, sum = 0;
while (i < length) {
const nextIndex = (i + 1 == length) ? 0 : i + 1;
if (input.charAt(nextIndex) == input.charAt(i)) {
sum += Number(input.charAt(i));
}
i++;
}
return sum;
}
function part2() {
if (length % 2 != 0) throw new Error('Odd number of characters');
const half = length / 2;
let i = 0, sum = 0;
while (i < length) {
let nextIndex = i + half;
if (nextIndex >= length) {
nextIndex = nextIndex - length;
}
if (input.charAt(nextIndex) == input.charAt(i)) {
sum += Number(input.charAt(i));
}
i++;
}
return sum;
}
console.log('Part 1: %d', part1());
console.log('Part 2: %d', part2());