Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def part1(mod_config: dict, steps: int = 1000) -> int:
- ff_modules = {k: 0 for k, v in mod_config.items() if v[0] == "%"}
- c_modules_start = defaultdict(
- dict
- ) # keeps the start values of Conjunction modules before button push
- for c in (k for k, v in mod_config.items() if v[0] == "&"):
- for k, v in mod_config.items():
- if c in v[1]:
- c_modules_start[c].update({k: 0})
- pulses = defaultdict(int)
- rx_switches = defaultdict(set)
- c_modules = {} # here all states for C modules are kept in deque
- for idx in range(steps):
- deq = deque(["broadcaster"])
- while deq:
- current = deq.popleft()
- if current == "broadcaster":
- pulse = False
- pulses[pulse] += 1
- elif mod_config[current][0] == "%":
- pulse = ff_modules[current]
- else:
- # here I take the first state of the C module. If it's the last int the deque it's kept separetely
- cur_c_module = c_modules[current].popleft()
- if not c_modules[current]:
- c_modules_start[current] = cur_c_module
- pulse = not all(cur_c_module.values())
- for item in mod_config[current][1]:
- pulses[pulse] += 1
- if item not in mod_config:
- # print(f"output pulse is {pulse}")
- if item == "rx":
- if any(cur_c_module.values()):
- rx_switches[
- next(k for k, v in cur_c_module.items() if v)
- ].add(idx + 1)
- print(rx_switches)
- # if len(rx_switches) == 4:
- # return lcm(*set.union(*rx_switches.values()))
- continue
- if mod_config[item][0] == "%":
- if pulse:
- continue
- ff_modules[item] = not ff_modules[item]
- else:
- # here we update states of the C module. If this state is the first it is based on the last state before button push
- if c_modules.get(item, None):
- c_modules[item].append(c_modules[item][-1].copy())
- else:
- c_modules[item] = deque([c_modules_start[item]])
- c_modules[item][-1][current] = pulse
- deq.append(item)
- # print(f"{ff_modules=}, {c_modules=}")
- """
- for v in rx_switches.values():
- print([y - x for x, y in pairwise(sorted(v))])
- """
- return prod(pulses.values())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement