Working out solutions for 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.

преди 6 години
1234567891011121314151617181920212223242526272829303132333435363738
  1. from collections import defaultdict
  2. lines = open('04in.txt').read().split('\n')
  3. lines.sort()
  4. def parseTime(line):
  5. words = line.split()
  6. date, time = words[0][1:], words[1][:-1]
  7. return int(time.split(':')[1])
  8. C = defaultdict(int)
  9. CM = defaultdict(int)
  10. guard = None
  11. asleep = None
  12. for line in lines:
  13. if line:
  14. time = parseTime(line)
  15. if 'begins shift' in line:
  16. guard = int(line.split()[3][1:])
  17. asleep = None
  18. elif 'falls asleep' in line:
  19. asleep = time
  20. elif 'wakes up' in line:
  21. for t in range(asleep, time):
  22. CM[(guard, t)] += 1
  23. C[guard] += 1
  24. def argmax(d):
  25. best = None
  26. for k,v in d.items():
  27. print("{}: {}".format(k,v))
  28. if best is None or v > d[best]:
  29. best = k
  30. return best
  31. best_guard, best_min = argmax(CM)
  32. print (best_guard, best_min)
  33. print (best_guard * best_min)