diff --git a/2015/d10.py b/2015/d10.py new file mode 100644 index 0000000..2aa082b --- /dev/null +++ b/2015/d10.py @@ -0,0 +1,35 @@ +from functools import lru_cache +data = open(0).read().strip() + +part_1 = False +if part_1: + repeats = 40 +else: + repeats = 50 + +@lru_cache +def look_and_say(data: str) -> str: + r = "" + i = 0 + while i < len(data): + count = 0 + c = data[i] + while i < len(data) and data[i] == c: + count += 1 + i += 1 + r += f"{count}{c}" + return r + +CHUNK_SIZE = 10000 +for _ in range(repeats): + ndata = "" + lo, up = 0, CHUNK_SIZE + while up < len(data): + while up < len(data) and data[up - 1] == data[up]: + up += 1 + ndata += look_and_say(data[lo:up]) + lo, up = up, up + CHUNK_SIZE + ndata += look_and_say(data[lo:up]) + data = ndata + +print(len(data)) diff --git a/2015/d8.py b/2015/d8.py new file mode 100644 index 0000000..ad26a3a --- /dev/null +++ b/2015/d8.py @@ -0,0 +1,26 @@ +from lib import * + +data = open(0).read() +part_1 = False + +if part_1: + r1 = re.compile(r"\\x[0-9a-f][0-9a-f]") + r2 = re.compile(r"\\\\") + r3 = re.compile(r"\\\"") + + enc, mem = 0, 0 + for line in data.splitlines(): + mem += len(line) + line = r1.sub("^", line) + line = r2.sub("^", line) + line = r3.sub("^", line) + enc += len(line) - 2 + print(mem - enc) +else: + ori, enc = 0, 0 + for line in data.splitlines(): + ori += len(line) + line = line.replace("\\", "\\\\") + line = line.replace("\"", "\\\"") + enc += len(line) + 2 + print(enc - ori) diff --git a/2015/d9.py b/2015/d9.py new file mode 100644 index 0000000..e28fad9 --- /dev/null +++ b/2015/d9.py @@ -0,0 +1,42 @@ +from lib import * +from typing import Iterator + +# Just for fun instead of using itertools.permutations. +def permutations(xs) -> Iterator: + assert len(xs) > 0 + if len(xs) == 1: + yield xs + else: + x = xs.pop() + for p in permutations(xs): + for i in range(len(p) + 1): + pn = list(p) + pn.insert(i, x) + yield pn + +part_1 = False +data = open(0).read() + +dists = {} +nodes = set() +for line in data.splitlines(): + path, dist = line.split(" = ") + dist = int(dist) + a, b = path.split(" to ") + nodes.add(a) + nodes.add(b) + dists[(a, b)] = dist + dists[(b, a)] = dist + +if part_1: + mdist = 10**12 + for route in permutations(nodes): + dist = sum([dists[(route[i], route[i + 1])] for i in range(len(route) - 1)]) + mdist = min(dist, mdist) + print(mdist) +else: + mdist = 0 + for route in permutations(nodes): + dist = sum([dists[(route[i], route[i + 1])] for i in range(len(route) - 1)]) + mdist = max(dist, mdist) + print(mdist) diff --git a/README.md b/README.md index c578332..adc7515 100644 --- a/README.md +++ b/README.md @@ -12,4 +12,7 @@ written in Python. - Day 5: 14:05 o.O - Day 6: 15:00 - Day 7: 17:00 -- +- Day 8: 14:55 +- Day 9: 13:48 +- Day 10: 70:00 ... slow, but fun +- Day 11: