Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const LOW_PULSE = "low";
- const HIGH_PULSE = "high";
- const parse = (input) => {
- const lines = utils.getInputLines(input);
- const modules = {};
- const inputMap = {};
- const rxInputs = [];
- for (let line of lines) {
- const parts = line.split("->");
- const label = parts[0].trim() == "broadcaster" ? "broadcaster" : parts[0].trim().substring(1);
- const type = parts[0].trim().charAt(0);
- const destinations = parts[1].trim().split(", ");
- if (destinations.includes("rx")) {
- rxInputs.push(label);
- }
- for (const d of destinations) {
- if (!inputMap[d]) {
- inputMap[d] = [];
- }
- inputMap[d].push(label);
- }
- modules[label] = {
- label: label,
- type: type,
- destinations: destinations,
- };
- if (type == "%") {
- modules[label].on = false;
- }
- }
- for (const label of Object.keys(inputMap)) {
- const module = modules[label];
- if (module?.type == "&") {
- module.inputs = inputMap[label].reduce((o, label) => {
- o[label] = LOW_PULSE;
- return o;
- }, {});
- }
- }
- return {
- modules: modules,
- rxInputs: rxInputs,
- };
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement