From c2739620813b86dd8f5b862daff8c5e74f5cd306 Mon Sep 17 00:00:00 2001 From: randomuser Date: Tue, 20 Jul 2021 12:55:49 -0500 Subject: [PATCH] add solution 7, parts 1 and 2 --- 2020/7/__pycache__/solution.cpython-39.pyc | Bin 0 -> 2059 bytes 2020/7/paste | 15 ++++++ 2020/7/sample_input | 9 ++++ 2020/7/solution.py | 52 +++++++++++++++++++++ 4 files changed, 76 insertions(+) create mode 100644 2020/7/__pycache__/solution.cpython-39.pyc create mode 100644 2020/7/paste create mode 100644 2020/7/sample_input create mode 100644 2020/7/solution.py diff --git a/2020/7/__pycache__/solution.cpython-39.pyc b/2020/7/__pycache__/solution.cpython-39.pyc new file mode 100644 index 0000000000000000000000000000000000000000..1af0e25d74faf14c2f73d25a4b7707eccdaa48ca GIT binary patch literal 2059 zcmZ`)&u<$=6rP#gwb#xjX+zpHP$W|d!cgMSOF2|k1QAFatcvzzDOwiK#M#8&-OQ{j z)i|ewKY;(hIp$xQE2o^f^~CpPlei9Ll9_#Pe!cg;_sw&%yxe4Hk5(^#`z2uPZ&KcG zm}5KWW`f~oCYj`C=%=Dbnvfne4;q)g49-|j$eOI<>nUG`vVoOAHs#V8Q^Blu&UV=; z@6`va7pnR$+gs~3R5)uYt{SKOoO5>kQY|S@g^J1O6jn|#CzoHb_DUc27n*>!gKi#T zPz)ZBbuI;2m!9!<5eu);T{BQz6ABoAGm|pH%Xc z#Wd?}GDu#wek0SYU(HGeLqYc)Z(ZUxcFAcVi*4TqrWmHBt*0g#76*y-5_xEYem=}~ z+q1q=!+q-y(@g0a4qPxD;q&g>&Ots>ow3oKL0Wba`9@`BzTeNK>O9?ky4`u!G5K&@ zrg^qiytQE*r&(IYv5&)sZeYC0BkH2bClL_Xy57ePeO9Y>I5yP)d#j&kWs+tsO(ALuak8A{t-L%?n$lq=;z_F=IObRn z6E@W9c${iwY%M7Yl}U#Owzw26;f7xQ>Q=o0&GPhC;vC?sSTaNhq7e~kh1Yn<*ZE|b zP?>YdC6@M~k$>c z^7W4NUNPyrXEl%h^-pcjx$TJODA@L^(C)*Y_4-+)&TBj5hs{a|*uAe~U%v>#W+cmvL z2S1`=od!quPe`K5_TG(i&fmiVZ8PKuINt~xn&uX`N{OL_IFDNprbx#W1@Zy=6BTX> z?n<$Nl1|zNV!80TbD*8wwxGgfO6wK5dCwE~yraUXvCOp^hSl8@P`qQdKsI(KaY_3i3Oa%mIf@%N& literal 0 HcmV?d00001 diff --git a/2020/7/paste b/2020/7/paste new file mode 100644 index 0000000..8c2c586 --- /dev/null +++ b/2020/7/paste @@ -0,0 +1,15 @@ + File "/home/usr/git/adventofcode/2020/7/solution.py", line 52, in + print(bagconfigurations(rules, Bag('shiny gold'))) + File "/home/usr/git/adventofcode/2020/7/solution.py", line 42, in bagconfigurations + cnt += bagconfigurations(rules, i.bagspec()) + File "/home/usr/git/adventofcode/2020/7/solution.py", line 42, in bagconfigurations + cnt += bagconfigurations(rules, i.bagspec()) + File "/home/usr/git/adventofcode/2020/7/solution.py", line 42, in bagconfigurations + cnt += bagconfigurations(rules, i.bagspec()) + [Previous line repeated 994 more times] + File "/home/usr/git/adventofcode/2020/7/solution.py", line 40, in bagconfigurations + if i.cancontain(bag): cnt += 1 + File "/home/usr/git/adventofcode/2020/7/solution.py", line 27, in cancontain + if not i.color == bag.color: continue +RecursionError: maximum recursion depth exceeded in comparison + diff --git a/2020/7/sample_input b/2020/7/sample_input new file mode 100644 index 0000000..1cec74f --- /dev/null +++ b/2020/7/sample_input @@ -0,0 +1,9 @@ +light red bags contain 1 bright white bag, 2 muted yellow bags. +dark orange bags contain 3 bright white bags, 4 muted yellow bags. +bright white bags contain 1 shiny gold bag. +muted yellow bags contain 2 shiny gold bags, 9 faded blue bags. +shiny gold bags contain 1 dark olive bag, 2 vibrant plum bags. +dark olive bags contain 3 faded blue bags, 4 dotted black bags. +vibrant plum bags contain 5 faded blue bags, 6 dotted black bags. +faded blue bags contain no other bags. +dotted black bags contain no other bags. diff --git a/2020/7/solution.py b/2020/7/solution.py new file mode 100644 index 0000000..e40fcff --- /dev/null +++ b/2020/7/solution.py @@ -0,0 +1,52 @@ +#!/usr/bin/env python + +class Bag: + def __init__(self, line): + split = line.split(' ') + if split[0].isalpha(): + self.adj = split[0] + self.color = split[1] + else: + self.adj = split[1] + self.color = split[2] + def __str__(self): + return "{} {} bag".format(self.adj, self.color) +class Rule: + def __init__(self, line): + contain = line.split(' contain ') + self.bag = Bag(contain[0]) + if contain[1] != 'no other bags': + requires = contain[1].split(', ') + self.requires = [] + for i in requires: + self.requires.append(Bag(i)) + else: self.requires = [] + def cancontain(self, bag): + cnt = 0 + for i in self.requires: + if not i.color == bag.color: continue + if not i.adj == bag.adj: continue + cnt += 1 + + if cnt > 0: return True + return False + def bagspec(self): + return self.bag +def pseudoremove(list, pos): + return list[0:pos - 1] + list[pos + 1:] +def bagconfigurations(rules, bag): + cnt = 0 + for i in rules: + if i.cancontain(bag): cnt += 1 + for j in rules: + if j.cancontain(i.bagspec()): cnt += 1 + + return cnt + +fd = open('sample_input', 'r') +lines = [i.rstrip() for i in fd.readlines()] +rules = [] + +for i in lines: + rules.append(Rule(i)) +print(bagconfigurations(rules, Bag('shiny gold')))