-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2023_Day04.js
66 lines (58 loc) · 2.16 KB
/
2023_Day04.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
60
61
62
63
64
65
66
/**
* Script for Day 04
* Call script with expected runmode
* node 2023_Day04.js 0
* node 2023_Day04.js 1
*/
const utils = require( './utils.js' );
const inputLines = utils.getInputLines( '2023', '04' );
var cards = []; // { winningNums, cardNums }
solvePuzzle01 = () => {
if( cards.length == 0 ){ convertLinesToCards(); }
// Loop over cards and check the number of winning numbers; first gives 1 pt, after doubles
let totalScore = 0;
cards.forEach( ( card, i ) => {
var cardScore = 0;
for( num of card.cardNums ){
if( card.winningNums.includes( num ) ){
cardScore = ( cardScore == 0 ) ? 1 : cardScore * 2;
}
}
totalScore += cardScore;
} );
return totalScore;
}
solvePuzzle02 = () => {
if( cards.length == 0 ){ convertLinesToCards(); }
// Loop over cards and keep track of the numbers; each card of index 1 has the same score, thus we can multiply
let numCards = Array( inputLines.length ).fill( 1 );
cards.forEach( ( card, i ) => {
var nextCards = 0;
for( num of card.cardNums ){
if( card.winningNums.includes( num ) ){
nextCards++;
}
}
// Loop over additionally won next cards to add copies, equal to the number of cards of the current index
let numCardsCurrIndex = numCards[ i ];
for( let n = i+1, m = i+1+nextCards; n < m; n++ ){
numCards[ n ] += numCardsCurrIndex;
}
} );
// Answer is the sum of all available cards
return numCards.reduce( ( total, item ) => total + item );
}
const convertLinesToCards = () => {
// Card 1: 41 48 83 86 17 | 83 86 6 31 17 9 48 53
inputLines.forEach( ( line, i ) => {
let [ winningString, cardString ] = line.split( ': ' )[ 1 ].split( ' | ');
cards.push( {
winningNums: winningString.trim().split( /\s+/ ),
cardNums: cardString.trim().split( /\s+/ )
} );
} );
}
let startTime = utils.getCurrentTime();
console.log( 'Answer part 1: ' + solvePuzzle01() );
console.log( 'Answer part 2: ' + solvePuzzle02() );
console.log( 'Completed in', utils.getDuration( startTime ) );