Skip to content

Commit

Permalink
solved d24 p2 mathematically via SageMath
Browse files Browse the repository at this point in the history
  • Loading branch information
NullDev committed Dec 24, 2023
1 parent bc76cbd commit e179258
Showing 1 changed file with 35 additions and 7 deletions.
42 changes: 35 additions & 7 deletions 2023/Day_24/part_2.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,40 @@ const INPUT = String(fs.readFileSync(path.join(__dirname, "input.txt"))).trim().

const pStart = performance.now();

//
// YOUR CODE HERE
//
const result = "...";
// I aint gonna implement a Z3 Solver in JS, so I'm sending it to SageMath.

const pEnd = performance.now();
const [a1, a2] = INPUT[0].split(" @ ").map(x => x.split(", ").map(Number));
const [b1, b2] = INPUT[1].split(" @ ").map(x => x.split(", ").map(Number));
const [c1, c2] = INPUT[2].split(" @ ").map(x => x.split(", ").map(Number));

console.log("<DESCRIPTION>: " + result);
console.log(pEnd - pStart);
// We have a system of 9 equations and 9 variables, so we can just solve for x, y and z.

const sage = `
var('u v w x y z a b c')
eq1 = ${a1[0]} + ${a2[0]}*u == x + a*u
eq2 = ${a1[1]} + ${a2[1]}*u == y + b*u
eq3 = ${a1[2]} + ${a2[2]}*u == z + c*u
eq4 = ${b1[0]} + ${b2[0]}*v == x + a*v
eq5 = ${b1[1]} + ${b2[1]}*v == y + b*v
eq6 = ${b1[2]} + ${b2[2]}*v == z + c*v
eq7 = ${c1[0]} + ${c2[0]}*w == x + a*w
eq8 = ${c1[1]} + ${c2[1]}*w == y + b*w
eq9 = ${c1[2]} + ${c2[2]}*w == z + c*w
solve([eq1, eq2, eq3, eq4, eq5, eq6, eq7, eq8, eq9], u, v, w, x, y, z, a, b, c)
`.trim();

fetch("https://cocalc.com/api/v2/jupyter/execute", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
input: sage,
kernel: "sage-10.1",
}),
}).then(q => q.json()).then(q => q.output[0].data["text/plain"]).then(f => {
const res = (Number(f.match(/x == (\d+)/)[1]) || 0) + (Number(f.match(/y == (\d+)/)[1]) || 0) + (Number(f.match(/z == (\d+)/)[1]) || 0);

const pEnd = performance.now();

console.log("SUM OF COORDINATES: " + res);
console.log(pEnd - pStart);
});

0 comments on commit e179258

Please sign in to comment.