Compare commits

..

5 Commits

Author SHA1 Message Date
e58ddb9f96 day 8, generators are faster (2x) 2025-09-08 16:38:35 +02:00
d165384617 day 7, saved by Counter 2025-09-08 01:46:10 +02:00
6fe70f851f day 14, use rotation, proper cycle detection, merge both parts, enable codon 2023-12-16 02:21:09 +01:00
a4e02c42a7 day 15, factor both parts 2023-12-16 00:26:27 +01:00
032f0c4caa day 14, 1E9 is not more than 1E3 2023-12-15 23:58:40 +01:00
7 changed files with 192 additions and 13 deletions

56
d07/part1.py Normal file
View File

@@ -0,0 +1,56 @@
import sys
from collections import Counter as C
from functools import cmp_to_key as ctk
def score(h:str) -> int: # get type of a hand
D = C(h)
E = C(v for k,v in D.items())
if 5 in E: # five of a kind
return 7
elif 4 in E: # four of a kind
return 6
elif 3 in E and 2 in E: # full house
return 5
elif 3 in E: # three of a kind
return 4
elif 2 in E and E[2] == 2: # two pairs
return 3
elif 2 in E:
return 2
else: # high card
return 1
H = list(reversed('AKQJT98765432'))
def compareH(h1, h2):
for a,b in zip(h1,h2):
if H.index(a) < H.index(b):
return 1
elif H.index(a) > H.index(b):
return -1
return 0
def compare(T1, T2): # return >:-1, ==:0, <:1
h1, s1, *_ = T1
h2, s2, *_ = T2
s1 = score(h1)
s2 = score(h2)
if s1 < s2:
return 1
elif s2 < s1:
return -1
else:
return compareH(h1,h2)
L = [ l.split() for l in sys.stdin.read().splitlines()]
L = [[h,int(b)] for [h,b] in L]
for h,b in L:
print(h, score(h))
pass
L.sort(key=ctk(compare))
print(
sum((i+1)*b for i,(h,b) in enumerate(reversed(L)))
)

42
d07/part2.py Normal file
View File

@@ -0,0 +1,42 @@
import sys
from collections import Counter as C
from functools import cmp_to_key as ctk
H = list(reversed('AKQT98765432J'))
def score(h:str) -> int:
D = C(h)
j = D['J']
del D['J']
if j<5:
m = max(D.values())
for k,v in D.items():
if v == m:
D[k] = v+j
break
E = C(v for k,v in D.items()) if j<5 else {5:1}
r = 0
if 5 in E: # five of a kind
r = 7
elif 4 in E: # four of a kind
r = 6
elif 3 in E and 2 in E: # full house
r = 5
elif 3 in E: # three of a kind
r = 4
elif 2 in E and E[2] == 2: # two pairs
r = 3
elif 2 in E: # one pair
r = 2
else: # high card
r = 1
return (r, *[H.index(c) for c in h])
L = [ l.split() for l in sys.stdin.read().splitlines()]
L = [(score(h),int(b)) for [h,b] in L]
sorted(L)
print(
sum((i+1)*b for i,(h,b) in enumerate(reversed(L)))
)

18
d07/terse.py Normal file
View File

@@ -0,0 +1,18 @@
from collections import Counter
lines = [i for i in open('input').read().split('\n') if i.strip()]
def hand(h,part1):
if part1: h = h.replace('J', 'X')
h2 = ['J23456789TXQKA'.index(i)for i in h]
ts = []
for r in '23456789TQKA':
c = Counter(h.replace('J', r))
p = tuple(sorted(c.values()))
t = [(1,1,1,1,1),(1,1,1,2),(1,2,2),(1,1,3),(2,3),(1,4),(5,)].index(p)
ts.append(t)
return (max(ts), *h2)
for part1 in (True, False):
h = sorted((hand(h,part1), int(b)) for h, b in (l.split() for l in lines))
t = 0
for i,(_,b) in enumerate(h):
t+=i*b+b
print('Part', 2-part1, ':', t)

22
d08/run.py Normal file
View File

@@ -0,0 +1,22 @@
import sys
from itertools import cycle
from math import lcm
I, _, *P = [l for l in sys.stdin.read().splitlines()]
# 0000000000111111111
# 0123456789012345678
# BCN = (HFN, KFC)
P = {l[0:3]:(l[7:10],l[12:15]) for l in P}
def countsteps(g):
C = g
N = 0
for i in cycle(I):
if C[2] == 'Z':
break
C = P[C][i == 'R']
N += 1
return N
print("part 1:", countsteps("AAA"))
print("part 2:", lcm(*(countsteps(p) for p in P.keys() if p[2] == 'A')))

49
d14/run.py Normal file
View File

@@ -0,0 +1,49 @@
import sys
I = sys.stdin.read().splitlines()
G = [list(l) for l in I]
R = len(G)
C = len(G[0])
def rotate(G, F):
for r in range(R):
for c in range(C):
F[c][R-1-r] = G[r][c]
def tilt(G):
for x in range(C):
for y in range(R):
if G[y][x] == "O":
for j in range(y-1,0-1,-1):
if G[j][x] == '.':
G[j][x] = "O"
G[j+1][x] = "."
else:
break
score = lambda: sum((R-y)*row.count('O') for y,row in enumerate(G))
cache = {}
F = [['?' for _ in range(C)] for _ in range(R)] # pre-allocate saves 10% time (no GC)
target = 1_000_000_000
t = 0
while t<target:
print(t, end='\r')
t += 1
for j in range(4):
tilt(G)
if t==1 and j==0: # part 1
print(score())
rotate(G, F); F, G = G, F
#Gh = tuple(tuple(row) for row in G)
Gh = ''.join(''.join(row) for row in G)
if Gh in cache:
cycle_length = t-cache[Gh]
print(f'{t=}, {cycle_length=}')
amt = (target-t)//cycle_length
t += amt * cycle_length
cache[Gh] = t
print(t, score())

View File

@@ -1,12 +0,0 @@
import sys
S=0
for l in sys.stdin.read().split(','):
c = 0
for w in l:
c += ord(w)
c *= 17
c = c % 256
print(l, c)
S += c
print(S)

View File

@@ -8,9 +8,13 @@ def hash(l):
c = c % 256
return c
L = sys.stdin.read().split(',')
print(sum(hash(l) for l in L))
B = [ dict() for _ in range(256) ]
for l in sys.stdin.read().split(','):
for l in L:
e = l.find('=')
d = l.find('-')
i = max(e,d)