2020 day 9
This commit is contained in:
parent
04656699c9
commit
1590f4be6e
1000
2020/09/input.txt
Normal file
1000
2020/09/input.txt
Normal file
File diff suppressed because it is too large
Load diff
21
2020/09/part1.js
Normal file
21
2020/09/part1.js
Normal file
|
@ -0,0 +1,21 @@
|
|||
const fs = require('fs');
|
||||
const data = fs.readFileSync('input.txt', 'utf-8');
|
||||
const lines = data.split('\n').map(num => parseInt(num));
|
||||
const preambleLength = 25;
|
||||
|
||||
let first
|
||||
for (let i = preambleLength; i < lines.length; i++) {
|
||||
valid = false
|
||||
for (let j = i-preambleLength; j < i && !valid; j++) {
|
||||
for (let k = j+1; k < i && !valid; k++) {
|
||||
if (lines[j]+lines[k]== lines[i]) {
|
||||
valid = true
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!valid) {
|
||||
first = lines[i]
|
||||
break
|
||||
}
|
||||
}
|
||||
console.log(first)
|
30
2020/09/part2.js
Normal file
30
2020/09/part2.js
Normal file
|
@ -0,0 +1,30 @@
|
|||
const fs = require('fs');
|
||||
const data = fs.readFileSync('input.txt', 'utf-8');
|
||||
const lines = data.split('\n').map(num => parseInt(num));
|
||||
const preambleLength = 25;
|
||||
|
||||
let sum
|
||||
for (let i = preambleLength; i < lines.length; i++) {
|
||||
valid = false
|
||||
for (let j = i-preambleLength; j < i && !valid; j++) {
|
||||
for (let k = j+1; k < i && !valid; k++) {
|
||||
if (lines[j]+lines[k]== lines[i]) {
|
||||
valid = true
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!valid) {
|
||||
sum = lines[i]
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
for ( let start = 0; start < lines.length; start++) {
|
||||
for (let end = start+1; end < lines.length; end++) {
|
||||
if (lines[end] > sum) {continue}
|
||||
let range = lines.slice(start, end)
|
||||
if (range.reduce((a,b) => (a+b)) == sum) {
|
||||
console.log(Math.min(...range)+ Math.max(...range))
|
||||
}
|
||||
}
|
||||
}
|
|
@ -49,6 +49,7 @@ if (advent) {
|
|||
6. [:star: :star:](https://adventofcode.com/2020/day/6 "see puzzle")
|
||||
7. [:star: :star:](https://adventofcode.com/2020/day/7 "see puzzle")
|
||||
8. [:star: :star:](https://adventofcode.com/2020/day/8 "see puzzle")
|
||||
9. [:star: :star:](https://adventofcode.com/2020/day/9 "see puzzle")
|
||||
|
||||
## Languages Used
|
||||
* Python
|
||||
|
|
Loading…
Reference in a new issue