-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpuzzle-2-alt.js
59 lines (50 loc) · 1.54 KB
/
puzzle-2-alt.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
54
55
56
57
58
59
const markNumber = (bingoNumber, card) => {
for (let rowIndex = 0; rowIndex < card.length; rowIndex++) {
const row = card[rowIndex];
for (let colIndex = 0; colIndex < row.length; colIndex++) {
const num = row[colIndex];
if (num === bingoNumber) {
card[rowIndex][colIndex] = "X";
return;
}
}
}
};
const hasBingo = (card) => {
const gridSize = card.length;
for (let index = 0; index < gridSize; index++) {
const isRowBingo = card[index].every((num) => num === "X");
const isColBingo = card.every((row) => row[index] === "X");
if (isRowBingo || isColBingo) {
return true;
}
}
};
const output = (input) => {
const groups = input.split("\n\n");
const bingoNumbers = groups.splice(0, 1)[0].split(",");
const bingoCards = groups.map((card) =>
card.split("\n").map((row) => row.match(/.{1,3}/g).map((num) => num.trim()))
);
let winningCardCount = 0;
let remainingCards = bingoCards;
for (const bingoNumber of bingoNumbers) {
for (const [index, bingoCard] of remainingCards.entries()) {
if (!bingoCard) {
continue;
}
markNumber(bingoNumber, bingoCard);
if (hasBingo(bingoCard)) {
delete remainingCards[index];
winningCardCount++;
if (winningCardCount === remainingCards.length) {
const sumOfUnmarked = bingoCard
.flat()
.reduce((acc, cur) => acc + (cur !== "X" ? parseInt(cur) : 0), 0);
return sumOfUnmarked * bingoNumber;
}
}
}
}
};
module.exports = output;