Working out solutions for Advent of Code
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import re
  2. def main(lines):
  3. """ Sort the input by date/time, then find the best guard/minute combination
  4. Note: the example answer is WRONG!
  5. """
  6. # [1518-09-25 00:28] falls asleep
  7. # Don't actually care about the year, since it's always the same
  8. # grp1 grp2 grp3 grp4 grp5
  9. pattern = re.compile("\[[0-9]+-([0-9]+)-([0-9]+) ([0-9]+):([0-9]+)\] (.+)")
  10. guardptn = re.compile("Guard #([0-9]+) begins shift")
  11. entries = []
  12. guards = {}
  13. for line in lines:
  14. m = pattern.match(line)
  15. g = guardptn.match(m.group(5))
  16. guard = -1 if g == None else int(g.group(1))
  17. # guard # month # day # hour # minute # activity
  18. entries.append([guard, int(m.group(1)), int(m.group(2)), int(m.group(3)), int(m.group(4)), m.group(5)])
  19. entries.sort(key = lambda x: (x[1], x[2], -x[3], x[4]))
  20. for i in range(1, len(entries)):
  21. if entries[i][0] == -1:
  22. entries[i][0] = entries[i-1][0]
  23. with open("04out.txt", "w") as ofile:
  24. for entry in entries:
  25. ofile.write("{}\n".format(entry))
  26. # so entries is
  27. # [0, 1, 2, 3, 4, 5]
  28. # #guard # month #day #hour #minute #activity
  29. stt = -1
  30. end = -1
  31. for i in range(1,len(entries)):
  32. sleeping = False
  33. cguard = entries[i][0]
  34. #print(cguard)
  35. if cguard == entries[i-1][0]:
  36. if entries[i][5] == "falls asleep":
  37. stt = entries[i][4]
  38. #print("{} fell asleep at {}".format(cguard, stt))
  39. sleeping = True
  40. elif entries[i][5] == "wakes up":
  41. end = entries[i][4]
  42. sleeping = False
  43. #print("{} woke up at {} (fell asleep at {})".format(cguard, end, stt))
  44. for q in range(stt, end):
  45. if cguard not in guards.keys():
  46. guards[cguard] = {}
  47. guards[cguard][q] = 1
  48. else:
  49. if q not in guards[cguard].keys():
  50. guards[cguard][q] = 1
  51. else:
  52. guards[cguard][q] = guards[cguard][q] + 1
  53. else:
  54. stt = -1
  55. end = -1
  56. gslp = {}
  57. for k,v in guards.items():
  58. maxsleeps = 0
  59. maxmin = 0
  60. for minute, sleeps in v.items():
  61. if sleeps > maxsleeps:
  62. maxsleeps = sleeps
  63. maxmin = minute
  64. print("{} slept the most times ({}) at minute {}".format(k, maxsleeps, maxmin))
  65. gslp[k] = [maxmin, maxsleeps]
  66. tguard, tmin, tsleeps = 0, 0, 0
  67. for k,v in gslp.items():
  68. if v[1] > tsleeps:
  69. tguard, tmin, tsleeps = k, v[0], v[1]
  70. print("Guard {} slept the most times ({}) at minute {}. ID * minute = {}!".format(tguard, tsleeps, tmin, tguard*tmin))
  71. def add_sleeping(dct, mins):
  72. pass
  73. if __name__ == "__main__":
  74. lines = []
  75. with open("04in.txt","r") as f:
  76. for line in f:
  77. lines.append(line.strip())
  78. main(lines)