first 4
This commit is contained in:
commit
ecd5e84609
|
@ -0,0 +1,28 @@
|
|||
#!/usr/bin/env python3
|
||||
from useful import *
|
||||
|
||||
def win(b, d):
|
||||
#print(b, d)
|
||||
hit = np.vectorize(d.get)(b)
|
||||
#print(hit)
|
||||
size = b.shape[0]
|
||||
return any(correct == size for correct in (*hit.sum(axis=0), *hit.sum(axis=1)))
|
||||
|
||||
def score(b, d, last):
|
||||
return last * sum(x for x in b.reshape(np.prod(b.shape)) if not d[x])
|
||||
|
||||
with open(0) as f:
|
||||
|
||||
header, footer = f.read().split('\n\n', maxsplit=1)
|
||||
pool = [int(x) for x in header.strip().split(',')]
|
||||
boards = [np.asarray([int(n) for n in board.strip().split()]).reshape((5,5))
|
||||
for board in footer.split('\n\n')]
|
||||
#print(boards)
|
||||
drawn = {n: False for n in pool} # to vectorize
|
||||
for n in pool:
|
||||
#print('%d...' % n)
|
||||
drawn[n] = True
|
||||
for b in boards:
|
||||
if win(b, drawn):
|
||||
print(score(b, drawn, n))
|
||||
exit()
|
|
@ -0,0 +1,28 @@
|
|||
#!/usr/bin/env python3
|
||||
from useful import *
|
||||
|
||||
def win(b, d):
|
||||
hit = np.vectorize(d.get)(b)
|
||||
size = b.shape[0]
|
||||
return any(correct == size for correct in (*hit.sum(axis=0), *hit.sum(axis=1)))
|
||||
|
||||
def score(b, d, last):
|
||||
return last * sum(x for x in b.reshape(np.prod(b.shape)) if not d[x])
|
||||
|
||||
with open(0) as f:
|
||||
|
||||
header, footer = f.read().split('\n\n', maxsplit=1)
|
||||
pool = [int(x) for x in header.strip().split(',')]
|
||||
boards = [np.asarray([int(n) for n in board.strip().split()]).reshape((5,5))
|
||||
for board in footer.split('\n\n')]
|
||||
drawn = {n: False for n in pool} # to vectorize
|
||||
winners = []
|
||||
for n in pool:
|
||||
drawn[n] = True
|
||||
for i in range(len(boards))[::-1]:
|
||||
b = boards[i]
|
||||
if win(b, drawn):
|
||||
winners.append(score(b, drawn, n))
|
||||
del boards[i]
|
||||
|
||||
print(winners[-1])
|
|
@ -0,0 +1,3 @@
|
|||
#!/usr/bin/awk -f
|
||||
|
||||
NR>1 && $1 > prev {n++} 1 {prev=$1} END{print n}
|
|
@ -0,0 +1,3 @@
|
|||
#!/usr/bin/awk -f
|
||||
|
||||
{a[NR]=$1} END{for(i=4;i<=NR;i++) if(a[i] > a[i-3]) n++; print n}
|
|
@ -0,0 +1,16 @@
|
|||
#!/usr/bin/env python3
|
||||
from useful import *
|
||||
|
||||
def tpl(l):
|
||||
return [int(c) for c in l]
|
||||
|
||||
with open(0) as f:
|
||||
|
||||
lines = [l.strip() for l in f.readlines()]
|
||||
t = [tpl(l) for l in lines]
|
||||
|
||||
a = np.asarray(t)
|
||||
gamma = int(''.join(str(np.bincount(c).argmax()) for c in a.T), 2)
|
||||
epsilon = 2 ** len(a[0]) - 1 - gamma
|
||||
print(gamma * epsilon)
|
||||
|
|
@ -0,0 +1,25 @@
|
|||
#!/usr/bin/env python3
|
||||
from useful import *
|
||||
|
||||
def tpl(l):
|
||||
return [int(c) for c in l]
|
||||
|
||||
def makenum(l):
|
||||
return int(''.join(str(n) for n in l), 2)
|
||||
|
||||
def nextcol(a, i=0, crit=lambda a, b: a == b):
|
||||
if len(a) <= 1:
|
||||
return a[0]
|
||||
counts = np.bincount(a.T[i])
|
||||
most_common = 1 - counts[::-1].argmax() # like argmax but takes 1 if equal
|
||||
return nextcol(np.asarray([c for c in a if crit(c[i], most_common)]), i+1, crit)
|
||||
|
||||
with open(0) as f:
|
||||
|
||||
lines = [l.strip() for l in f.readlines()]
|
||||
t = [tpl(l) for l in lines]
|
||||
|
||||
a = np.asarray(t)
|
||||
oxy = nextcol(a)
|
||||
co2 = nextcol(a, crit=lambda a, b: a != b)
|
||||
print(makenum(oxy) * makenum(co2))
|
|
@ -0,0 +1,3 @@
|
|||
#!/usr/bin/awk -f
|
||||
|
||||
/forward/ {x+=$2} /down/ {y+=$2} /up/ {y-=$2} END {print x*y}
|
|
@ -0,0 +1,3 @@
|
|||
#!/usr/bin/awk -f
|
||||
|
||||
/forward/ {x+=$2; y+=$2*aim} /down/ {aim+=$2} /up/ {aim-=$2} END {print x*y}
|
Loading…
Reference in New Issue