Advent of Code, Day 1: Report Repair
Go to Day 1 Challenge |
Go to index
I ran this code in the devtoolbar in Chrome, and the answers were simply outputted to the console.
Learn more about my solution.
Part I
Which 2 numbers, when combined, equal 2020? And what is their product?
var nums = window.document.getElementsByTagName("pre")[0]
nums = nums.innerText.split('\n')
nums.forEach(first => {
nums.forEach(second => {
if(Number(first) + Number(second) === 2020) {
console.warn(first, 'plus', second, 'equals 2020')
console.warn(first, 'times', second, 'equals', Number(first) * Number(second))
}
})
})
Part II
Which 3 numbers, when combined, equal 2020? And what is their product?
var nums = window.document.getElementsByTagName("pre")[0]
nums = nums.innerText.split('\n')
nums.forEach(first => {
nums.forEach(second => {
nums.forEach(third => {
if(Number(first) + Number(second) + Number(third) === 2020) {
console.warn(first, 'plus', second, 'plus', third, 'equals 2020')
console.warn(first, 'times', second, 'times', third, 'equals', Number(first) * Number(second) * Number(third))
}
})
})
})