Browse Source

Test files

master
Roderic Day 1 year ago
parent
commit
b38515d8f6
3 changed files with 34 additions and 6 deletions
  1. +0
    -4
      toolkit.py
  2. +3
    -2
      y2023/p05.py
  3. +31
    -0
      y2023/p08.py

+ 0
- 4
toolkit.py View File

@@ -16,10 +16,6 @@ from pathlib import Path
product = functools.partial(functools.reduce, operator.mul)


def lcm(a, b):
return abs(a * b) // math.gcd(a, b)


def render(grid, brush=None):
if brush is None:
brush = {v: v for v in grid.values()}

+ 3
- 2
y2023/p05.py View File

@@ -26,9 +26,10 @@ print(ans2)

# old method, slow
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)
return n


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))

+ 31
- 0
y2023/p08.py View File

@@ -0,0 +1,31 @@
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))

Loading…
Cancel
Save