This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import itertools as it | |
import re | |
cubes = sorted(([*map(int, re.findall(r'\d+', l))] for l in open(0)), | |
key=lambda p: p[2]) | |
def points(c, dz=0): | |
x1, y1, z1, x2, y2, z2 = c | |
return it.product(range(x1, x2+1), range(y1, y2+1), range(z1-dz, z2+1-dz)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from collections import * | |
from math import * | |
O, I = defaultdict(list), defaultdict(list) | |
FLIP, CONJ = set(), set() | |
for l in open(0): | |
a, bs = l.strip().split(' -> ') | |
a = a.strip() | |
if a[0]=='%': | |
a = a[1:] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
wf, pt = open(0).read().split('\n\n') | |
wfd = {} | |
for l in wf.split(): | |
w, cs = l.split('{') | |
cd = list() | |
conds = cs[:-1].split(',') | |
for i, c in enumerate(conds): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
x, y, A, L, P2 = 0, 0, 0, 0, True | |
for d, s, c in map(str.split, open(0)): | |
s = int(c[2:-2], 16) if P2 else int(s) | |
if P2: d = "RDLU"[int(c[-2])] | |
nx, ny = x, y | |
if d=='R': | |
ny += s | |
elif d=='L': | |
ny -= s | |
elif d=='U': |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import itertools as it | |
L = [l.strip() for l in open(0)] | |
N, M = len(L), len(L[0]) | |
starts: list[tuple[int, int]] = list( | |
it.chain(*zip(*(((i, -1), (i, M)) for i in range(N))), | |
*zip(*(((-1, j), (N, j)) for j in range(M))))) | |
ms = [(1, 0), (0, 1), (-1, 0), (0, -1)] | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from heapq import * | |
L = [[*map(int, l.strip())] for l in open(0)] | |
N, M = len(L), len(L[0]) | |
MS = [(1, 0), (-1, 0), (0, 1), (0, -1)] | |
x, y = 0, 0 | |
E = (N-1, M-1) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def chsh(s): | |
hsh = 0 | |
for c in s: | |
hsh += ord(c) | |
hsh *= 17 | |
hsh %= 256 | |
return hsh | |
hshsum = 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def tilt(ls): | |
for i in range(len(ls)): | |
for j in range(len(ls[0])): | |
if ls[i][j]!='O': continue | |
new_loc = 0 | |
for k in range(i-1, -1, -1): | |
if ls[k][j]=='.': continue | |
new_loc = k+1 | |
break | |
ls[i][j] = '.' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ans = 0 | |
for l in open(0): | |
P, G = l.split() | |
G = tuple(map(int, G.split(','))) | |
P, G = '?'.join([P]*5), G*5 # part 2 | |
P = P+'.' | |
N, M = len(P), len(G) | |
cdot = [P[i:].count('.') for i in range(N)] | |
dp = [[0]*(M+1) for _ in range(N+1)] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def collect(u): | |
ri, i = [], 0 | |
for r in u: | |
n = r.count('#') | |
if n: ri.extend([i]*n) | |
i += 1 if n else D | |
return ri | |
D = 1000000 # 2 # for part 1 | |
manhattan_sum = lambda xs: sum((1+i+i-len(xs))*x for i, x in enumerate(xs)) |
NewerOlder