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