Skip to content

Commit

Permalink
Day 7
Browse files Browse the repository at this point in the history
  • Loading branch information
Av32000 committed Dec 7, 2024
1 parent 73ae2ca commit 507ab80
Show file tree
Hide file tree
Showing 2 changed files with 154 additions and 0 deletions.
76 changes: 76 additions & 0 deletions 07/part1.js
Original file line number Diff line number Diff line change
@@ -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);
78 changes: 78 additions & 0 deletions 07/part2.js
Original file line number Diff line number Diff line change
@@ -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);

0 comments on commit 507ab80

Please sign in to comment.