#!/usr/bin/env python3 from sys import argv from math import prod i = open(argv[1]).read().split("\n\n") r = {x:{c for z in y.split(" or ") for a,b in [map(int, z.split("-"))] for c in range(a,b+1)} for x,y in [x.split(": ") for x in i[0].strip().split("\n")]} m = [int(x) for x in i[1].strip().split("\n",1)[1].split(",")] n = [[int(y) for y in x.split(",")] for x in i[2].strip().split("\n")[1:]] e = 0 p = {x:{*range(len(m))} for x in r} for t in n: for f in t: for l in r: if f in r[l]: break else: e += f break else: for i,f in enumerate(t): for l in r: if f not in r[l]: p[l].remove(i) print(e) d = set() for l,f in sorted(p.items(), key=lambda x: len(x[1])): p[l] -= d d |= f print(prod(m[list(p[l])[0]] for l in p if l.startswith("departure ")))