Commit 486511c0 authored by Philipp Rummelsberger's avatar Philipp Rummelsberger
Browse files

initial day 1 - 4

parent 869ee893
const importer = require('../utils/import');
const utils = require('../utils/utils');
function remap(input) {
return input.map(i => {
return i.split(' ').reduce((prev, curr) => {
return {
...prev,
[curr.split(':')[0]] : curr.split(':')[1],
};
}, {});
});
}
function validatePassports(input, required, optional) {
return input.map(element => {
const keys = Object.keys(element).filter(key => key !== '');
const requiredKeys = Object.keys(required);
return requiredKeys
.map(requiredKey => {
if (!keys.includes(requiredKey) || !required[requiredKey].args) { return false }
const current = required[requiredKey]
console.log('CURRENT: ', element[requiredKey], current.method(element[requiredKey], ...current.args));
return current.method(element[requiredKey], ...current.args)
});
})
}
function validateYear() {
const year = parseInt(arguments[0]);
const length = arguments[1];
const range = arguments[2];
return year.toString().length === length && utils.inRange(year, range[0], range[1]);
}
function validateHeight() {
const rawHeight = arguments[0];
const unit = rawHeight.includes('cm') ? 'cm' : 'in'
if (!rawHeight.includes(unit)) { return false }
const height = parseInt(rawHeight.replace(unit, ''));
const range = unit === 'cm' ? arguments[1] : arguments[2];
return utils.inRange(height, range[0], range[1]);
}
function validateEyeColor() {
const color = arguments[0];
const allowed = arguments[1];
return allowed.includes(color);
}
function validateHairColor() {
const color = arguments[0];
const reg = new RegExp('^#([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$')
return reg.test(color);
}
function validatePID() {
const pid = arguments[0];
return pid.length === 9;
}
/*
* byr (Birth Year) - four digits; at least 1920 and at most 2002.
iyr (Issue Year) - four digits; at least 2010 and at most 2020.
eyr (Expiration Year) - four digits; at least 2020 and at most 2030.
hgt (Height) - a number followed by either cm or in:
If cm, the number must be at least 150 and at most 193.
If in, the number must be at least 59 and at most 76.
hcl (Hair Color) - a # followed by exactly six characters 0-9 or a-f.
ecl (Eye Color) - exactly one of: amb blu brn gry grn hzl oth.
pid (Passport ID) - a nine-digit number, including leading zeroes.
cid (Country ID) - ignored, missing or not.
* */
async function exec() {
const requiredFields = {
'byr': {
args: [4, [1920, 2002]],
method: validateYear
},
'iyr': {
args: [4, [2010, 2020]],
method: validateYear
},
'eyr': {
args: [4, [2020, 2030]],
method: validateYear
},
'hgt': {
args: [[150, 193], [59, 76]],
method: validateHeight,
},
'ecl': {
args: [['amb', 'blu', 'brn', 'gry', 'grn', 'hzl', 'oth']],
method: validateEyeColor,
},
'hcl': {
args: [],
method: validateHairColor
},
'pid': {
args: [],
method: validatePID,
},
};
const optionalFields = [ 'cid' ];
const result = await importer.importInputAsArray('day_04/input.txt')
.then(result => result.split("\n\n").filter(res => !!res).map(res => res.replace(/(\r\n|\n|\r)/gm, " ")))
.then(result => remap(result))
.then(result => validatePassports(result, requiredFields, optionalFields))
.then(result => {
console.log('RESULT: ', result);
return result;
})
.then(result => result.filter(res => res.every(e => e)).length);
console.log('TADA ', result);
}
exec();
This diff is collapsed.
exports.importInputAsArray = async (filePath) => {
return new Promise((resolve, reject) => {
const fs = require('fs');
const path = require('path');
const result = fs.readFileSync(path.resolve(__dirname, filePath), 'utf8');
resolve(result);
})
}
exports.inRange = (x, min, max) => ((x-min) * (x-max) <= 0);
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment