From f2abf8c2a731cd3b3fc68b9e1dc5050178a34b13 Mon Sep 17 00:00:00 2001 From: TobenderZephyr Date: Sat, 21 Dec 2024 15:23:55 +0100 Subject: [PATCH] day19 parsing done --- 2023/day19/example.txt | 17 +++++++++++++++++ 2023/day19/index.js | 37 +++++++++++++++++++++++++++++++++++++ 2023/day19/input.txt | 0 3 files changed, 54 insertions(+) create mode 100644 2023/day19/example.txt create mode 100644 2023/day19/index.js create mode 100644 2023/day19/input.txt diff --git a/2023/day19/example.txt b/2023/day19/example.txt new file mode 100644 index 0000000..a08746e --- /dev/null +++ b/2023/day19/example.txt @@ -0,0 +1,17 @@ +px{a<2006:qkq,m>2090:A,rfg} +pv{a>1716:R,A} +lnx{m>1548:A,A} +rfg{s<537:gd,x>2440:R,A} +qs{s>3448:A,lnx} +qkq{x<1416:A,crn} +crn{x>2662:A,R} +in{s<1351:px,qqz} +qqz{s>2770:qs,m<1801:hdj,R} +gd{a>3333:R,R} +hdj{m>838:A,pv} + +{x=787,m=2655,a=1222,s=2876} +{x=1679,m=44,a=2067,s=496} +{x=2036,m=264,a=79,s=2244} +{x=2461,m=1339,a=466,s=291} +{x=2127,m=1623,a=2188,s=1013} \ No newline at end of file diff --git a/2023/day19/index.js b/2023/day19/index.js new file mode 100644 index 0000000..1b0035a --- /dev/null +++ b/2023/day19/index.js @@ -0,0 +1,37 @@ +import fs from 'node:fs' +import { json } from 'node:stream/consumers' + +function init(filename) { + const data = fs.readFileSync(filename, { encoding: 'utf-8' }, data => data) + const workflow = new Workflow() + let rules = data.split('\n\n')[0].split('\n') + rules.forEach(rule => { + rule.matchAll(/(.*)\{(.*),(\w+)\}/gm) + .forEach(r => { + r[2].split(',').forEach(condition => { + workflow.rules.push({ name: r[1], condition: condition, default: r[3] }) + }) + }) + }) + let flows = data.split('\n\n')[1].split('\n') + flows.forEach(flow => { + let newStr = flow.replace(/([a-zA-Z0-9]+)=([a-zA-Z0-9]+)/gm, "\"$1\": \"$2\"") + workflow.flows.push(JSON.parse(newStr)) + }) + workflow.test() +} + +class Workflow { + constructor() { + this.rules = [] + this.flows = [] + } + + test() { + + console.log(this.rules) + console.log(this.flows) + } +} + +init('example.txt') \ No newline at end of file diff --git a/2023/day19/input.txt b/2023/day19/input.txt new file mode 100644 index 0000000..e69de29