#!/bin/python3 import sys, re from pprint import pprint input_f = 'test' part = 2 ######################################### # # # Part 1 # # # ######################################### if part == 1: children = [] mains = [] with open(input_f)as file: for line in file: l = line.rstrip() if '->' in l: x = l.split('->') for i in x[1].split(','): children.append(i.strip()) match = re.match(r"^(\S+)",x[0]) if match: mains.append(match.group(1)) for x in mains: if x not in children: print(x) ######################################### # # # Part 2 # # # ######################################### if part == 2: children = [] mains = [] with open(input_f)as file: for line in file: l = line.rstrip() if '->' in l: x = l.split('->') for i in x[1].split(','): children.append(i.strip()) match = re.match(r"^(\S+)",x[0]) if match: mains.append(match.group(1))