Skip to content

Commit 756b95b

Browse files
committed
more code golf!
1 parent 6c0ecea commit 756b95b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+219
-201
lines changed

2018/01/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,5 +63,5 @@ Your puzzle answer was `232`.
6363

6464
A simple first puzzle. So simple, in fact, that the first part of it can be done by just opening the input file in a spreadsheet, selecting the column and reading the sum off the status bar.
6565

66-
* Part 1, Python: 53 bytes, <100 ms
67-
* Part 2, Python: 138 bytes, <100 ms
66+
* Part 1, Python: 52 bytes, <100 ms
67+
* Part 2, Python: 134 bytes, <100 ms

2018/01/aoc2018_01_part1.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
print sum(int(x.strip()) for x in open("input.txt"))
1+
print sum(int(x.strip())for x in open("input.txt"))

2018/01/aoc2018_01_part2.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import itertools as I
1+
from itertools import*
22
s,f=set(),0
3-
for d in I.cycle([int(x.strip()) for x in open("input.txt")]):
3+
for d in cycle([int(x.strip())for x in open("input.txt")]):
44
f+=d
55
if f in s:break
6-
s.add(f)
6+
s|={f}
77
print f

2018/02/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,5 +54,5 @@ Your puzzle answer was `prtkqyluiusocwvaezjmhmfgx`.
5454

5555
A rather straightforward puzzle; nothing to note here.
5656

57-
* Part 1, Python: 165 bytes, <100 ms
58-
* Part 2, Python: 141 bytes, <100 ms
57+
* Part 1, Python: 158 bytes, <100 ms
58+
* Part 2, Python: 135 bytes, <100 ms

2018/02/aoc2018_02_part1.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import collections as C
1+
from collections import*
22
n={2:0,3:0}
33
for x in open("input.txt"):
4-
h=C.defaultdict(int)
4+
h=defaultdict(int)
55
for l in x:h[l]+=1
6-
for i in (2,3):n[i]+=int(i in h.values())
6+
for i in(2,3):n[i]+=i in h.values()
77
print n[2]*n[3]

2018/02/aoc2018_02_part2.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
x=map(str.strip, open("input.txt"))
2-
for p in xrange(len(x[0])):
3-
s=set()
1+
x=map(str.strip,open("input.txt"))
2+
for p in range(len(x[0])):
3+
s={0}
44
for f in x:
55
k=f[:p]+f[p+1:]
66
if k in s:print k;break
7-
s.add(k)
7+
s|={k}

2018/03/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,5 +66,5 @@ Your puzzle answer was `656`.
6666

6767
The apparent 1Kx1K problem size turned out to be a much smaller problem than it seemed, as the patches are very small and the resulting list of occupied fields is quite sparse.
6868

69-
* Part 1, Python: 233 bytes, ~200 ms
70-
* Part 2, Python: 305 bytes, ~1 s
69+
* Part 1, Python: 222 bytes, ~250 ms
70+
* Part 2, Python: 291 bytes, ~1 s

2018/03/aoc2018_03_part1.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
_=range
12
import re
2-
from collections import *
3+
from collections import*
34
d=defaultdict(int)
4-
for i,x,y,w,h in (map(int,re.findall('\d+',x)) for x in open("input.txt")):
5-
for y in xrange(y,y+h):
6-
for j in xrange(x,x+w):d[(j,y)]+=1
7-
print sum(1 for v in d.values() if v>1)
5+
for i,x,y,w,h in(map(int,re.findall('\d+',x))for x in open("input.txt")):
6+
for y in _(y,y+h):
7+
for j in _(x,x+w):d[(j,y)]+=1
8+
print sum(v>1for v in d.values())

2018/03/aoc2018_03_part2.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1+
_=range
12
import re,collections as C
2-
d=C.defaultdict(set)
3-
a=[0]
4-
for i,x,y,w,h in (map(int,re.findall('\d+',x)) for x in open("input.txt")):
5-
a.append(w*h)
6-
for y in xrange(y,y+h):
7-
for j in xrange(x,x+w):d[(j,y)].add(i)
3+
d,a=C.defaultdict(set),[0]
4+
for i,x,y,w,h in(map(int,re.findall('\d+',x))for x in open("input.txt")):
5+
a+=[w*h]
6+
for y in _(y,y+h):
7+
for j in _(x,x+w):d[(j,y)]|={i}
88
for s in d.values():
99
if len(s)==1:a[s.pop()]-=1
10-
print [i for i,a in enumerate(a) if a==0][1]
10+
print[i for i,a in enumerate(a)if a<1][1]

2018/04/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,5 +74,5 @@ The most complex task in this puzzle is converting the input into nice (guard ID
7474

7575
The funny thing is how similar parts 1 and 2 are: In my implementation, I just had to change a `sum` into a `max`, and that was it!
7676

77-
* Part 1, Python: 363 bytes, <100 ms
78-
* Part 2, Python: 363 bytes, <100 ms
77+
* Part 1, Python: 359 bytes, <100 ms
78+
* Part 2, Python: 359 bytes, <100 ms

2018/04/aoc2018_04_part1.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import re,collections as C
22
s=C.defaultdict(lambda:60*[0])
3-
for t,e in sorted(((int(l[6:17].translate(None," -:")),l[19:]) for l in open("input.txt"))):
3+
for t,e in sorted(((int(l[6:17].translate(None," -:")),l[19:])for l in open("input.txt"))):
44
m=re.search('\d+',e);t%=100
55
if m:g=int(m.group(0));continue
6-
if e[0]=='f':a=t;continue
6+
if e[0]<'w':a=t;continue
77
for m in range(a,t):s[g][m]+=1
8-
g=max((sum(v),k) for k,v in s.items())[1]
9-
print g*max(x[::-1] for x in enumerate(s[g]))[1]
8+
g=max((sum(v),k)for k,v in s.items())[1]
9+
print g*max(x[::-1]for x in enumerate(s[g]))[1]

2018/04/aoc2018_04_part2.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import re,collections as C
22
s=C.defaultdict(lambda:60*[0])
3-
for t,e in sorted(((int(l[6:17].translate(None," -:")),l[19:]) for l in open("input.txt"))):
3+
for t,e in sorted(((int(l[6:17].translate(None," -:")),l[19:])for l in open("input.txt"))):
44
m=re.search('\d+',e);t%=100
55
if m:g=int(m.group(0));continue
6-
if e[0]=='f':a=t;continue
6+
if e[0]<'w':a=t;continue
77
for m in range(a,t):s[g][m]+=1
8-
g=max((max(v),k) for k,v in s.items())[1]
9-
print g*max(x[::-1] for x in enumerate(s[g]))[1]
8+
g=max((max(v),k)for k,v in s.items())[1]
9+
print g*max(x[::-1]for x in enumerate(s[g]))[1]

2018/05/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,5 +55,5 @@ My initial implementation (and seemingly the ones from many other participants a
5555

5656
In the end, I found out that the reduction can be done in a single pass -- you just need to go back one additional position after removing a reacting pair of units! This makes the code more than an order of magnitude faster, as can be seen in my implementations: part 1 still uses the repeated partial reaction algorithm, while part 2 performs 26 iterations of the faster algorithm, and is still twice as fast.
5757

58-
* Part 1, Python (multi-pass): 185 bytes, ~3 s
59-
* Part 2, Python (single-pass): 270 bytes, ~2 s
58+
* Part 1, Python (multi-pass): 181 bytes, ~3 s
59+
* Part 2, Python (single-pass): 260 bytes, ~2 s

2018/05/aoc2018_05_part1.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
p=open("input.txt").read().strip()
2-
o=None
1+
p,o=open("input.txt").read().strip(),0
32
while o!=p:
43
o,i=p,1
54
while i<len(p):
65
a,b=sorted([p[i-1],p[i]])
7-
if b.islower() and a==b.upper():p=p[:i-1]+p[i+1:]
6+
if b.islower()and a==b.upper():p=p[:i-1]+p[i+1:]
87
else:i+=1
98
print len(p)

2018/05/aoc2018_05_part2.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@ def r(p):
33
i=0
44
while i<len(p)-1:
55
a,b=sorted([p[i],p[i+1]])
6-
if b.islower() and a==b.upper():
7-
p=p[:i]+p[i+2:]
8-
i=max(0,i-1)
6+
if b.islower()and a==b.upper():p=p[:i]+p[i+2:];i=max(0,i-1)
97
else:i+=1
108
return len(p)
11-
print min(r(p.translate(None,c+c.lower())) for c in map(chr,xrange(65,91)))
9+
print min(r(p.translate(None,c+c.lower()))for c in map(chr,range(65,91)))

2018/06/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,5 +105,5 @@ I used a neat little (though probably obvious) trick to detect the infinite-area
105105

106106
This is also one of the puzzles where part 2 is actually easier to implement (and compute) than part 1.
107107

108-
* Part 1, Python: 374 bytes, ~2 s
109-
* Part 2, Python: 265 bytes, ~500 ms
108+
* Part 1, Python: 357 bytes, ~1.5 s
109+
* Part 2, Python: 255 bytes, ~750 ms

2018/06/aoc2018_06_part1.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
_=range
12
import re
2-
v=[map(int,re.findall('\d+',x)) for x in open("input.txt")]
3-
s,t=(min(p[i] for p in v)-1 for i in (0,1))
4-
w,h=(max(p[i] for p in v)+1 for i in (0,1))
3+
v=[map(int,re.findall('\d+',x))for x in open("input.txt")]
4+
s,t=(min(p[i]for p in v)-1for i in(0,1))
5+
w,h=(max(p[i]for p in v)+1for i in(0,1))
56
r=[0]*len(v)
6-
for y in xrange(t,h+1):
7-
for x in xrange(s,w+1):
8-
d=sorted((abs(x-p[0])+abs(y-p[1]),i) for i,p in enumerate(v))
9-
if d[0][0]<d[1][0]:r[d[0][1]]+=-1e6 if x<=s or y<=t or x>=w or y>=h else 1
7+
for y in _(t,h+1):
8+
for x in _(s,w+1):
9+
d=sorted((abs(x-p[0])+abs(y-p[1]),i)for i,p in enumerate(v))
10+
if d[0][0]<d[1][0]:r[d[0][1]]+=1-999*(x<=s or y<=t or x>=w or y>=h)
1011
print max(r)

2018/06/aoc2018_06_part2.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
_=range
12
import re
2-
r,v=0,[map(int,re.findall('\d+',x)) for x in open("input.txt")]
3-
s,t=(min(p[i] for p in v) for i in (0,1))
4-
w,h=(max(p[i] for p in v)+1 for i in (0,1))
5-
for y in xrange(t,h):
6-
for x in xrange(s,w):
7-
if sum(abs(x-a)+abs(y-b) for a,b in v)<10000:r+=1
3+
r,v=0,[map(int,re.findall('\d+',x))for x in open("input.txt")]
4+
s,t=(min(p[i]for p in v)for i in(0,1))
5+
w,h=(max(p[i]for p in v)+1for i in(0,1))
6+
for y in _(t,h):
7+
for x in _(s,w):
8+
if sum(abs(x-a)+abs(y-b)for a,b in v)<10000:r+=1
89
print r

2018/07/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,5 +88,5 @@ Your puzzle answer was `948`.
8888

8989
The first part was reasonably easy to solve, but the second part caused me a few headaches. In the end, I had a strange off-by-one error: For the simple scenario in the example, my code produced the correct answer, but for the actual puzzle input, the result was exactly one too much. I still don't understand what the problem is.
9090

91-
* Part 1, Python: 251 bytes, <100 ms
92-
* Part 2, Python: 414 bytes, <100 ms
91+
* Part 1, Python: 246 bytes, <100 ms
92+
* Part 2, Python: 408 bytes, <100 ms

2018/07/aoc2018_07_part1.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import re,collections as C
22
D=C.defaultdict
33
l,g,r=D(int),D(set),""
4-
for a,b in (re.findall(r'\b\w\b',x) for x in open("input.txt")):g[a].add(b);l[a]+=0;l[b]+=1
4+
for a,b in(re.findall(r'\b\w\b',x)for x in open("input.txt")):g[a]|={b};l[a]+=0;l[b]+=1
55
while l:
6-
x=min((c,x) for x,c in l.items())[1]
6+
x=min((c,x)for x,c in l.items())[1]
77
for y in g[x]:l[y]-=1
88
r+=x;del l[x]
99
print r

2018/07/aoc2018_07_part2.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import re,collections as C
22
D=C.defaultdict
33
l,g,t=D(int),D(set),-1
4-
for a,b in (re.findall(r'\b\w\b',x) for x in open("input.txt")):g[a].add(b);l[a]+=0;l[b]+=1
4+
for a,b in(re.findall(r'\b\w\b',x)for x in open("input.txt")):g[a]|={b};l[a]+=0;l[b]+=1
55
w,l[0]=5*[(0,0)],1
66
while t<0 or any(j for j,d in w):
77
t+=1
@@ -10,8 +10,8 @@
1010
if j and t>=d:
1111
for x in g[j]:l[x]-=1
1212
j=0
13-
if j==0:
14-
c,k=min(sorted((c,x) for x,c in l.items()))
13+
if j<1:
14+
c,k=min(sorted((c,x)for x,c in l.items()))
1515
if not c:j,d=k,t+ord(k)-4;del l[j]
1616
w[i]=j,d
1717
print t-1

2018/08/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,5 +65,5 @@ Your puzzle answer was `22608`.
6565

6666
This was a refreshingly simple and straighforward puzzle for a change. Nothing particular to report here.
6767

68-
* Part 1, Python: 166 bytes, <100 ms
69-
* Part 2, Python: 238 bytes, <100 ms
68+
* Part 1, Python: 163 bytes, <100 ms
69+
* Part 2, Python: 232 bytes, <100 ms

2018/08/aoc2018_08_part1.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1-
d,s=map(int, open("input.txt").read().strip().split())[::-1],0
2-
def v():n,m=d.pop(),d.pop();return sum(v() for i in range(n))+sum(d.pop() for i in range(m))
1+
_=range
2+
d,s=map(int,open("input.txt").read().strip().split())[::-1],0
3+
def v():n,m=d.pop(),d.pop();return sum(v()for i in _(n))+sum(d.pop()for i in _(m))
34
print v()

2018/08/aoc2018_08_part2.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
d=map(int, open("input.txt").read().strip().split())[::-1]
2-
def b():n,m=d.pop(),d.pop();return ([b() for i in range(n)],[d.pop() for i in range(m)])
3-
v=lambda c,m:sum(v(*c[i-1]) for i in m if i and i<=len(c)) if c else sum(m)
1+
d=map(int,open("input.txt").read().strip().split())[::-1]
2+
def b():n,m=d.pop(),d.pop();return([b()for i in range(n)],[d.pop()for i in range(m)])
3+
v=lambda c,m:sum(v(*c[i-1])for i in m if i and i<=len(c))if c else sum(m)
44
print v(*b())

2018/09/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,6 @@ _(Fun fact: Juggling around with references in (doubly-)linked lists never cease
7777

7878
The C version is more than two orders of magnitude faster than the Python version, by the way -- so much so, that part 2 in C is twice as fast as part 1 in Python (with lists)! And that's without optimization. (Optimization didn't buy me anything, as I include the compilation time in the runtime here -- sure, `gcc -O4` generates 30% faster code, but it takes twice as long to do so ...)
7979

80-
* Part 1, Python (array): 205 bytes, ~500 ms
81-
* Part 2, Python (CDLL): 277 bytes, ~30 s
80+
* Part 1, Python (array): 204 bytes, ~500 ms
81+
* Part 2, Python (CDLL): 276 bytes, ~30 s
8282
* Part 2, C (CDLL): 427 bytes, ~200 ms (including compilation)

2018/09/aoc2018_09_part1.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import re
22
n,m=map(int,re.findall('\d+',open("input.txt").read()))
33
t,p,s=[0],0,n*[0]
4-
for i in xrange(1,m+1):
4+
for i in range(1,m+1):
55
l=len(t);p=(p+2)%l
66
if i%23:t.insert(p,i)
77
else:p=(p+l-9)%l;s[(i-1)%n]+=i+t.pop(p)

2018/09/aoc2018_09_part2.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
n,m=map(int,re.findall('\d+',open("input.txt").read()))
33
class S:pass
44
c=S();c.i,c.p,c.n,s=0,c,c,n*[0]
5-
for i in xrange(1,m*100+1):
5+
for i in range(1,m*100+1):
66
if i%23:c=c.n;x=S();x.i,x.p,x.n=i,c,c.n;x.n.p,x.p.n,c=x,x,x
77
else:c=c.p.p.p.p.p.p.p;s[i%n]+=i+c.i;c.p.n,c.n.p,c=c.n,c.p,c.n
88
print max(s)

2018/10/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,4 +163,4 @@ A quick glance at the inputs shows coordinates in the positive or negative 10000
163163

164164
This puzzle is a little unusual in that the solution for part 2 is actually an interim result of part 1, so there's not even a separate implementation required for it.
165165

166-
* Parts 1+2, Python: 384 bytes, <100 ms
166+
* Parts 1+2, Python: 365 bytes, <100 ms

2018/10/aoc2018_10.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1+
_=xrange
12
import sys,re
2-
r=[map(int,re.findall('-?\d+',x)) for x in sys.stdin]
3+
r=[map(int,re.findall('-?\d+',x))for x in open("input.txt")]
34
def c(t):
4-
p=[(x+u*t,y+v*t) for x,y,u,v in r]
5-
x,y=(min(k[i] for k in p) for i in (0,1))
6-
p=[(a-x,b-y) for a,b in p]
7-
w,h=(max(k[i] for k in p)+1 for i in (0,1))
5+
p=[(x+u*t,y+v*t)for x,y,u,v in r]
6+
x,y=(min(k[i]for k in p)for i in(0,1))
7+
p=[(a-x,b-y)for a,b in p]
8+
w,h=(max(k[i]for k in p)+1for i in(0,1))
89
return w*h,t,w,h,p
9-
a,t,w,h,p=min(c(t) for t in xrange(9900,10100))
10+
a,t,w,h,p=min(c(t)for t in _(9900,10100))
11+
for y in _(h):print''.join(" #"[(x,y)in p]for x in _(w))
1012
print t
11-
for y in xrange(h):print ''.join(" #"[(x,y) in p] for x in xrange(w))

2018/11/README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,10 @@ An alternate solution is making use of a [summed-area table](https://en.wikipedi
9292

9393
In C, I got the two fastest Python implementations down to less than 100 milliseconds each, *including* compilation. (Without that, the SAT solution takes are mere 20 milliseconds to run.) This time, compiler optimization was a net win, i.e. it makes the code so much faster (~3x) that the longer compile times are more than compensated.
9494

95-
* Part 1, Python: 216 bytes, ~100 ms
96-
* Part 2, Python (naive): 281 bytes, ~10 minutes
97-
* Part 2, Python (pre-summed matrix): 330 bytes, ~15 s
98-
* Part 2, Python (optimized pre-summed matrix): 385 bytes, ~3.5 s
99-
* Part 2, Python (SAT): 331 bytes, ~2.5 s
95+
* Part 1, Python: 201 bytes, ~100 ms
96+
* Part 2, Python (naive): 260 bytes, ~10 minutes
97+
* Part 2, Python (pre-summed matrix): 305 bytes, ~15 s
98+
* Part 2, Python (optimized pre-summed matrix): 361 bytes, ~3.5 s
99+
* Part 2, Python (SAT): 303 bytes, ~2.5 s
100100
* Part 2, C (optimized pre-summed matrix): 438 bytes, <100 ms
101101
* Part 2, C (SAT): 381 bytes, <100 ms

2018/11/aoc2018_11_part1.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import itertools as I
1+
_=range
2+
from itertools import*
23
S=7989
3-
g=[[((x+11)*(y+1)+S)*(x+11)/100%10-5 for x in xrange(300)] for y in xrange(300)]
4-
r=range(297);print max((sum(sum(g[y+i][x:x+3]) for i in range(3)),x+1,y+1) for x,y in I.product(r,r))[1:]
4+
g=[[((x+11)*(y+1)+S)*(x+11)/100%10-5for x in _(300)]for y in _(300)]
5+
r=_(297);print max((sum(sum(g[y+i][x:x+3])for i in _(3)),x+1,y+1)for x,y in product(r,r))[1:]

2018/11/aoc2018_11_part2_try1.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
import itertools as I
1+
_=range
2+
from itertools import*
23
S=7989
34
N=300
4-
g=[[((x+11)*(y+1)+S)*(x+11)/100%10-5 for x in xrange(N)] for y in xrange(N)]
5-
def M(s):r=range(N-s);return max((sum(sum(g[y+i][x:x+s]) for i in xrange(s)),x+1,y+1,s) for x,y in I.product(r,r))
5+
g=[[((x+11)*(y+1)+S)*(x+11)/100%10-5for x in _(N)]for y in _(N)]
6+
def M(s):r=_(N-s);return max((sum(sum(g[y+i][x:x+s])for i in _(s)),x+1,y+1,s)for x,y in product(r,r))
67
b=(0,)
7-
for s in xrange(1,N):b=max(b,M(s));print b[1:]
8+
for s in _(1,N):b=max(b,M(s));print b[1:]

2018/11/aoc2018_11_part2_try2.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
import itertools as I
1+
_=range
2+
from itertools import*
23
S=7989
34
N=300
4-
g=[[((x+11)*(y+1)+S)*(x+11)/100%10-5 for x in xrange(N)] for y in xrange(N)]
5-
S=lambda r,s:[sum(r[x:x+s]) for x in xrange(N-s)]
6-
def M(s):t=[S(r,s) for r in zip(*(S(r,s) for r in g))];r=xrange(N-s);return max((t[y][x],y+1,x+1,s) for x,y in I.product(r,r))
7-
print max(M(s) for s in xrange(1,N))[1:]
5+
g=[[((x+11)*(y+1)+S)*(x+11)/100%10-5for x in _(N)]for y in _(N)]
6+
S=lambda r,s:[sum(r[x:x+s])for x in _(N-s)]
7+
def M(s):t=[S(r,s)for r in zip(*(S(r,s)for r in g))];r=_(N-s);return max((t[y][x],y+1,x+1,s)for x,y in product(r,r))
8+
print max(M(s)for s in _(1,N))[1:]

2018/11/aoc2018_11_part2_try3.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
import itertools as I
1+
_=range
2+
from itertools import*
23
S=7989
34
N=300
4-
g=[[((x+11)*(y+1)+S)*(x+11)/100%10-5 for x in xrange(N)] for y in xrange(N)]
5+
g=[[((x+11)*(y+1)+S)*(x+11)/100%10-5for x in _(N)]for y in _(N)]
56
def S(d,s):
67
r,a=(N-s)*[0],sum(d[:s]);r[0]=a
7-
for i in xrange(1,N-s):a+=d[i+s-1]-d[i-1];r[i]=a
8+
for i in _(1,N-s):a+=d[i+s-1]-d[i-1];r[i]=a
89
return r
9-
def M(s):t=[S(r,s) for r in zip(*(S(r,s) for r in g))];r=xrange(N-s);return max((t[y][x],y+1,x+1,s) for x,y in I.product(r,r))
10-
print max(M(s) for s in xrange(1,N))[1:]
10+
def M(s):t=[S(r,s)for r in zip(*(S(r,s)for r in g))];r=_(N-s);return max((t[y][x],y+1,x+1,s)for x,y in product(r,r))
11+
print max(M(s)for s in _(1,N))[1:]

0 commit comments

Comments
 (0)