11.1, 11.2 python fast

This commit is contained in:
Fedor Lyanguzov
2024-12-11 17:03:58 +03:00
parent df8549d801
commit 3d08758cb8
2 changed files with 30 additions and 1 deletions
+2 -1
View File
@@ -6,7 +6,7 @@ data = [list(map(int, x)) for x in data]
def make_bound(data): def make_bound(data):
N = len(data) N = len(data)
M = len(data) M = len(data[0])
def bound(i, j): def bound(i, j):
return 0<=i<N and 0<=j<M return 0<=i<N and 0<=j<M
return bound return bound
@@ -23,6 +23,7 @@ def f1(data, collect=set):
yield from score(i+di, j+dj, x+1) yield from score(i+di, j+dj, x+1)
zeroes = [(i, j) for i, l in enumerate(data) for j, x in enumerate(l) if x==0] zeroes = [(i, j) for i, l in enumerate(data) for j, x in enumerate(l) if x==0]
bound = make_bound(data) bound = make_bound(data)
d = [(1,0), (0,1), (-1,0), (0,-1)] d = [(1,0), (0,1), (-1,0), (0,-1)]
return sum(1 for z in zeroes for _ in collect(score(*z))) return sum(1 for z in zeroes for _ in collect(score(*z)))
print(f1(data)) print(f1(data))
+28
View File
@@ -0,0 +1,28 @@
from collections import defaultdict
data = list(map(int, open("input.txt").read().strip().split()))
def rules(x):
if x==0:
return [1]
s = str(x)
if len(s)%2==0:
return [int(s[:len(s)//2]), int(s[len(s)//2:])]
return [x*2024]
def f(data, n):
d = defaultdict(int)
for x in data:
d[x] += 1
for i in range(n):
t = defaultdict(int)
for j, k in d.items():
for x in rules(j):
t[x] += k
d = t
return sum(d.values())
assert f(data, 25)==203609
print(f(data, 25))
print(f(data, 75))