-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day1.js
41 lines (35 loc) · 1.04 KB
/
Day1.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
"use strict"
//https://adventofcode.com/2020/day/1
//load input
const fs = require('fs')
const encoded = fs.readFileSync("/Users/hakizu/Downloads/message.txt")
const input = encoded.toString('utf-8').split('\n')
//Part1
const toNumberInput = input.map(it => parseInt(it))
const sortedInput = toNumberInput.sort()
const targetValue = 2020
sortedInput.find(it => {
const neededValue = targetValue - it
if (sortedInput.includes(neededValue)) {
console.log(neededValue * it)
return true
}
})
//Part2
sortedInput.find(it => {
const sumOfNeededValues = targetValue - it
return findTwoRemainingInputs(sumOfNeededValues, it)
})
function findTwoRemainingInputs (targetValue, excludedValue) {
const wasSuccessful = sortedInput.find(it => {
if (excludedValue === it) {
return
}
const neededValue = targetValue - it
if (sortedInput.includes(neededValue)) {
console.log(excludedValue * neededValue * it)
return true
}
})
return wasSuccessful
}