Browse Source

2016/14

master
Roderic Day 4 years ago
parent
commit
0dee7412c2
4 changed files with 111 additions and 13 deletions
  1. +7
    -13
      toolkit.py
  2. +53
    -0
      y2016/p12.py
  3. +27
    -0
      y2016/p13.py
  4. +24
    -0
      y2016/p14.py

+ 7
- 13
toolkit.py View File

import collections import collections
import hashlib
import itertools import itertools
import os import os
import re import re
import subprocess
import sys import sys
from pathlib import Path from pathlib import Path


Path(path).write_bytes(response.content) Path(path).write_bytes(response.content)




def md5gen(template, pattern=r'.+', batch=6000):
for i in itertools.count():
strings = (template.format(i=i * batch + k) for k in range(batch))
args = [c for s in strings for c in ['-s', s]]
out = subprocess.check_output(['md5'] + args).decode()
yield from re.findall(rf'"(.+)"\) = ({pattern})', out)
def batch(iterable, size):
count = itertools.count()
for _, sub in itertools.groupby(iterable, lambda _: next(count) // size):
yield sub




def interpret(string, globals):
fn, *args = (
x if x[0].isalpha() else eval(x)
for x in string.split()
)
globals[fn](**dict(zip(*[iter(args)] * 2)))
def md5(string):
return hashlib.md5(string.encode()).hexdigest()




def loop_consume(lines, handler): def loop_consume(lines, handler):

+ 53
- 0
y2016/p12.py View File

import sys


def get(x):
return regs[x] if x in regs else int(x)


def cpy(x, y):
regs[y] = get(x)


def inc(x):
regs[x] += 1


def dec(x):
regs[x] -= 1


def jnz(x, y):
if get(x) != 0:
return int(y)


def run(regs, lim=1000):
pos = 0
seen = {}
for _ in range(lim):
fn, *args = instructions[pos].split()
pos += eval(fn)(*args) or 1
if regs['d'] not in seen:
seen[regs['d']] = regs['a']
print(sorted(seen))


text = sys.stdin.read()
instructions = text.splitlines()

regs = {k: 0 for k in 'abcd'}
run(regs)
regs['c'] = 1
run(regs)


def fib(n):
a, b = 0, 1
for _ in range(n):
a, b = b, a + b
return b


print(fib(27) + 19 * 11)
print(fib(34) + 19 * 11)

+ 27
- 0
y2016/p13.py View File

import sys


def is_open(x, y):
n = x*x + 3*x + 2*x*y + y + y*y + inp
return f'{n:b}'.count('1') % 2 == 0


inp = int(sys.stdin.read())
valid = {
complex(x, y)
for y in range(50)
for x in range(50)
if is_open(x, y)
}
steps = (1, -1, 1j, -1j)
edge = {1 + 1j}
seen = set()
hop = 0
while 31 + 39j not in seen:
edge = {old + step for old in edge for step in steps} & valid - seen
seen |= edge
hop += 1
if hop == 50:
ans2 = len(seen)
print(hop)
print(ans2)

+ 24
- 0
y2016/p14.py View File

import re
import sys

import toolkit


text = sys.stdin.read().strip()
found = {i: toolkit.md5(f'{text}{i}') for i in range(30_000)}
print([
i
for i, dig in found.items()
for c in re.findall(r'(.)\1\1', dig)[:1]
if any(c * 5 in found.get(i + j + 1, '') for j in range(1000))
][63])

for _ in range(2016):
found = {k: toolkit.md5(v) for k, v in found.items()}

print([
i
for i, dig in found.items()
for c in re.findall(r'(.)\1\1', dig)[:1]
if any(c * 5 in found.get(i + j + 1, '') for j in range(1000))
][63])

Loading…
Cancel
Save