ref: 15fb556dc51521b8506ec5959381d1910cbb83ed
2019/006/main.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
from functools import reduce data = """COM)B B)C C)D D)E E)F B)G G)H D)I E)J J)K K)L""" data = """COM)B B)C C)D D)E E)F B)G G)H D)I E)J J)K K)L K)YOU I)SAN""" def add(a, b): return a + b def assert_root(lines): orbitors = [] orbitees = [] for line in lines: a, b = line.split(')') orbitors.append(b) orbitees.append(a) orbitors = set(orbitors) orbitees = set(orbitees) root = orbitees.difference(orbitors) assert len(root) == 1 assert 'COM' in root def count(x, k, n=0): c = 0 c += n for item in x.get(k, []): c += count(x, item, n + 1) return c def path(x, k): if k == 'COM': return [] return [x[k]] + path(x, x[k]) def main(): x = {} data = open('input').read() lines = data.splitlines() assert_root(lines) for line in lines: a, b = line.split(')') x[b] = a you_path = path(x, 'YOU') san_path = path(x, 'SAN') you_path.reverse() san_path.reverse() common = [] for y, s in zip(you_path, san_path): if y == s: common.append(y) else: break print(you_path) print(san_path) print(common) res = (len(you_path) - len(common)) + (len(san_path) - len(common)) print(res) if __name__ == '__main__': main() |