diff options
| author | Yigit Sever | 2021-12-13 10:38:11 +0300 | 
|---|---|---|
| committer | Yigit Sever | 2021-12-13 10:38:11 +0300 | 
| commit | 74b27ccca31bb757c737dd7fdc02f513f57561b2 (patch) | |
| tree | e27db4cd0873c81a53d32277446d926d176304e0 /2020/day7/haversacks.py | |
| parent | 3919f90cfbfbba26c8e39f979280649f5e08aea8 (diff) | |
| parent | ac8125750abed263619da4cc6d653bb5ab76f007 (diff) | |
| download | aoc-74b27ccca31bb757c737dd7fdc02f513f57561b2.tar.gz aoc-74b27ccca31bb757c737dd7fdc02f513f57561b2.tar.bz2 aoc-74b27ccca31bb757c737dd7fdc02f513f57561b2.zip | |
Merge remote-tracking branch 'origin/main'
Diffstat (limited to '2020/day7/haversacks.py')
| -rw-r--r-- | 2020/day7/haversacks.py | 35 | 
1 files changed, 35 insertions, 0 deletions
| diff --git a/2020/day7/haversacks.py b/2020/day7/haversacks.py new file mode 100644 index 0000000..4f7292d --- /dev/null +++ b/2020/day7/haversacks.py | |||
| @@ -0,0 +1,35 @@ | |||
| 1 | import networkx as nx | ||
| 2 | import re | ||
| 3 | |||
| 4 | bagtree = nx.DiGraph() | ||
| 5 | bagremover = re.compile(r" bags?\.?$") | ||
| 6 | numremover = re.compile(r"^\d+ ") | ||
| 7 | |||
| 8 | with open("input", "r") as baglines: | ||
| 9 | for line in baglines: | ||
| 10 | (miniroot, child_str) = list(map(str.strip, line.split("contain"))) | ||
| 11 | |||
| 12 | miniroot = miniroot.replace(" bags", "") | ||
| 13 | |||
| 14 | children = list( | ||
| 15 | map( | ||
| 16 | lambda b: re.sub(numremover, "", b), | ||
| 17 | list( | ||
| 18 | map( | ||
| 19 | lambda a: re.sub(bagremover, "", a), | ||
| 20 | list(map(str.strip, child_str.split(","))), | ||
| 21 | ) | ||
| 22 | ), | ||
| 23 | ) | ||
| 24 | ) | ||
| 25 | |||
| 26 | if "no other" in children: | ||
| 27 | continue | ||
| 28 | |||
| 29 | for kid in children: | ||
| 30 | bagtree.add_edge(kid, miniroot) | ||
| 31 | |||
| 32 | lengths = dict(nx.all_pairs_shortest_path(bagtree)) | ||
| 33 | |||
| 34 | # we don't count the shiny gold itself | ||
| 35 | print(len(lengths["shiny gold"]) - 1) | ||
