day 2
This commit is contained in:
parent
1d00f7c69e
commit
44376bf893
1000
day_2/input.txt
Normal file
1000
day_2/input.txt
Normal file
File diff suppressed because it is too large
Load diff
31
day_2/task_1.js
Normal file
31
day_2/task_1.js
Normal file
|
@ -0,0 +1,31 @@
|
|||
const fs = require('fs');
|
||||
const file = fs.readFileSync('./input.txt').toString('utf-8');
|
||||
const arr = file.split('\n');
|
||||
|
||||
let invalid = []
|
||||
|
||||
arr.forEach(item => {
|
||||
// Split the puzzle input
|
||||
let split = item.split(": ");
|
||||
let pw = split[1];
|
||||
|
||||
split = split[0].split(" ");
|
||||
let wantedLetter = split[1];
|
||||
|
||||
split = split[0].split("-");
|
||||
let [lowerNum, upperNum] = split;
|
||||
|
||||
// Check if the password is valid
|
||||
let count = 0;
|
||||
for (let letter of pw) {
|
||||
if (letter == wantedLetter) count++;
|
||||
}
|
||||
|
||||
if (count < lowerNum || count > upperNum) {
|
||||
invalid.push(pw);
|
||||
}
|
||||
});
|
||||
|
||||
console.log(`Total: ${arr.length}`);
|
||||
console.log(`Invalid: ${invalid.length}`);
|
||||
console.log(`Valid: ${arr.length - invalid.length}`);
|
26
day_2/task_2.js
Normal file
26
day_2/task_2.js
Normal file
|
@ -0,0 +1,26 @@
|
|||
const fs = require('fs');
|
||||
const file = fs.readFileSync('./input.txt').toString('utf-8');
|
||||
const arr = file.split('\n');
|
||||
|
||||
let invalid = []
|
||||
|
||||
arr.forEach(item => {
|
||||
// Split the puzzle input
|
||||
let split = item.split(": ");
|
||||
let pw = split[1];
|
||||
|
||||
split = split[0].split(" ");
|
||||
let wantedLetter = split[1];
|
||||
|
||||
split = split[0].split("-");
|
||||
let [firstPos, secondPos] = split;
|
||||
|
||||
// Check if the password is valid
|
||||
if (!(pw.charAt(Number(firstPos) - 1) == wantedLetter ^ pw.charAt(Number(secondPos) - 1) == wantedLetter)) {
|
||||
invalid.push(pw);
|
||||
}
|
||||
});
|
||||
|
||||
console.log(`Total: ${arr.length}`);
|
||||
console.log(`Invalid: ${invalid.length}`);
|
||||
console.log(`Valid: ${arr.length - invalid.length}`);
|
Loading…
Reference in a new issue