product = functools.partial(functools.reduce, operator.mul) | product = functools.partial(functools.reduce, operator.mul) | ||||
def lcm(a, b): | |||||
return abs(a * b) // math.gcd(a, b) | |||||
def render(grid, brush=None): | def render(grid, brush=None): | ||||
if brush is None: | if brush is None: | ||||
brush = {v: v for v in grid.values()} | brush = {v: v for v in grid.values()} |
# old method, slow | # old method, slow | ||||
def backward(n): | def backward(n): | ||||
for group in groups: | |||||
for group in groups[::-1]: | |||||
n = next((n - (b - a) for b, a, c in group if b <= n < b + c), n) | n = next((n - (b - a) for b, a, c in group if b <= n < b + c), n) | ||||
return n | return n | ||||
pairs = [(a, a + b) for a, b in zip(seed[::2], seed[1::2])] | pairs = [(a, a + b) for a, b in zip(seed[::2], seed[1::2])] | ||||
next(n for n in itertools.count(ans2) if any(a <= backward(n) < a + b for a, b in pairs)) | |||||
next(n for n in itertools.count(ans2) if any(a <= backward(n) < b for a, b in pairs)) |
import math | |||||
import collections | |||||
import itertools | |||||
import re | |||||
text = open(0).read() | |||||
head, tail = text.split('\n\n') | |||||
mapping = {} | |||||
for line in tail.splitlines(): | |||||
aa, bb, cc = re.findall(r'[\dA-Z]{3}', line) | |||||
mapping[aa] = [bb, cc] | |||||
pos = 'AAA' | |||||
steps = itertools.cycle(map('LR'.index, head)) | |||||
for ans1 in itertools.count(1): | |||||
pos = mapping[pos][next(steps)] | |||||
if pos == 'ZZZ': | |||||
break | |||||
print(ans1) | |||||
state = [p for p in mapping if p.endswith('A')] | |||||
seen = collections.defaultdict(collections.Counter) | |||||
steps = itertools.cycle(enumerate(map('LR'.index, head))) | |||||
for _ in range(100_000): | |||||
i, step = next(steps) | |||||
state = [mapping[pos][step] for pos in state] | |||||
for j, pos in enumerate(state): | |||||
seen[j][i, pos] += 1 | |||||
ns = [sum(v > 1 for v in c.values()) for c in seen.values()] | |||||
print(math.lcm(*ns)) |