Day 2
This commit is contained in:
33
02/part1.js
Normal file
33
02/part1.js
Normal file
@@ -0,0 +1,33 @@
|
||||
const fs = require('fs')
|
||||
const readline = require('readline');
|
||||
|
||||
const readInterface = readline.createInterface({
|
||||
input: fs.createReadStream('input'),
|
||||
console: false
|
||||
});
|
||||
|
||||
let re = /^(\d+)-(\d+) (\w+): (\w+)$/i
|
||||
let valid = 0;
|
||||
|
||||
readInterface.on('line', function(line) {
|
||||
let result = re.exec(line);
|
||||
if (result == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
let min = Number(result[1]);
|
||||
let max = Number(result[2]);
|
||||
let match = result[3];
|
||||
let pass = result[4];
|
||||
|
||||
let count = 0;
|
||||
pass.split('').forEach(x => x == match ? count++ : null);
|
||||
|
||||
if (count >= min && count <= max) {
|
||||
valid++;
|
||||
};
|
||||
});
|
||||
|
||||
readInterface.on('close', function(line) {
|
||||
console.log(valid);
|
||||
});
|
30
02/part2.js
Normal file
30
02/part2.js
Normal file
@@ -0,0 +1,30 @@
|
||||
const fs = require('fs')
|
||||
const readline = require('readline');
|
||||
|
||||
const readInterface = readline.createInterface({
|
||||
input: fs.createReadStream('input'),
|
||||
console: false
|
||||
});
|
||||
|
||||
let re = /^(\d+)-(\d+) (\w+): (\w+)$/i
|
||||
let valid = 0;
|
||||
|
||||
readInterface.on('line', function(line) {
|
||||
let result = re.exec(line);
|
||||
if (result == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
let pos1 = Number(result[1]) - 1;
|
||||
let pos2 = Number(result[2]) - 1;
|
||||
let match = result[3];
|
||||
let pass = result[4];
|
||||
|
||||
if ((pass[pos1] == match && pass[pos2] != match) || (pass[pos1] != match && pass[pos2] == match)) {
|
||||
valid++;
|
||||
};
|
||||
});
|
||||
|
||||
readInterface.on('close', function(line) {
|
||||
console.log(valid);
|
||||
});
|
Reference in New Issue
Block a user