Last active
April 12, 2024 10:57
-
-
Save kissingboys/70230ff41e80cd198716e30cc63d43fd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let get = str => { | |
let amount = Number(str.match(/^\d*/)[0]) || 1; | |
return { | |
comps: str.matchAll(/([A-Z][a-z]?)(\d?)/g) | |
.toArray().map(([,c,a])=>({ c, a: Number(a) || 1 })), | |
amount | |
}; | |
} | |
let takes = args.join(" "); | |
let [left, right] = takes.replace(/\s/g, '') | |
.split("->").map(e=>e.split("+")); | |
let [left_val, right_val] = [left, right].map(re => { | |
let val = {}; | |
for (let thing of re) { | |
let res = get(thing); | |
res.comps.map(g => { | |
val[g.c] ??= 0 | |
val[g.c] += res.amount * g.a | |
}) | |
} | |
return Object.fromEntries(Object.entries(val).sort((a, b) => a[0].charCodeAt() - b[0].charCodeAt())); | |
}); | |
return ([left_val, right_val].map(e => JSON.stringify(e).replaceAll('"', '').replaceAll(/.(?=})|[{:,]/g, '$& ')).join(' -> ')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment