day19 parsing done
This commit is contained in:
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')
|
Reference in New Issue
Block a user