days 5,6,7 of advent of code

This commit is contained in:
Peter 2020-12-07 17:43:43 +08:00
parent ef5adffb49
commit 32cb11f3a9
6 changed files with 167 additions and 0 deletions

32
5/a.py Normal file
View File

@ -0,0 +1,32 @@
import math as m
lines = open('input').read().splitlines()
def uphalf(t):
return [t[0], t[1] + int(abs(t[0] - t[1])/2) + 1]
def lowhalf(t):
return [t[0] - int(abs(t[0] - t[1])/2) - 1, t[1]]
def bsp(dom, low, up, input):
for p in input:
if p == low:
dom = lowhalf(dom)
else:
dom = uphalf(dom)
if input[-1] == low:
return dom[1]
else:
return dom[0]
ids = []
rowdomain = [127, 0]
coldomain = [7, 0]
for line in lines:
bruh = [
bsp(rowdomain, "F", "B", line[0:7]),
bsp(coldomain, "L", "R", line[7:10])
]
ids.append(bruh[0]*8 + bruh[1])
print(max(ids))

38
5/b.py Normal file
View File

@ -0,0 +1,38 @@
import math as m
lines = open('input').read().splitlines()
def uphalf(t):
return [t[0], t[1] + int(abs(t[0] - t[1])/2) + 1]
def lowhalf(t):
return [t[0] - int(abs(t[0] - t[1])/2) - 1, t[1]]
def bsp(dom, low, up, input):
for p in input:
if p == low:
dom = lowhalf(dom)
else:
dom = uphalf(dom)
if input[-1] == low:
return dom[1]
else:
return dom[0]
ids = []
seats = []
rowdomain = [127, 0]
coldomain = [7, 0]
for line in lines:
bruh = [
bsp(rowdomain, "F", "B", line[0:7]),
bsp(coldomain, "L", "R", line[7:10])
]
id = bruh[0]*8 + bruh[1]
ids.append(id)
seats.append([bruh[0], bruh[1],id])
for i in range(0,128):
for j in range(0,8):
if i*8+j not in ids and i*8+j+1 in ids and i*8+j-1 in ids:
print(i*8+j)

21
6/a.py Normal file
View File

@ -0,0 +1,21 @@
lines = open('input').read().splitlines()
arr = []
_ = []
for line in lines:
if line == '':
arr.append(_)
_ = []
else:
_.append(list(line))
arr.append(_)
c = 0
for group in arr:
s = []
for p in group:
s = s+p
# print(group)
# print(s)
c += len(set(s))
print(c)

21
6/b.py Normal file
View File

@ -0,0 +1,21 @@
lines = open('input').read().splitlines()
arr = []
_ = []
for line in lines:
if line == '':
arr.append(_)
_ = []
else:
_.append(list(line))
arr.append(_)
c = 0
for group in arr:
s = set(group[0])
print(group)
for p in group:
s = s & set(p)
print(s)
c += len(s)
print(c)

31
7/a.py Normal file
View File

@ -0,0 +1,31 @@
lines = open('input').read().replace(' bags','').replace(' bag','').replace('.','').splitlines()
empties = []
dictionary = {}
for line in lines:
r = line.split(' contain ')
if 'no other' in line or r[0] == 'shiny gold':
dictionary[r[0]] = {}
empties.append(r[0])
else:
dictionary[r[0]] = {y[1]: int(y[0]) for x in r[1].split(', ') if (y := x.split(' ',1))}
for i in range(1,10): #Ten because why not? Not breaking at the amount of passes I need for this
for u in dictionary:
# print(u)
# print(dictionary[u])
a = {}
for v in dictionary[u]:
k = dictionary[u][v]
if v not in empties:
a.update({x:y*k for x in dictionary[v] if (y := dictionary[v][x])})
else:
a.update({v:k})
dictionary[u] = a
c = 0
for k in dictionary:
if 'shiny gold' in dictionary[k].keys():
c += 1
print(k)
print(dictionary[k]['shiny gold'])
print(c)

24
7/b.py Normal file
View File

@ -0,0 +1,24 @@
lines = open('input').read().replace(' bags','').replace(' bag','').replace('.','').splitlines()
empties = ['base']
dictionary = {}
for line in lines:
r = line.split(' contain ')
if 'no other' in line:
dictionary[r[0]] = {}
empties.append(r[0])
else:
dictionary[r[0]] = {y[1]: int(y[0]) for x in r[1].split(', ') if (y := x.split(' ',1))}
def f(li,count):
for u in li:
k = li[u]
if u not in empties:
tmp = {x:y*k for x in dictionary[u] if (y := dictionary[u][x])}
tmp.update({'base':k})
count = f(tmp,count)
else:
count += k
return count
print( f(dictionary['shiny gold'],0) )