-
Notifications
You must be signed in to change notification settings - Fork 0
/
day-4-part-2.js
53 lines (44 loc) · 1.53 KB
/
day-4-part-2.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
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 numMatches;
}
const totalScratcherPoints = (inArr) => {
const allCards = formatGameCards(inArr);
const cardCount = allCards.map(c => 1);
for (let i=0; i<allCards.length; i++) {
const extraCards = totalCardPoints(allCards[i])
for (let x=(Math.min(i+1, allCards.length - 1)); x<= (Math.min(i+extraCards, allCards.length - 1)); x++) {
if (extraCards) {
cardCount[x] +=(1*cardCount[i]);
}
}
}
console.log(cardCount);
let sumOfAllScratchers = cardCount.reduce((acc, curr) => {
return acc + curr;
}, 0);
console.log(sumOfAllScratchers);
}
totalScratcherPoints(exampleInputArr); // 30
totalScratcherPoints(inputArr); // 6283755