Solve 2015 days 8-10.
This commit is contained in:
35
2015/d10.py
Normal file
35
2015/d10.py
Normal file
@@ -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))
|
||||||
26
2015/d8.py
Normal file
26
2015/d8.py
Normal file
@@ -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)
|
||||||
42
2015/d9.py
Normal file
42
2015/d9.py
Normal file
@@ -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)
|
||||||
Reference in New Issue
Block a user