From 32cb11f3a9d0c8250ab074ede8d7664058df63f3 Mon Sep 17 00:00:00 2001 From: npc-strider Date: Mon, 7 Dec 2020 17:43:43 +0800 Subject: [PATCH] days 5,6,7 of advent of code --- 5/a.py | 32 ++++++++++++++++++++++++++++++++ 5/b.py | 38 ++++++++++++++++++++++++++++++++++++++ 6/a.py | 21 +++++++++++++++++++++ 6/b.py | 21 +++++++++++++++++++++ 7/a.py | 31 +++++++++++++++++++++++++++++++ 7/b.py | 24 ++++++++++++++++++++++++ 6 files changed, 167 insertions(+) create mode 100644 5/a.py create mode 100644 5/b.py create mode 100644 6/a.py create mode 100644 6/b.py create mode 100644 7/a.py create mode 100644 7/b.py diff --git a/5/a.py b/5/a.py new file mode 100644 index 0000000..2f3ab8f --- /dev/null +++ b/5/a.py @@ -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)) \ No newline at end of file diff --git a/5/b.py b/5/b.py new file mode 100644 index 0000000..fad0cee --- /dev/null +++ b/5/b.py @@ -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) \ No newline at end of file diff --git a/6/a.py b/6/a.py new file mode 100644 index 0000000..9ff8680 --- /dev/null +++ b/6/a.py @@ -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) \ No newline at end of file diff --git a/6/b.py b/6/b.py new file mode 100644 index 0000000..7b3340d --- /dev/null +++ b/6/b.py @@ -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) \ No newline at end of file diff --git a/7/a.py b/7/a.py new file mode 100644 index 0000000..beb2ea3 --- /dev/null +++ b/7/a.py @@ -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) \ No newline at end of file diff --git a/7/b.py b/7/b.py new file mode 100644 index 0000000..07a0a3f --- /dev/null +++ b/7/b.py @@ -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) )