diff --git a/package-lock.json b/package-lock.json index fbd4f55..d052ed7 100644 --- a/package-lock.json +++ b/package-lock.json @@ -7,6 +7,9 @@ "": { "name": "aoc.emilydoesastro.com", "version": "0.0.1", + "dependencies": { + "nerdamer": "^1.1.13" + }, "devDependencies": { "@fontsource/fira-mono": "^4.5.0", "@neoconfetti/svelte": "^1.0.0", @@ -1114,6 +1117,14 @@ "node": "^10 || ^12 || ^13.7 || ^14 || >=15.0.1" } }, + "node_modules/nerdamer": { + "version": "1.1.13", + "resolved": "https://registry.npmjs.org/nerdamer/-/nerdamer-1.1.13.tgz", + "integrity": "sha512-kQGQYd42eQpKDOnU8ZnRKF47c+gK6jVC46eUchrABsovtFruHvsjyjBO32jck8QnpZE5z5R8HQw72hQX9Oq2MQ==", + "engines": { + "node": ">=0.10.0" + } + }, "node_modules/normalize-path": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/normalize-path/-/normalize-path-3.0.0.tgz", @@ -2413,6 +2424,11 @@ "integrity": "sha512-MqBkQh/OHTS2egovRtLk45wEyNXwF+cokD+1YPf9u5VfJiRdAiRwB2froX5Co9Rh20xs4siNPm8naNotSD6RBw==", "dev": true }, + "nerdamer": { + "version": "1.1.13", + "resolved": "https://registry.npmjs.org/nerdamer/-/nerdamer-1.1.13.tgz", + "integrity": "sha512-kQGQYd42eQpKDOnU8ZnRKF47c+gK6jVC46eUchrABsovtFruHvsjyjBO32jck8QnpZE5z5R8HQw72hQX9Oq2MQ==" + }, "normalize-path": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/normalize-path/-/normalize-path-3.0.0.tgz", diff --git a/package.json b/package.json index b008fb5..654ed84 100644 --- a/package.json +++ b/package.json @@ -26,5 +26,8 @@ "typescript": "^4.7.4", "vite": "^3.1.0" }, - "type": "module" + "type": "module", + "dependencies": { + "nerdamer": "^1.1.13" + } } diff --git a/src/routes/2022/day/21/+page.svelte b/src/routes/2022/day/21/+page.svelte index 5cb5196..28cde83 100644 --- a/src/routes/2022/day/21/+page.svelte +++ b/src/routes/2022/day/21/+page.svelte @@ -2,14 +2,14 @@ // The following should be edited for every puzzle, and includes // metadata on this page accessible across the whole site. export const myMetadata = { - title: "Puzzle Title", + title: "Monkey Math", day: "21", // Day, as string year: "2022", // Year, as string - description: "Puzzle description", + description: "Oh no, the monkeys are back! Now for a fun adventure in doing some math operations.", longRuntime: false, // Warning for if the page takes a while to run (> 1 second) - result1: "Result", // Text to display part 1 result with - result2: "Result", // Text to display part 2 result with - keywords: ["",], // General keywords about the code used + result1: "Value root will get", // Text to display part 1 result with + result2: "Required value to shout", // Text to display part 2 result with + keywords: ["nerdamer.js", "symbolic maths", "TODO: COMMENT"], // General keywords about the code used visible: true, // Whether or not to display in menus }; diff --git a/src/routes/2022/day/21/main.js b/src/routes/2022/day/21/main.js index 4d30d1b..d9baa7a 100644 --- a/src/routes/2022/day/21/main.js +++ b/src/routes/2022/day/21/main.js @@ -1,6 +1,7 @@ /* Primary functions for solving the puzzle live here. */ import { solveMonkeyJobs } from "./monkeys"; +import { solveSimultaneousEquation } from "./simultaneousEqn"; /** * Preprocessor applied to all possible inputs. @@ -27,5 +28,7 @@ export function part1 (preprocessedData) { * @returns {any} result of part 2! */ export function part2 (preprocessedData) { - return solveMonkeyJobs(preprocessedData, true); + const equation = solveMonkeyJobs(preprocessedData, true); + console.log("equation", equation); + return solveSimultaneousEquation(equation); }; \ No newline at end of file diff --git a/src/routes/2022/day/21/monkeys.js b/src/routes/2022/day/21/monkeys.js index 3cfe017..4b3746b 100644 --- a/src/routes/2022/day/21/monkeys.js +++ b/src/routes/2022/day/21/monkeys.js @@ -1,5 +1,18 @@ /* Functions for calculating*/ +import { countInstances } from "./simultaneousEqn"; + + +function addBrackets(expression) { + if (countInstances(expression[0], " ")) { + expression[0] = "( " + expression[0] + " )"; + } + if (countInstances(expression[2], " ")) { + expression[2] = "( " + expression[2] + " )"; + } + return expression.join(" "); +} + function applyOperator(unsolvedMonkey, withHumanVariable) { @@ -7,11 +20,11 @@ function applyOperator(unsolvedMonkey, withHumanVariable) { if (withHumanVariable && (typeof unsolvedMonkey.monkeyOne === "string" || typeof unsolvedMonkey.monkeyTwo === "string")) { - return [ + return addBrackets([ unsolvedMonkey.monkeyOne.toString(), unsolvedMonkey.operator, unsolvedMonkey.monkeyTwo.toString() - ].join(" "); + ]); } if (unsolvedMonkey.operator === "+") { diff --git a/src/routes/2022/day/21/simultaneousEqn.js b/src/routes/2022/day/21/simultaneousEqn.js index d54209d..c38828e 100644 --- a/src/routes/2022/day/21/simultaneousEqn.js +++ b/src/routes/2022/day/21/simultaneousEqn.js @@ -1,5 +1,10 @@ /* Functions for solving simultaneous equations */ +// I can't believe this is how I have to use this fucking module +import nerdamer from "nerdamer"; +import { solve } from "nerdamer/Solve"; +solve; + /** * https://stackoverflow.com/a/4009771/12709989 @@ -7,46 +12,35 @@ * @param {*} substring * @returns */ -function countInstances(string, substring) { +export function countInstances(string, substring) { return string.split(substring).length - 1; } -function applyOperator(expressionSplit, operator) { +// const OPERAND_MAPPING = { +// "/": (left, right) => left / right, +// "*": (left, right) => left * right, +// "+": (left, right) => left + right, +// "-": (left, right) => left - right, +// } - const newExpression = []; - // for (let i = 2; i < expressionSplit.length; i++) { - // const left = expressionSplit[i - 2]; - // const operator = expressionSplit[i - 1]; - // const right = expressionSplit[i]; +// function simplifyExpression(expression, result, variableName) { +// // Remove trailing/leading spaces +// expression = expression.trim(); - // if (operator === operator && left !== "humn" && right !== "humn") { - // if (operator === "/") { - // newExpression.push(left / right); - // } else if (operator === "*") { - // newExpression.push(left * right); - // } - // } - // } +// // Find first bracket instance +// const firstBracket = expression.indexOf("("); +// const lastBracket = expression.lastIndexOf(")"); -} +// // Handle if the entire expression is bracketted +// if (firstBracket === 0 && lastBracket === expression.length - 1) { +// // Handle if one side is not bracketed +// } else { -function simplifyExpression(expression) { - let expressionSplit = expression.split(" "); - - if (expressionSplit.length === 1) { - return expressionSplit[0]; - } - - expressionSplit = applyOperator(expressionSplit, "/"); - expressionSplit = applyOperator(expressionSplit, "*"); - expressionSplit = applyOperator(expressionSplit, "+"); - expressionSplit = applyOperator(expressionSplit, "-"); - - return expressionSplit; -} +// } +// } export function solveSimultaneousEquation(equation) { @@ -60,7 +54,7 @@ export function solveSimultaneousEquation(equation) { throw `Equation contains invalid number of equality operators '='`; } - const [leftSide, rightSide] = equation.split("="); - - + equation = equation.replaceAll(variable, "x"); + console.log(equation); + return nerdamer.solve(equation, "x"); } \ No newline at end of file