Day 6 & 7
This commit is contained in:
38
2024/05/index.ts
Normal file
38
2024/05/index.ts
Normal file
@@ -0,0 +1,38 @@
|
||||
const fs = require('fs');
|
||||
|
||||
const input = fs.readFileSync(__dirname + '/input.txt', 'utf8');
|
||||
|
||||
const rules: Array<[number, number]> = [];
|
||||
const updates: Array<Array<number>> = [];
|
||||
|
||||
let rulesSection = true;
|
||||
input.split("\n").forEach((line: string) => {
|
||||
if (!line) {
|
||||
rulesSection = false;
|
||||
return;
|
||||
}
|
||||
if (rulesSection) {
|
||||
rules.push(line.split('|').map((s) => parseInt(s)) as [number, number]);
|
||||
} else {
|
||||
updates.push(line.split(',').map((s) => parseInt(s)));
|
||||
}
|
||||
});
|
||||
|
||||
let result = 0;
|
||||
|
||||
updates.forEach((update) => {
|
||||
for (let rule of rules) {
|
||||
const minIndex = update.indexOf(rule[0]);
|
||||
const maxIndex = update.indexOf(rule[1]);
|
||||
if (
|
||||
minIndex !== -1
|
||||
&& maxIndex !== -1
|
||||
&& minIndex > maxIndex
|
||||
) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
result += update[(update.length - 1) / 2];
|
||||
});
|
||||
|
||||
console.log(result);
|
||||
Reference in New Issue
Block a user