-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
154 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |