From 088cf71aa2225e6015986b9c482ccfb62ad3b031 Mon Sep 17 00:00:00 2001 From: daviddoji Date: Tue, 8 Mar 2022 21:14:40 +0100 Subject: [PATCH] Solution to problem 6 part 1 in Python --- src/Year_2019/P6.py | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/src/Year_2019/P6.py b/src/Year_2019/P6.py index 582be01..70d1784 100644 --- a/src/Year_2019/P6.py +++ b/src/Year_2019/P6.py @@ -68,24 +68,30 @@ # What is the total number of direct and indirect orbits in your map data? -with open("files/test") as f: +with open("files/P6.txt") as f: orbits = [line for line in f.read().strip().split()] -# print(orbits) from collections import defaultdict -planets = defaultdict(list) -for orbit in orbits: - planet, satellite = orbit.split(")") - planets[planet].append(satellite) - -def count_orbits(node, counter, total_sum): +def count_orbits(universe, node, counter, total_sum): total_sum += counter - for satellite in planets[node]: - total_sum = count_orbits(satellite, counter + 1, total_sum) + for satellite in universe[node]: + total_sum = count_orbits(universe, satellite, counter + 1, total_sum) return total_sum -print(count_orbits("COM", 0, 0)) +def part_1(): + universe = defaultdict(list) + for orbit in orbits: + planet, satellite = orbit.split(")") + universe[planet].append(satellite) + + total_orbits = count_orbits(universe, "COM", 0, 0) + + print(f"There are {total_orbits} in our universe") + + +if __name__ == "__main__": + part_1()