scibuff

Advent of Code 2023 - Day 19 - Part 1

Dec 19th, 2023
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 0.68 KB | Source Code | 0 0
  1. const solvePart1 = (input) => {
  2.   const data = parse(input);
  3.   let sum = 0;
  4.   for (const part of data.parts) {
  5.     let next = "in";
  6.     while (next != "A" && next != "R") {
  7.       for (const flow of data.workflows[next]) {
  8.         if (!flow.condition) {
  9.           next = flow.next;
  10.           break;
  11.         }
  12.         if (flow.condition == "<" && part[flow.category] < flow.value) {
  13.           next = flow.next;
  14.           break;
  15.         } else if (flow.condition == ">" && part[flow.category] > flow.value) {
  16.           next = flow.next;
  17.           break;
  18.         }
  19.       }
  20.     }
  21.     if (next == "A") {
  22.       sum += part.x + part.m + part.a + part.s;
  23.     }
  24.   }
  25.   return sum;
  26. };
Advertisement
Add Comment
Please, Sign In to add comment