Skip to content

Instantly share code, notes, and snippets.

@BiosBoy
Last active December 4, 2022 11:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BiosBoy/2e1597fda20f8fb9495fbb0c4311193a to your computer and use it in GitHub Desktop.
Save BiosBoy/2e1597fda20f8fb9495fbb0c4311193a to your computer and use it in GitHub Desktop.
const fs = require('fs');
const path = require('path');
// 2-4,6-8
// 2-3,4-5
// 5-7,7-9
// 2-8,3-7
// 6-6,4-6
// 2-6,4-8
const data = fs.readFileSync(path.join(__dirname, './input3.txt'), 'utf8');
const areas = data.split(/\r?\n/);
const game = (getCondition) => {
let sum = 0;
const createIdxAr = (value) => {
const firstIdx = value.match(/^\d+/i)[0];
const lastIdx = value.match(/\d+$/i)[0];
const result = [];
Array.from(Array(Number((lastIdx - firstIdx) + 1)).keys()).map(index => {
result.push(Number(firstIdx) + index);
});
return result;
};
areas.forEach(area => {
const firstZone = area.split(',')[0];
const secondZone = area.split(',')[1];
sum += getCondition(createIdxAr(firstZone), createIdxAr(secondZone));
});
return sum;
};
const conditionPart1 = (first, second) => {
if (first.length >= second.length) {
const isInitValueMatch = first[0] <= second[0];
const isFinalValueMatch = first[first.length - 1] >= second[second.length - 1];
return isInitValueMatch && isFinalValueMatch ? 1 : 0;
}
const isInitValueMatch = second[0] <= first[0];
const isFinalValueMatch = second[second.length - 1] >= first[first.length - 1];
return isInitValueMatch && isFinalValueMatch ? 1 : 0;
}
const conditionPart2 = (first, second) => {
let matches = 0;
first.forEach(item => {
if (second.some(idx => idx === item) && !matches) {
matches += 1;
}
})
return matches;
}
const part1 = game(conditionPart1);
const part2 = game(conditionPart2);
console.log(part1, part2, 'game1');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment