-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
133 lines (124 loc) · 4.42 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/usr/bin/env python3
"""
Advent of Code 2021
Done in Python3 with goal of playing code golf i.e. low line/byte count
This is not meant to be "good" code.
It is dumb, it is insecure, it is inefficient, but it is fun.
"""
__author__ = "EanNewton"
__version__ = "0.1.0"
__license__ = "AGPL-3.0"
from copy import deepcopy as dc
from collections import deque as De
from numpy import asarray as A
from numpy import exp as E
from numpy.random import randn as N
from numpy.random import rand as R
from numpy.random import seed as S
from cutil import debug, multi_in
f_,u_,d_='forward','up','down'
i_,f_,l_,L_,r_,M_,m_,x_,e_,a_,s_,S_,z_,rl_=int,float,len,list,range,max,min,map,enumerate,abs,sum,set,zip,lambda _:r_(l_(_))
lmi_ = lambda _:list(map(int, _))
# Helper Functions
# most common bit
# 2 usage
def _m(r,i):
return[M_(S_(e),key=e.count)for e in[([_[e]for _ in r])for e in rl_(r[0])]][i]
# least common bit
# 1 usage
def _l(r,i):
return[m_(S_(e),key=e.count)for e in[([_[e]for _ in r])for e in rl_(r[0])]][i]
# matrix rotation
# def _mr(r):
# return[[_[e]for _ in r]for e in rl_(r[0])]
# day 4 total score
def _t(b,c,l,s=[]):
c=[i_(_)for _ in c]
for _ in[x for y in[lmi_(i)for i in b]for x in y]:
if _ not in c[0:c.index(i_(l))+1]:s.append(_)
return s_(s)*i_(l)
# day 5 filter input
def _f(r,c=[],m=0):
for _ in [_.split(" -> ")for _ in r.splitlines()]:
v=[i_(j)for k in _ for j in k.split(',')]
n={"a":v[0],"c":v[1],"b":v[2],"d":v[3]}
if m<M_(v):m=M_(v)
c.append(n)
return c,m+1
# day 5 draw positions on board
def _sp(p,g,a="c",b="d",c="a",d="b"):
if p[a]>p[b]:p[a],p[b],p[c],p[d]=p[b],p[a],p[d],p[c]
for _ in r_(p[a],p[b]+1):
if a=="c":g[_][p[c]]+=1
else:g[p[c]][_]+=1
# day 5 Bresenham's line algo
def _L(x,b,c,d,e=0):
f,g=c-x,d-b
h,i,j=a_(f)//f,a_(g)//g,a_(g/f)
for x in r_(x,c,h):
yield x,b;e+=j
while e>=.5:b+=i;e-=1
yield c,d
def d11(r):
return l_([x for x,y in z_(r,r[1:])if x<y])
def d12(r):
return l_([x for x,y in z_([s_(r[i:i+3])for i in rl_(r)if i<l_(r)-2],[s_(r[i:i+3])for i in rl_(r)if i<l_(r)-2][1:])if x<y])
def d21(r,m=[[0,0]]):
for o,v in[(o,i_(v))for _ in r for(o,v)in[_.split()]]:m=[[m[i][j]+{f_:[[v,0]],u_:[[0,v*-1]],d_:[[0,v]]}.get(o,None)[i][j]for j in rl_(m[0])]for i in rl_(m)]
return m[0][0]*m[0][1]
def d22(a,b=[[0,0,0]]):
for d,e in[(d,i_(e))for _ in a for(d,e)in[_.split()]]:b=[[b[i][j]+{f_:[[e,e*b[0][2],0]],u_:[[0,0,e*-1]],d_:[[0,0,e]]}.get(d,None)[i][j]for j in rl_(b[0])]for i in rl_(b)]
return b[0][0]*b[0][1]
def d31(r):
return(lambda _:_[0]*_[1])([i_(''.join([f(S_(e),key=e.count)for e in[([_[e]for _ in r])for e in rl_(r[0])]]),2)for f in(m_,M_)])
def d32(r):
def l(r,f,i=0):
while l_(r)>2:r=[_ for _ in r if _[i]==globals()[f](r,i)];i+=1
return i_([([r[0],r[1]][r[0][i]=='1']),([r[1],r[0]][r[0][i]=='1'])][f=='_m'],2)
return l(r,'_m')*l(r,'_l')
def d40(r,W=[],f=0): # f = True for part 1, False for part 2
c=[r.splitlines()][0][0].split(',')
b=[_ for _ in r.splitlines()[1::]if _]
b=[x.split()for x in b]
R=[b[n:n+5]for n in r_(0,l_(b),5)]
k=dc(R)
z=[[x,y]for(x,y)in z_([[[_[e]for _ in r]for e in rl_(r[0])]for _ in R],R)]
for e in c:
for bi,_ in e_(z):
for h in _:
for i in h:
if e in i:i.pop(i.index(e))
if l_(i)==0:
if f:
if bi not in W:W.append(bi)
if l_(W)==l_(z):return _t(k[bi],c,e)
else:return _t(k[bi],c,e)
def d50(r,f=0): # f = False for part 1, True for part 2
p,m=_f(r)
g=[[0 for _ in r_(m)]for i in r_(m)]
for _ in p:
if _["a"]==_["b"]:_sp(_,g)
elif _["c"]==_["d"]:_sp(_,g,"a","b","c","d")
elif f*a_(_["a"]-_["b"])==a_(_["c"]-_["d"]):
for x,y in _L(_["a"],_["c"],_["b"],_["d"]):g[y][x]+=1
return l_([i for s in g for i in s if i>1])
def d60(r,e=256,c=[0for _ in[0]*9]):
for _ in lmi_(r[0].split(',')):c[_]+=1
for _ in[0]*e:
c[7]+=c[0]
d=De(c)
d.rotate(-1)
c=L_(d)
return s_(c)
def d70(r,f=0):
r=lmi_(r.split(','))
o=((lambda p:s_([a_(e-p)for e in r])),(lambda p:s_([(f_(a_(p-e))**2+f_(a_(p-e)))/2for e in r])))[f]
b,B=0,o(0)
for _ in r_(0,M_(r)+1):
c,C=_,o(_)
if C<B:b,B=c,C
return B
if __name__ == '__main__':
#print(globals()['d' + input('>')](multi_in())) # ENTER to EOF
#print(globals()['d'+input('>')](sys.stdin.readlines())) # Ctrl-D to EOF
print(globals()['d' + input('>')](open(0).read())) # Ctrl-D to EOF