Solve 2016 days 6 to 8.
This commit is contained in:
25
2016/d6.py
Normal file
25
2016/d6.py
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
from lib import *
|
||||||
|
|
||||||
|
data = open(0).read().strip()
|
||||||
|
second = True
|
||||||
|
|
||||||
|
p = ""
|
||||||
|
for row in zip(*data.splitlines()):
|
||||||
|
counts = {}
|
||||||
|
for l in LETTERS_LOWER:
|
||||||
|
counts[l] = row.count(l)
|
||||||
|
maxc = 0
|
||||||
|
minc = 999
|
||||||
|
c = ""
|
||||||
|
for l, count in counts.items():
|
||||||
|
if second:
|
||||||
|
if count > 0 and count < minc:
|
||||||
|
minc = count
|
||||||
|
c = l
|
||||||
|
else:
|
||||||
|
if count > maxc:
|
||||||
|
maxc = count
|
||||||
|
c = l
|
||||||
|
p += c
|
||||||
|
|
||||||
|
print(p)
|
||||||
72
2016/d7.py
Normal file
72
2016/d7.py
Normal file
@@ -0,0 +1,72 @@
|
|||||||
|
from lib import *
|
||||||
|
|
||||||
|
data = open(0).read().strip()
|
||||||
|
first = False
|
||||||
|
|
||||||
|
def has_abba(xs):
|
||||||
|
for i in range(len(xs) - 3):
|
||||||
|
if xs[i] == xs[i + 3] and xs[i + 1] == xs[i + 2] and xs[i] != xs[i + 1]:
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
|
if first:
|
||||||
|
r = 0
|
||||||
|
for line in data.splitlines():
|
||||||
|
is_abba = False
|
||||||
|
|
||||||
|
xs = ""
|
||||||
|
for c in line:
|
||||||
|
if c == "[":
|
||||||
|
if has_abba(xs):
|
||||||
|
is_abba = True
|
||||||
|
xs = ""
|
||||||
|
elif c == "]":
|
||||||
|
if has_abba(xs):
|
||||||
|
is_abba = False
|
||||||
|
xs = ""
|
||||||
|
break
|
||||||
|
xs = ""
|
||||||
|
xs += c
|
||||||
|
|
||||||
|
if xs != "" and has_abba(xs):
|
||||||
|
is_abba = True
|
||||||
|
|
||||||
|
if is_abba:
|
||||||
|
r += 1
|
||||||
|
print(r)
|
||||||
|
else:
|
||||||
|
def abas(xs) -> set:
|
||||||
|
r = set()
|
||||||
|
for i in range(len(xs) - 2):
|
||||||
|
if not in_square and xs[i] == xs[i + 2] and xs[i] != xs[i + 1]:
|
||||||
|
r.add(xs[i:i+3])
|
||||||
|
return r
|
||||||
|
|
||||||
|
def babs(xs) -> set:
|
||||||
|
r = set()
|
||||||
|
for i in range(len(xs) - 2):
|
||||||
|
if not in_square and xs[i] == xs[i + 2] and xs[i] != xs[i + 1]:
|
||||||
|
r.add(xs[i + 1] + xs[i] + xs[i + 1])
|
||||||
|
return r
|
||||||
|
|
||||||
|
r = 0
|
||||||
|
for line in data.splitlines():
|
||||||
|
aba, bab = set(), set()
|
||||||
|
in_square = False
|
||||||
|
xs = ""
|
||||||
|
for c in line:
|
||||||
|
if c == "[":
|
||||||
|
aba |= abas(xs)
|
||||||
|
xs = ""
|
||||||
|
elif c == "]":
|
||||||
|
bab |= babs(xs)
|
||||||
|
xs = ""
|
||||||
|
else:
|
||||||
|
xs += c
|
||||||
|
if xs != "":
|
||||||
|
aba |= abas(xs)
|
||||||
|
|
||||||
|
if aba & bab:
|
||||||
|
r += 1
|
||||||
|
|
||||||
|
print(r)
|
||||||
53
2016/d8.py
Normal file
53
2016/d8.py
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
from lib import *
|
||||||
|
|
||||||
|
data = open(0).read().strip()
|
||||||
|
|
||||||
|
cols = 50
|
||||||
|
rows = 6
|
||||||
|
pxs = []
|
||||||
|
|
||||||
|
def display(pxs):
|
||||||
|
for r in range(rows):
|
||||||
|
row = ""
|
||||||
|
for c in range(cols):
|
||||||
|
if (r, c) in pxs:
|
||||||
|
row += "#"
|
||||||
|
else:
|
||||||
|
row += "."
|
||||||
|
print(row)
|
||||||
|
print()
|
||||||
|
|
||||||
|
for line in data.splitlines():
|
||||||
|
if "rect" in line:
|
||||||
|
col, row = str_to_ints(line)
|
||||||
|
for r in range(row):
|
||||||
|
for c in range(col):
|
||||||
|
if r < rows and c < cols:
|
||||||
|
pxs.append((r, c))
|
||||||
|
elif "row" in line:
|
||||||
|
npxs = []
|
||||||
|
row, offset = str_to_ints(line)
|
||||||
|
for (r, c) in pxs:
|
||||||
|
if r == row:
|
||||||
|
nc = (c + offset) % cols
|
||||||
|
npxs.append((r, nc))
|
||||||
|
else:
|
||||||
|
npxs.append((r, c))
|
||||||
|
pxs = npxs
|
||||||
|
elif "column" in line:
|
||||||
|
npxs = []
|
||||||
|
col, offset = str_to_ints(line)
|
||||||
|
for (r, c) in pxs:
|
||||||
|
if c == col:
|
||||||
|
nr = (r + offset) % rows
|
||||||
|
npxs.append((nr, c))
|
||||||
|
else:
|
||||||
|
npxs.append((r, c))
|
||||||
|
pxs = npxs
|
||||||
|
else:
|
||||||
|
assert False
|
||||||
|
|
||||||
|
pxs = list(set(pxs))
|
||||||
|
|
||||||
|
print(len(pxs))
|
||||||
|
display(pxs) # EOARGPHYAO
|
||||||
Reference in New Issue
Block a user