from aoc.input import get_input import time import collections import re from aoc.partselector import part_one, part_two def pw(line): return line.strip().split(' ') def p1(): inp = get_input(pw) pwd = dict() for i in inp: key = ''.join(i[0:2]) data = [] for n in range (4, len(i), 4): if i[n] == 'no': continue data.append(''.join(i[n+1:n+3]).strip('., ')) pwd[''.join(key)] = data count = 0 def contains_gold(y): if 'shinygold' == y: return True else: ret = False for j in pwd[y]: ret = ret | contains_gold(j) return ret for j in pwd: ret = False for t in pwd[j]: ret |= contains_gold(t) if ret: count += 1 print(count) return count def p2(r): inp = get_input(pw) pwd = dict() for i in inp: key = ''.join(i[0:2]) data = [] for n in range (4, len(i), 4): if i[n] == 'no': continue data.append([int(i[n]), ''.join(i[n+1:n+3]).strip('., ')]) pwd[''.join(key)] = data count = 0 def count_bags(y): count = 1 for j in pwd[y]: tmpcount = count_bags(j[1]) count += tmpcount * j[0] return count ret = 0 for t in pwd['shinygold']: ret += count_bags(t[1]) * t[0] print(ret) return 0 if part_one(): start = time.time() result1 = p1() print(round(time.time() - start, 4), 's') if part_two(): start = time.time() p2(0) print(round(time.time() - start, 4), 's')