Parcourir la source

2016/10

master
Roderic Day il y a 4 ans
Parent
révision
584e09eaf4
3 fichiers modifiés avec 44 ajouts et 3 suppressions
  1. +16
    -1
      toolkit.py
  2. +2
    -2
      y2016/p09.py
  3. +26
    -0
      y2016/p10.py

+ 16
- 1
toolkit.py Voir le fichier

@@ -75,7 +75,22 @@ def md5gen(template, pattern=r'.+', batch=6000):

def interpret(string, globals):
fn, *args = (
x if x.replace('_', '').isalpha() else eval(x)
x if x[0].isalpha() else eval(x)
for x in string.split()
)
globals[fn](**dict(zip(*[iter(args)] * 2)))


def loop_consume(lines, handler):
instructions = collections.deque(lines)
count = 0
while instructions:
ok = handler(instructions[0])
if ok:
instructions.popleft()
count = 0
elif count < len(instructions):
instructions.rotate(1)
count += 1
else:
raise RuntimeError('Reached steady state')

+ 2
- 2
y2016/p09.py Voir le fichier

@@ -30,8 +30,8 @@ def unzip_len(text, recurse=False):

text = sys.stdin.read().strip()

# ans1 = unzip_len(text)
# print(ans1)
ans1 = unzip_len(text)
print(ans1)

ans2 = unzip_len(text, recurse=True)
print(ans2)

+ 26
- 0
y2016/p10.py Voir le fichier

@@ -0,0 +1,26 @@
import collections
import sys

import toolkit


def handle(string):
if string.startswith('value'):
_, value, _, _, t0, i0 = string.split()
regs[t0 + i0].append(int(value))
return True
elif string.startswith('bot'):
t0, i0, _, _, _, t1, i1, _, _, _, t2, i2 = string.split()
try:
lo, hi = sorted(regs[t0 + i0])
except ValueError:
return False
regs[t1 + i1].append(lo)
regs[t2 + i2].append(hi)
return True


regs = collections.defaultdict(list)
toolkit.loop_consume(sys.stdin.read().splitlines(), handle)
print(next(k for k, v in regs.items() if set(v) == {17, 61}))
print(regs['output0'][0] * regs['output1'][0] * regs['output2'][0])

Chargement…
Annuler
Enregistrer