-
Notifications
You must be signed in to change notification settings - Fork 0
/
day3.js
38 lines (33 loc) · 1.18 KB
/
day3.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
import { readFile } from './helpers.js';
// [['vJrwpWtwJgWr', 'hcsFMMfFFhFp'], ['etc', 'etc']]
const data = readFile(3)
.split('\n')
.map((rucksack) => [
rucksack.slice(0, rucksack.length / 2),
rucksack.slice(rucksack.length / 2),
]);
const go = (data) =>
data.reduce((total, incoming) => {
// Accept unlimited length of arrays to check intersection
const intersection = incoming
.map((rucksack) => rucksack.split(''))
.reduce((a, b) => a.filter((c) => b.includes(c)))[0];
return (
total +
// Compute the value based on char code
(intersection === intersection?.toUpperCase()
? intersection.charCodeAt() - 38
: intersection.charCodeAt() - 96)
);
}, 0);
// put every 3 items into an array, rejoin them
// [['vJrwpWtwJgWrhcsFMMfFFhFp', 'another', 'another'], ['etc', 'etc', 'etc]]
const data2 = data.reduce((acc, next, i) => {
return i % 3 === 0
? [...acc, [next.join('')]]
: [...acc.slice(0, -1), [...acc.slice(-1)[0], next.join('')]];
}, []);
const p1 = go(data);
console.log(p1);
const p2 = go(data2);
console.log(p2);