Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def day20(s, *, part2=False, num_buttons=1000):
- all_ch, outputs, flipflop, inputs = {}, {}, {}, collections.defaultdict(dict)
- for line in s.splitlines():
- ch, name, rhs = re.match(r'^(.)(\w+) -> (.*)$', line).groups()
- all_ch[name], outputs[name], flipflop[name] = ch, rhs.split(', '), 0
- for output in outputs[name]:
- inputs[output][name] = 0
- counts = collections.Counter()
- period = {name: 0 for name in inputs[next(iter(inputs['rx']))]} if part2 else {}
- for button_index in range(10**9 if part2 else num_buttons):
- counts[0] += 1
- pulses = collections.deque([('button', 0, 'broadcaster'[1:])])
- while pulses:
- source, value, name = pulses.popleft()
- if name in period and value == 0:
- period[name] = button_index + 1
- if all(period.values()):
- return math.lcm(*period.values())
- match all_ch[name]:
- case '%':
- if value == 1:
- continue
- value = flipflop[name] = 1 - flipflop[name]
- case '&':
- inputs[name][source] = value
- value = 1 - all(inputs[name].values())
- for output in outputs[name]:
- counts[value] += 1
- if output not in {'rx', 'output'}:
- pulses.append((name, value, output))
- return math.prod(counts.values())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement