From bab8ca01e9ebd014092dc2e7b85bcbc1990d2186 Mon Sep 17 00:00:00 2001 From: Av32000 Date: Fri, 6 Dec 2024 15:23:54 +0100 Subject: [PATCH] Day 5 --- 05/part1.js | 49 +++++++++++++++++++++++++++++++++++++ 05/part2.js | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 119 insertions(+) create mode 100644 05/part1.js create mode 100644 05/part2.js diff --git a/05/part1.js b/05/part1.js new file mode 100644 index 0000000..49f2136 --- /dev/null +++ b/05/part1.js @@ -0,0 +1,49 @@ +const { readFileSync } = require("fs"); +// Parse Input +const inputFile = "input.txt"; +const input = readFileSync(inputFile).toString(); +const rules = []; +const updates = []; + +input.split("\n").forEach((line) => { + if (line.includes("|")) { + rules.push(line.split("|").map((x) => parseInt(x))); + } else if (line.includes(",")) { + updates.push(line.split(",").map((x) => parseInt(x))); + } +}); + +function checkRule(rule, update) { + const n1 = update.indexOf(rule[0]); + const n2 = update.indexOf(rule[1]); + + return n1 == -1 || n2 == -1 || n1 < n2; +} + +function findRulesForNumber(number) { + const result = []; + rules.forEach((r) => { + if (r.includes(number)) result.push(r); + }); + + return result; +} + +const valid = []; +updates.forEach((u) => { + let isValid = true; + u.forEach((n) => { + findRulesForNumber(n).forEach((r) => { + if (!checkRule(r, u)) isValid = false; + }); + }); + + if (isValid) valid.push(u); +}); + +let result = 0; +valid.forEach((u) => { + result += u[(u.length / 2) >> 0]; +}); + +console.log(result); diff --git a/05/part2.js b/05/part2.js new file mode 100644 index 0000000..d84c5e9 --- /dev/null +++ b/05/part2.js @@ -0,0 +1,70 @@ +const { readFileSync } = require("fs"); +// Parse Input +const inputFile = "input.txt"; +const input = readFileSync(inputFile).toString(); +const rules = []; +const updates = []; + +input.split("\n").forEach((line) => { + if (line.includes("|")) { + rules.push(line.split("|").map((x) => parseInt(x))); + } else if (line.includes(",")) { + updates.push(line.split(",").map((x) => parseInt(x))); + } +}); + +function applyRule(rule, update) { + const n1 = update.indexOf(rule[0]); + const n2 = update.indexOf(rule[1]); + + if (n1 == -1 || n2 == -1 || n1 < n2) { + return; + } + + let newUpdate = update; + let temp = newUpdate[n2]; + newUpdate[n2] = newUpdate[n1]; + newUpdate[n1] = temp; + + return newUpdate; +} + +function findRulesForNumber(number) { + const result = []; + rules.forEach((r) => { + if (r.includes(number)) result.push(r); + }); + + return result; +} + +const invalid = []; +updates.forEach((u) => { + let currentUpdate = u; + let updated = false; + let modified = true; + while (modified) { + modified = false; + currentUpdate.forEach((n) => { + findRulesForNumber(n).forEach((r) => { + const fixed = applyRule(r, currentUpdate); + if (fixed) { + currentUpdate = fixed; + updated = true; + modified = true; + } + }); + }); + } + + if (updated) { + invalid.push(currentUpdate); + } +}); + +let result = 0; +invalid.forEach((u) => { + result += u[(u.length / 2) >> 0]; +}); + +console.log(result);