Add 2020 day 1,2,3
This commit is contained in:
parent
243a4800d6
commit
8780290662
24
2020/day1/day1.js
Executable file
24
2020/day1/day1.js
Executable file
@ -0,0 +1,24 @@
|
||||
#!/bin/node
|
||||
|
||||
const fs = require('fs');
|
||||
|
||||
fs.readFile('input.txt', (err, data) => {
|
||||
const numbers = data.toString().split('\n').map(n => parseInt(n));
|
||||
for (let i = 0; i < numbers.length; i++) {
|
||||
for (let j = i + 1; j < numbers.length; j++) {
|
||||
if (numbers[i] + numbers[j] === 2020) {
|
||||
console.log(numbers[i] * numbers[j]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (let i = 0; i < numbers.length; i++) {
|
||||
for (let j = i + 1; j < numbers.length; j++) {
|
||||
for (let k = j + 1; k < numbers.length; k++) {
|
||||
if (numbers[i] + numbers[j] + numbers[k] === 2020) {
|
||||
console.log(numbers[i] * numbers[j] * numbers[k]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
29
2020/day2/day2.js
Executable file
29
2020/day2/day2.js
Executable file
@ -0,0 +1,29 @@
|
||||
#!/bin/node
|
||||
|
||||
const fs = require('fs');
|
||||
|
||||
fs.readFile('input.txt', (err, data) => {
|
||||
const passwords = data.toString().split('\n').filter(l => l.length > 0);
|
||||
let q1 = 0, q2 = 0;
|
||||
|
||||
for (const [format, password] of passwords.map(line => line.split(':'))) {
|
||||
const [occurrences, letter] = format.split(' ');
|
||||
const [min, max] = occurrences.split('-').map(n => parseInt(n));
|
||||
|
||||
console.log(min, max, letter, password);
|
||||
|
||||
// q1
|
||||
let count = 0;
|
||||
for (let j = 0; j < password.length; j++) {
|
||||
if (password[j] === letter) {
|
||||
count++;
|
||||
}
|
||||
}
|
||||
if (count >= min && count <= max) q1++;
|
||||
|
||||
// q2
|
||||
if (password[min] === letter && password[max] !== letter || password[min] !== letter && password[max] === letter) q2++;
|
||||
}
|
||||
|
||||
console.log(q1, q2);
|
||||
});
|
34
2020/day3/day3.js
Executable file
34
2020/day3/day3.js
Executable file
@ -0,0 +1,34 @@
|
||||
#!/bin/node
|
||||
|
||||
const fs = require('fs');
|
||||
|
||||
fs.readFile('input.txt', (err, data) => {
|
||||
const lines = data.toString().split('\n').filter(l => l.length > 0);
|
||||
width = lines[0].length;
|
||||
|
||||
const slopes = [
|
||||
[1, 1],
|
||||
[3, 1],
|
||||
[5, 1],
|
||||
[7, 1],
|
||||
[1, 2],
|
||||
];
|
||||
const positions = slopes.map(_ => 0);
|
||||
const results = slopes.map(_ => 0);
|
||||
|
||||
for (let i = 0; i < lines.length; i++) {
|
||||
const line = lines[i];
|
||||
|
||||
for (let j = 0; j < slopes.length; j++) {
|
||||
const [right, down] = slopes[j];
|
||||
|
||||
if (i % down === 0) {
|
||||
if (line[positions[j]] === '#') results[j]++;
|
||||
|
||||
positions[j] = (positions[j] + right) % width;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
console.log(results, results.reduce((a, n) => a * n, 1));
|
||||
});
|
Loading…
Reference in New Issue
Block a user