3_2.js 801 B

1234567891011121314151617181920212223242526272829303132
  1. import { loadInput } from "../common.js";
  2. // const input = loadInput("./3/test_2.input", parseInput, true);
  3. const input = loadInput(3, parseInput);
  4. solve(input);
  5. /**
  6. * @param {string} memDump
  7. */
  8. function parseInput(memDump) {
  9. return Array.from(memDump.matchAll(/(?:(do(?:n't)?\(\))|(mul\(\d{1,3},\d{1,3}\)))/g));
  10. }
  11. function solve(validInstructions) {
  12. let sum = 0;
  13. let enabled = true;
  14. for(const match of validInstructions) {
  15. const instruction = match[0];
  16. if (instruction == "do()") { enabled = true; continue; }
  17. else if (instruction == "don't()") { enabled = false; continue; }
  18. if (enabled) {
  19. sum += eval(instruction);
  20. }
  21. }
  22. console.log("Sum of result of valid instructions:", sum);
  23. }
  24. function mul(a,b) { return a*b; }