Update 2015 solutions

This commit is contained in:
2024-10-20 15:19:25 -04:00
parent 87ab42743e
commit e73fa3bae7
16 changed files with 362 additions and 411 deletions

View File

@@ -1,35 +1,29 @@
from functools import lru_cache
data = open(0).read().strip()
from lib import get_data
part_1 = False
if part_1:
repeats = 40
else:
repeats = 50
data = get_data(__file__)
@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:
def iter(xs):
if not xs:
return []
grouped = []
current_element = xs[0]
count = 1
for element in xs[1:]:
if element == current_element:
count += 1
i += 1
r += f"{count}{c}"
return r
else:
grouped.append(count)
grouped.append(current_element)
current_element = element
count = 1
grouped.append(count)
grouped.append(current_element)
return grouped
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))
for repeat in [40, 50]:
xs = list(map(int, list(data.strip())))
for _ in range(repeat):
xs = iter(xs)
print(len(xs))