04_rooms.py 784 B

12345678910111213141516171819202122232425262728293031
  1. #!/usr/bin/env python3
  2. import sys
  3. from collections import defaultdict
  4. def parse(f):
  5. for line in f:
  6. name, rest = line.rstrip().rsplit('-', 1)
  7. sector, checksum = rest[:-1].split('[')
  8. yield name.replace('-', ''), int(sector), checksum
  9. def is_real(name, checksum):
  10. count = defaultdict(int)
  11. for char in name:
  12. count[char] -= 1
  13. check = ''.join(sorted(count, key=lambda x: (count[x], x))[:5])
  14. return check == checksum
  15. def decrypt(name, sector):
  16. return ''.join(chr((ord(c) - 97 + sector) % 26 + 97) for c in name)
  17. rooms = list(parse(sys.stdin))
  18. print(sum(s for n, s, c in rooms if is_real(n, c)))
  19. for name, sector, checksum in rooms:
  20. if decrypt(name, sector) == 'northpoleobjectstorage':
  21. print(sector)
  22. break