diff options
| author | Gustav Sörnäs <gusso230@student.liu.se> | 2019-12-06 14:00:16 +0100 |
|---|---|---|
| committer | Gustav Sörnäs <gusso230@student.liu.se> | 2019-12-06 14:01:38 +0100 |
| commit | 8555fc71d0ba45760a16fafc785a7b6631336801 (patch) | |
| tree | 56b8c066c661fee52eda1b0d2d33d32d70c55e3f /solutions/py/06.py | |
| parent | 0b4082007d096b84fdf83ca00a4331c1a171260f (diff) | |
| download | aoc-8555fc71d0ba45760a16fafc785a7b6631336801.tar.gz | |
Day 6 py
Diffstat (limited to 'solutions/py/06.py')
| -rw-r--r-- | solutions/py/06.py | 91 |
1 files changed, 91 insertions, 0 deletions
diff --git a/solutions/py/06.py b/solutions/py/06.py new file mode 100644 index 0000000..660093c --- /dev/null +++ b/solutions/py/06.py @@ -0,0 +1,91 @@ +from typing import Iterable +import sys + +def flatten(l): + # https://stackoverflow.com/a/40857703 + for v in l: + if isinstance(v, Iterable): + yield from flatten(v) + else: + yield v + +class Tree(object): + # https://stackoverflow.com/a/28015122 + def __init__(self, name='root', children=None): + self.name = name + self.children = [] + self.parent = None + if children is not None: + for child in children: + self.add_child(child) + def add_child(self, node): + self.children.append(node) + node.parent = self + + def __eq__(self, other): + if isinstance(other, Tree): + return self.name == other.name + return False + def parents(self): + if self.parent == None: + return [] + return [self.parent, self.parent.parents()] + + +if __name__ == "__main__": + lines = open("../input/06", "r").readlines() + + trees = {} + seen = set() # seen trees. contains only IDs + leafs = {} + + for orbit in lines: + orbit = orbit.rstrip().split(")") + #print(orbit) + if not orbit[0] in seen: + trees[orbit[0]] = Tree(orbit[0]) + seen.add(orbit[0]) + + # hook up each child to its parent (since every parent now exists) + for orbit in lines: + orbit = orbit.rstrip().split(")") + if orbit[1] in seen: + trees[orbit[0]].add_child(trees[orbit[1]]) + else: + leafs[orbit[1]] = Tree(orbit[1]) + trees[orbit[0]].add_child(leafs[orbit[1]]) + for k in trees: + pass + #print(trees[k], trees[k].children) + + trees.update(leafs) + + sum_depths = 0 + depth = 0 + depths = {} + def count_children(tree, depth): + global sum_depths # fusk-rekursion? ville bara att det skulle fungera + sum_depths += depth + depths[tree.name] = depth + for child in tree.children: + count_children(child, depth+1) + count_children(trees["COM"], 0) + print("1:", sum_depths) + + parents = {} + for tree in trees: + parents[tree] = list(flatten(trees[tree].parents())) + + # find common parent and distance to it + src = trees["YOU"].parent.name + tar = trees["SAN"].parent.name + src_dist = 0 + for src_parent in parents[src]: + src_dist += 1 + tar_dist = 0 + for tar_parent in parents[tar]: + tar_dist += 1 + if src_parent == tar_parent: + print("2:", src_parent.name, src_dist, tar_dist, src_dist + tar_dist) + sys.exit() + |
