Last active
January 22, 2021 11:58
-
-
Save abuseofnotation/f44a5bcbaab577475b05e12b274b7535 to your computer and use it in GitHub Desktop.
Advent of code 2020 Day 1 - functional JS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
https://adventofcode.com/2020/day/1 | |
After saving Christmas five years in a row, you've decided to take a vacation at a nice resort on a tropical island. Surely, Christmas will go on without you. | |
The tropical island has its own currency and is entirely cash-only. The gold coins used there have a little picture of a starfish; the locals just call them stars. None of the currency exchanges seem to have heard of them, but somehow, you'll need to find fifty of these coins by the time you arrive so you can pay the deposit on your room. | |
To save your vacation, you need to get all fifty stars by December 25th. | |
Collect stars by solving puzzles. Two puzzles will be made available on each day in the Advent calendar; the second puzzle is unlocked when you complete the first. Each puzzle grants one star. Good luck! | |
Before you leave, the Elves in accounting just need you to fix your expense report (your puzzle input); apparently, something isn't quite adding up. | |
Specifically, they need you to find the two entries that sum to 2020 and then multiply those two numbers together. | |
For example, suppose your expense report contained the following: | |
1721 | |
979 | |
366 | |
299 | |
675 | |
*/ | |
const nums = [ | |
1721, | |
979, | |
366, | |
299, | |
675, | |
1456, | |
1010 | |
]; | |
// Accepts an array of numbers and one more number | |
// Returns an element of the array that amounts to 2020 when summed up with the number, if exist. | |
const amountsTo2020 = (arr, num) => arr.find((element) => element + num === 2020) | |
// Accepts a number (array index) and an array | |
// Returns a new array with the element corresponding with the index removed | |
const removeIndexFromArray = (i, arr) => arr.slice(0, i).concat(arr.slice(i + 1)); | |
// Flattens an array one level | |
const flatten = (arr) => arr.reduce((a, b) => a.concat(b)); | |
const make2020 = (nums) => { | |
const getPairsThatMake2020 = (firstNumber, firstNumberIndex) => { | |
// Get a version of an array without the current first number to avoid duplicates | |
const arrayWithoutFirstNumber = removeIndexFromArray(firstNumberIndex, nums); | |
// Retrieve the second number | |
const secondNumber = amountsTo2020(arrayWithoutFirstNumber, firstNumber); | |
//If the second number is present, return the pair | |
if (secondNumber !== undefined) { | |
return [{firstNumber, secondNumber}]; | |
} else { | |
return []; | |
} | |
} | |
// Can be done with `Array.flatMap` too | |
return flatten(nums.map(getPairsThatMake2020)); | |
} | |
console.log(make2020(nums)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment