Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const solvePart2 = (input) => {
- const data = parse(input);
- const modules = data.modules;
- const inputCycles = {};
- for (const input of data.rxInputs) {
- inputCycles[input] = {};
- for (const module of Object.values(modules)) {
- if (module.destinations.includes(input)) {
- inputCycles[input][module.label] = 1; // fix the off-by-one
- }
- }
- }
- const numberOfCyclesToDetermine = Object.values(inputCycles).reduce((sum, e) => sum + Object.keys(e).length, 0);
- let determinedCyclesCounter = 0;
- for (let cycle = 0; determinedCyclesCounter < numberOfCyclesToDetermine; cycle++) {
- const pulses = [
- {
- from: "button",
- to: "broadcaster",
- value: LOW_PULSE,
- },
- ];
- while (pulses.length > 0) {
- const pulse = pulses.shift(); // FIFO
- for (const input of data.rxInputs) {
- if (pulse.to == input && inputCycles[input][pulse.from] && pulse.value == HIGH_PULSE) {
- if (inputCycles[input][pulse.from] == 1) {
- inputCycles[input][pulse.from] += cycle;
- determinedCyclesCounter++;
- if (determinedCyclesCounter == numberOfCyclesToDetermine) {
- break;
- }
- }
- }
- }
- const module = modules[pulse.to];
- if (!module) {
- continue;
- }
- const newPulse = modulate(module, pulse);
- if (!newPulse) {
- continue;
- }
- for (const destination of module.destinations) {
- pulses.push({
- from: module.label,
- to: destination,
- value: newPulse,
- });
- }
- }
- }
- return Math.min(...Object.values(inputCycles).map((e) => Object.values(e).reduce((product, e) => product * e, 1)));
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement