Last active
December 16, 2020 17:34
-
-
Save halitanildonmez/ada45ae8bf8e046a16d925c42e8b2627 to your computer and use it in GitHub Desktop.
Solution for Advent of Code Day 7 Challenge
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def advent_day_seven_bag_content(statement): | |
so = statement.split(', ') | |
res = [] | |
for s in so: | |
s = s.replace('bags', '').replace('bag', '').replace('.', '') | |
s = s.lstrip().rstrip() | |
if s != 'no other': | |
pair = (s[2:], s[0]) | |
res.append(pair) | |
return res | |
def advent_day_seven_dfs(vals, k): | |
if k not in vals: | |
return 0 | |
s = 0 | |
for m in vals[k]: | |
s += int(m[1]) + (int(m[1]) * advent_day_seven_dfs(vals, m[0])) | |
return s | |
def advent_day_seven_part_two(vals, kn): | |
t = 0 | |
for n in vals['shiny gold']: | |
n_a = [] | |
dict_test = dict() | |
advent_day_seven_part_two_count(vals, n[0], n_a, dict_test) | |
print(n, dict_test) | |
print('end') | |
# undirected graph, id per line, | |
def advent_day_seven(): | |
all_vals = dict() | |
name_map = dict() | |
name_id = 0 | |
with open('input.txt', 'r') as file: | |
lst = file.read().split('\n') | |
for l in lst: | |
sp = l.split(' bags contain ') | |
k = str(sp[0]) | |
if len(sp) > 1: | |
all_vals[k] = advent_day_seven_bag_content(sp[1].lstrip()) | |
co = 0 | |
for v in all_vals: | |
if v == 'shiny gold': | |
continue | |
if advent_path_exists(all_vals, v): | |
co += 1 | |
rer = advent_day_seven_dfs(all_vals, 'shiny gold') | |
print(rer) | |
if __name__ == '__main__': | |
advent_day_seven() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment