{{ message }}

Instantly share code, notes, and snippets.

# SvenWritesCode/woohoo.js

Created Dec 10, 2020
advent of code 2020 day 9
 // get the full transmitted data to test const fullTransmission = `` .split("\n") .map((input) => parseInt(input)); // This is the number used to generate a sliding window to use as possible inputs const PREAMBLE_LENGTH = 25; // Sum to the goal number using only the possible operands function sumTo(goal, possibleOperands) { // return the operands when two operands can combine to the goal number return possibleOperands.find((leftOperand) => possibleOperands.some((rightOperand) => leftOperand + rightOperand === goal) ); } // Create a window of size from an array function* slidingWindow(array, size) { let startIndex = 0; while (startIndex + size < array.length) { yield array.slice(startIndex, startIndex + size); startIndex++; } } // part 1 function getNonSummableNumber(tape, windowLength) { // generator to give a window of the window length plus one to get the first number of the next window const lengthPlusOne = slidingWindow(tape, windowLength + 1); // for each view generated, extract the first number of the next window and see if the number can be summed to for (let view of lengthPlusOne) { // remove the last number to get the original window length const testNumber = view.splice(view.length - 1, 1); // if any number in the view fails to combine with another number in the view to equal the test number, return that number if (!sumTo(testNumber, view)) { return testNumber; } } } // part 2 function getSumContiguousNumbersToTarget(target) { const possibleWins = []; // check over all numbers for (let j = 0; j < fullTransmission.length; j++) { const number = fullTransmission[j]; // for each number, add it to the previous numbers, and check if reached the target for (let k = 0; k < possibleWins.length; k++) { const win = possibleWins[k]; if (win && win.numbers && win.total) { // keep track of previous numbers and total for each previous number win.numbers.push(number); win.total += number; // woo hoo we reached the target! if (win.total === target) { // return the sum of the min and max to get the sum return Math.min(...win.numbers) + Math.max(...win.numbers); } } } possibleWins.push({ numbers: [number], total: number }); } } const nonSummableNumber = getNonSummableNumber(fullTransmission, PREAMBLE_LENGTH); const contiguousSum = getSumContiguousNumbersToTarget(nonSummableNumber); console.log(`You've done great, the non summable number found, \${nonSummableNumber} is awesome, and the sum you found \${contiguousSum} is also great!`);

### RusseII commented Dec 10, 2020

 Wow. AMAZING. MUCH Faster than my solution of `x.forEach(o=>{x.forEach(c=>{x.forEach(a=>{o+c+a===2020&&console.log(o,c,a)})})});`

### RusseII commented Dec 10, 2020

 What was the point of using a generator here?

### SvenWritesCode commented Dec 10, 2020

 I don't think there is one? I haven't tested anything. I just thought a sliding window would be a fun use of one.

### KevinBatdorf commented Dec 11, 2020

 I like the sliding window idea. My part 1 isn't very efficient. Oh well :D We took a similar approach for part two, but I used `.some` instead of a for loop. https://github.com/KevinBatdorf/advent-2020-javascript-challenges/blob/master/day9/day9.js