Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const parseWorkflowRules = (s) => {
- const parts = s.split(",");
- const rules = [];
- for (const el of parts) {
- const m = el.match(/(a|m|s|x)(<|>)(\d+):(\w+)/);
- if (m) {
- rules.push({
- category: m[1],
- condition: m[2],
- value: parseInt(m[3]),
- next: m[4],
- });
- } else {
- rules.push({
- condition: null,
- next: el,
- });
- }
- }
- return rules;
- };
- const parseWorkflow = (line) => {
- const matches = line.match(/(\w+){(.*)}/);
- return {
- label: matches[1],
- rules: parseWorkflowRules(matches[2]),
- };
- };
- const parsePart = (line) => {
- return line.split(",").reduce((part, e) => {
- const m = e.match(/(x|m|a|s)=(\d+)/);
- part[m[1]] = parseInt(m[2]);
- return part;
- }, {});
- };
- const parse = (input) => {
- const sections = input.trim().split(/\r?\n\r?\n/);
- const lines1 = utils.getInputLines(sections[0]);
- const lines2 = utils.getInputLines(sections[1]);
- const workflows = {};
- for (const line of lines1) {
- const flow = parseWorkflow(line);
- workflows[flow.label] = flow.rules;
- }
- const parts = lines2.map(parsePart);
- return {
- parts: parts,
- workflows: workflows,
- };
- };
Advertisement
Add Comment
Please, Sign In to add comment