optimizations
This commit is contained in:
@@ -5,71 +5,50 @@ import re
|
||||
from aoc.partselector import part_one, part_two
|
||||
|
||||
def pw(line):
|
||||
return line.strip().split(' ')
|
||||
l = re.split(" bags?,?(?: contain)? ?", line.strip('.'))
|
||||
return list(filter(len, l))
|
||||
|
||||
def p1():
|
||||
inp = get_input(pw)
|
||||
pwd = dict()
|
||||
for i in inp:
|
||||
key = ''.join(i[0:2])
|
||||
for sample in inp:
|
||||
key = sample[0]
|
||||
data = []
|
||||
for n in range (4, len(i), 4):
|
||||
if i[n] == 'no':
|
||||
for element in sample[1:]:
|
||||
if 'no' in element:
|
||||
continue
|
||||
data.append(''.join(i[n+1:n+3]).strip('., '))
|
||||
pwd[''.join(key)] = data
|
||||
data.append(element.split(' ', 1))
|
||||
pwd[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
|
||||
return 'shiny gold' == y or any(map(lambda x: contains_gold(x[1]), pwd[y]))
|
||||
|
||||
for j in pwd:
|
||||
ret = False
|
||||
for t in pwd[j]:
|
||||
ret |= contains_gold(t)
|
||||
if ret:
|
||||
count += 1
|
||||
s = sum(map(lambda x: any(map(lambda x: contains_gold(x[1]), pwd[x])), pwd))
|
||||
|
||||
|
||||
print(count)
|
||||
return count
|
||||
print(s)
|
||||
return pwd
|
||||
|
||||
|
||||
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
|
||||
|
||||
pwd = r
|
||||
count = 0
|
||||
|
||||
def count_bags(y):
|
||||
count = 1
|
||||
for j in pwd[y]:
|
||||
tmpcount = count_bags(j[1])
|
||||
count += tmpcount * j[0]
|
||||
count += tmpcount * int(j[0])
|
||||
return count
|
||||
|
||||
ret = 0
|
||||
for t in pwd['shinygold']:
|
||||
ret += count_bags(t[1]) * t[0]
|
||||
|
||||
for t in pwd['shiny gold']:
|
||||
ret += count_bags(t[1]) * int(t[0])
|
||||
|
||||
print(ret)
|
||||
return 0
|
||||
return ret
|
||||
|
||||
if part_one():
|
||||
start = time.time()
|
||||
@@ -79,7 +58,7 @@ if part_one():
|
||||
|
||||
if part_two():
|
||||
start = time.time()
|
||||
p2(0)
|
||||
p2(result1)
|
||||
print(round(time.time() - start, 4), 's')
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user