From 507ab8023372aa8f6b1f70f196cb2a4d1bda1a5d Mon Sep 17 00:00:00 2001 From: Av32000 Date: Sat, 7 Dec 2024 16:11:23 +0100 Subject: [PATCH] Day 7 --- 07/part1.js | 76 +++++++++++++++++++++++++++++++++++++++++++++++++++ 07/part2.js | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 154 insertions(+) create mode 100644 07/part1.js create mode 100644 07/part2.js diff --git a/07/part1.js b/07/part1.js new file mode 100644 index 0000000..8375455 --- /dev/null +++ b/07/part1.js @@ -0,0 +1,76 @@ +const { readFileSync } = require("fs"); +// Parse Input +const inputFile = "input.txt"; + +let equations = []; +const input = readFileSync(inputFile).toString(); +input.split("\n").forEach((line) => { + if (line.split(":").length > 1) { + equations.push({ + target: parseInt(line.split(":")[0]), + numbers: line + .split(":")[1] + .trim() + .split(" ") + .map((x) => parseInt(x)), + operators: [], + isValid: false, + }); + } +}); + +function evaluateEquation(equation) { + let result = equation.numbers[0]; + equation.numbers.forEach((n, i) => { + if (i != 0) { + let operator = equation.operators[i - 1]; + if (operator == "+") { + result += n; + } else { + result *= n; + } + } + }); + + equation.isValid = result == equation.target; +} + +function convertPaternToOperators(pattern) { + return pattern + .toString(2) + .split("") + .slice(1) + .map((x) => (x == 1 ? "*" : "+")); +} + +function bruteforceEquation(equation) { + let operatorsCount = equation.numbers.length - 1; + let currentPattern = 1 << operatorsCount; + + let currentOffset = 0; + let maxOffset = 2 ** operatorsCount; + + while (currentOffset <= maxOffset) { + if (!equation.isValid) { + currentPattern = (1 << operatorsCount) + currentOffset; + equation.operators = convertPaternToOperators(currentPattern); + evaluateEquation(equation); + if (equation.isValid) break; + else currentOffset++; + } + } + + return equation; +} + +let result = 0; + +equations.forEach((e) => { + let parsed = bruteforceEquation(e); + + if (parsed.isValid) { + result += parsed.target; + } +}); + +console.log(result); diff --git a/07/part2.js b/07/part2.js new file mode 100644 index 0000000..8700cea --- /dev/null +++ b/07/part2.js @@ -0,0 +1,78 @@ +const { readFileSync } = require("fs"); +// Parse Input +const inputFile = "input.txt"; + +let equations = []; +const input = readFileSync(inputFile).toString(); +input.split("\n").forEach((line) => { + if (line.split(":").length > 1) { + equations.push({ + target: parseInt(line.split(":")[0]), + numbers: line + .split(":")[1] + .trim() + .split(" ") + .map((x) => parseInt(x)), + operators: [], + isValid: false, + }); + } +}); + +function evaluateEquation(equation) { + let result = equation.numbers[0]; + equation.numbers.forEach((n, i) => { + if (i != 0) { + let operator = equation.operators[i - 1]; + if (operator == "+") { + result += n; + } else if (operator == "*") { + result *= n; + } else { + result = parseInt(`${result}${n}`); + } + } + }); + + equation.isValid = result == equation.target; +} + +function bruteforceEquation(equation) { + let operatorsCount = equation.numbers.length - 1; + let operators = ["+", "*", "||"]; + + equation.operators = new Array(operatorsCount).fill("+"); + + while (true) { + evaluateEquation(equation); + if (equation.isValid) break; + + for (let i = operatorsCount - 1; i >= 0; i--) { + let currentIndex = operators.indexOf(equation.operators[i]); + if (currentIndex < operators.length - 1) { + equation.operators[i] = operators[currentIndex + 1]; + break; + } else { + equation.operators[i] = operators[0]; + if (i === 0) return equation; + } + } + } + + return equation; +} + +let result = 0; + +equations.forEach((e, i) => { + let parsed = bruteforceEquation(e); + process.stdout.write( + `Parsed : ${i + 1}/${equations.length}. Current Result : ${result}\r`, + ); + + if (parsed.isValid) { + result += parsed.target; + } +}); + +console.log("\n", result);