My solutions to 2018's advent of code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

04.py 735B

123456789101112131415161718192021
  1. from collections import defaultdict, Counter
  2. sleeps = defaultdict(Counter)
  3. with open('data/04.txt', 'r') as file:
  4. guard = sleep = 0
  5. for log in sorted(line.strip() for line in file):
  6. action = log[-5:]
  7. if action == 'shift':
  8. guard = log[26:-13]
  9. elif action == 'sleep':
  10. sleep = int(log[15:17])
  11. else:
  12. sleeps[guard].update(range(sleep, int(log[15:17])))
  13. most_sleep = max(sleeps.items(), key=lambda p: sum(p[1].values()))
  14. print(int(most_sleep[0]) * most_sleep[1].most_common(1)[0][0])
  15. sleepiest_minute = max([(guard, times.most_common(1)[0]) for (guard, times) in sleeps.items()], key=lambda p: p[1][1])
  16. print(int(sleepiest_minute[0]) * sleepiest_minute[1][0])