-
-
Save dencorg/ce685d1e0a3031c90333c15568025ccb to your computer and use it in GitHub Desktop.
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
# Advent of code 2019 6 solution | |
# using anytree ([https://anytree.readthedocs.io/en/latest/](https://anytree.readthedocs.io/en/latest/)) | |
from collections import defaultdict | |
from anytree import Node, Walker | |
orbit_map = defaultdict(list) | |
orbit_set = set() | |
orbit_tree = {} | |
# input_map is the input list provided | |
for item in input_map: | |
parts = item.split(')') | |
orbit_set.add(parts[0]) | |
orbit_set.add(parts[1]) | |
orbit_map[parts[0]].append(parts[1]) | |
for i in orbit_set: | |
orbit_tree[i] = Node(i) | |
# construct tree - add children | |
for key, values in orbit_map.items(): | |
orbit_tree[key].children = [orbit_tree[i] for i in values] | |
# part 1 | |
total = 0 | |
for node in orbit_tree: | |
total += orbit_tree[node].depth | |
print('Total orbits', total) | |
# part 2 | |
you = orbit_tree['YOU'] | |
san = orbit_tree['SAN'] | |
w = Walker() | |
route = w.walk(you, san) | |
transfers = len(route[0]) + len(route[2]) - 2 | |
print('Total transfers', transfers) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment