-
Notifications
You must be signed in to change notification settings - Fork 0
/
day-4.js
45 lines (39 loc) · 1.27 KB
/
day-4.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
const readInputToArray = require('./inputHelper');
const exampleInputArr = readInputToArray('day-4-example');
const inputArr = readInputToArray('day-4');
// [ [winning, yours], ]
const formatGameCards = (inArr) => {
const gameArrays = inArr.map(game => {
const [, nums] = game.split(': ');
const [winning, yours] = nums.split(' | ');
return([winning.trim().split(' '), yours.trim().split(' ')]);
})
console.log(gameArrays);
return gameArrays;
}
const totalCardPoints = (card) => {
let numMatches = 0;
// count matches
const [winning, yours] = card;
yours.forEach((yourNum) => {
// exclude weird formatting thing where extra spaces got included
if (yourNum !== "" && winning.includes(yourNum)) {
numMatches ++;
}
})
// return points by # matches
if (numMatches < 2) {
return numMatches
} else {
return (Math.pow(2, numMatches -1))
}
}
const totalScratcherPoints = (inArr) => {
const allCards = formatGameCards(inArr);
let sumOfAllScratchers = allCards.reduce((acc, curr) => {
return acc + totalCardPoints(curr);
}, 0);
console.log(sumOfAllScratchers);
}
totalScratcherPoints(exampleInputArr); // 13
totalScratcherPoints(inputArr); // 94084