aoc

Advent of Code Solutions
git clone git://git.alexkarle.com.com/aoc
Log | Files | Refs | README | LICENSE

1.py (654B) [raw]


      1 #!/usr/bin/env python3
      2 import sys
      3 from collections import defaultdict
      4 
      5 rules = defaultdict(set)
      6 prints = []
      7 
      8 in_rules = True
      9 for l in sys.stdin:
     10     l = l.strip()
     11     if l == "":
     12         in_rules = False
     13         continue
     14     if in_rules:
     15         (l, r) = l.split("|")
     16         rules[r].add(l)
     17     else:
     18         prints.append(l.split(","))
     19 
     20 tot = 0
     21 for pr in prints:
     22     passed = True
     23     all = set(pr)
     24     seen = set()
     25     for pg in pr:
     26         seen.add(pg)
     27         for dep in rules[pg]:
     28             if dep in all and dep not in seen:
     29                 passed = False
     30                 break
     31 
     32     if passed:
     33         tot += int(pr[int(len(pr)/2)])
     34 
     35 print(tot)