day19 parsing done
This commit is contained in:
17
2023/day19/example.txt
Normal file
17
2023/day19/example.txt
Normal file
@ -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}
|
37
2023/day19/index.js
Normal file
37
2023/day19/index.js
Normal file
@ -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')
|
0
2023/day19/input.txt
Normal file
0
2023/day19/input.txt
Normal file
Reference in New Issue
Block a user