16_auntsue.py 836 B

123456789101112131415161718192021222324252627282930
  1. #!/usr/bin/env python3
  2. import sys
  3. from operator import eq, gt, lt
  4. fields = ['children', 'cats', 'samoyeds', 'pomeranians', 'akitas',
  5. 'vizslas', 'goldfish', 'trees', 'cars', 'perfumes']
  6. def parse(f):
  7. for line in f:
  8. left, right = line.rstrip().split(': ', 1)
  9. sig = [None] * len(fields)
  10. for prop in right.split(', '):
  11. key, val = prop.split(': ')
  12. sig[fields.index(key)] = int(val)
  13. yield int(left[4:]), sig
  14. def find_sue(needle, haystack):
  15. for sue, sig in haystack:
  16. if all(s is None or op(s, n) for s, (op, n) in zip(sig, needle)):
  17. return sue
  18. sues = list(parse(sys.stdin))
  19. sig = [(eq, n) for n in [3, 7, 2, 3, 0, 0, 5, 3, 2, 1]]
  20. print(find_sue(sig, sues))
  21. sig[1] = gt, 7
  22. sig[7] = gt, 3
  23. sig[3] = lt, 3
  24. sig[6] = lt, 5
  25. print(find_sue(sig, sues))