Day 4: Scratchcards

I woke up very early this day and so did part one before work. I think I found it pretty easy and very enjoyable.

Did part two after work and if I remember correctly this one was pretty light and easy. Nice for a Monday after the difficult one on Sunday.

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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 const inputToUse = realInput; const scratchCards = []; const cardValues = []; var scratchCardSets = []; var inputLines = inputToUse.split(/\n/); inputLines.forEach(function(line){ const lineArr = line.split(':'); const lineNumbers = lineArr[1]; // console.log(lineNumbers); const numbersArr = lineNumbers.split('|'); const winCaseLine = numbersArr[0].trim(); const yourNumsLine = numbersArr[1].trim(); const winCaseNums = winCaseLine.split(/\s+/); const yourNums = yourNumsLine.split(/\s+/); const cardNums = [winCaseNums,yourNums]; const cardNumsCopies = [cardNums]; scratchCards.push(cardNums); scratchCardSets.push(cardNumsCopies); }); // console.log(scratchCards); // for part 1 scratchCards.forEach(function(card){ const winCaseNums = card[0]; const yourNums = card[1]; var winCount = 0; var cardValue = 0; yourNums.forEach(function(num) { var isAWinner = false; winCaseNums.forEach(function(winNum) { if( num === winNum ) { isAWinner = true; } }); if( isAWinner ) { winCount = winCount + 1; } }); // we now have a win count if( winCount > 0 ) { cardValue = 1; } for( var i=0; i<winCount-1; i++ ) { cardValue = cardValue * 2; } // console.log('cardValue',cardValue); cardValues.push(cardValue); }); var totalCardValue = 0; cardValues.forEach(function(value){ totalCardValue = totalCardValue + value; }); console.log('totalCardValue',totalCardValue); var buildingSets = scratchCardSets; // for part 2 scratchCardSets.forEach(function(cardSet,setNo){ const winCaseNums = cardSet[0][0]; const yourNums = cardSet[0][1]; var winCount = 0; const copyCount = cardSet.length; yourNums.forEach(function(num) { var isAWinner = false; winCaseNums.forEach(function(winNum) { if( num === winNum ) { isAWinner = true; } }); if( isAWinner ) { winCount = winCount + 1; } }); // we now have a win count // console.log('---'); // console.log(`card ${setNo}: ${copyCount} copies`); // console.log(`card ${setNo} has ${winCount} wins, and ${copyCount} copies, so we should gain ${copyCount} copies of the following ${winCount} cards`); // for each copy for( var i=0; i<copyCount; i++ ) { // for the following cards const nextSetNo = setNo + 1; for( var j=0; j<winCount; j++ ) { const targetSetNo = nextSetNo + j; if( targetSetNo < scratchCardSets.length ) { const setToDupOriginal = scratchCardSets[targetSetNo][0]; scratchCardSets[targetSetNo].push(setToDupOriginal); } } } }); // console.log('all scratchcards',scratchCardSets); var totalCardCount = 0; scratchCardSets.forEach(function(set) { const cardCount = set.length; totalCardCount = totalCardCount + cardCount; }); console.log('totalCardCount',totalCardCount);
All days / Day 5