summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--2018/aoc/day4.py26
-rw-r--r--2018/inputs/example.txt17
2 files changed, 37 insertions, 6 deletions
diff --git a/2018/aoc/day4.py b/2018/aoc/day4.py
index c0aad81..d0b6b74 100644
--- a/2018/aoc/day4.py
+++ b/2018/aoc/day4.py
@@ -1,14 +1,18 @@
1import re 1import re
2from collections import Counter
2from datetime import datetime 3from datetime import datetime
4from functools import reduce
3 5
4timestamp_pattern = re.compile(r'\[(?P<timestamp>.+)\]') 6timestamp_pattern = re.compile(r'\[(?P<timestamp>.+)\]')
5 7
6guard_pattern = re.compile(r'#(?P<id>[0-9]+)') 8guard_pattern = re.compile(r'#(?P<id>[0-9]+)')
7 9
10
8def parse_shifts(shift_list: list) -> iter: 11def parse_shifts(shift_list: list) -> iter:
9 shift = None 12 shift = None
10 felt_asleep = None 13 felt_asleep = None
11 for item in shift_list: 14 for item in shift_list:
15 print(item)
12 timestamp = datetime.strptime( 16 timestamp = datetime.strptime(
13 timestamp_pattern \ 17 timestamp_pattern \
14 .search(item) \ 18 .search(item) \
@@ -22,18 +26,28 @@ def parse_shifts(shift_list: list) -> iter:
22 yield shift 26 yield shift
23 shift = { 27 shift = {
24 'start': timestamp, 28 'start': timestamp,
25 'guard': guard_pattern.search(item).groupdict().get('id'), 29 'guard': int(guard_pattern.search(item).groupdict().get('id')),
26 'sleepytime': 0 30 'sleepytime': 0,
31 'sleepy_minutes': []
27 } 32 }
28 elif item.endswith('asleep'): 33 elif item.endswith('asleep'):
29 felt_asleep = timestamp 34 felt_asleep = timestamp
30 elif item.endswith('up'): 35 elif item.endswith('up'):
31 shift['sleepytime'] += int((timestamp - felt_asleep).seconds / 60) 36 shift['sleepy_minutes'] += list(range(felt_asleep.minute, timestamp.minute))
37 shift['sleepytime'] += timestamp.minute - felt_asleep.minute
32 felt_asleep = None 38 felt_asleep = None
33 39
34 40
41def main(input_file: list):
42 shifts = list(parse_shifts(sorted(input_file)))
43 shifts.sort(key=lambda s: s['sleepytime'], reverse=True)
44 most_sleepy_guard = shifts[0]['guard']
45 minutes_slept = reduce(lambda m, s: m + (
46 s['sleepy_minutes'] if s['guard'] == most_sleepy_guard else []
47 ), shifts, [])
48 minutes_slept_most = Counter(minutes_slept).most_common()
49 print(minutes_slept_most)
50
51 print(f'Guard {most_sleepy_guard} slept at minute {minutes_slept_most[0][0]}: {most_sleepy_guard * (minutes_slept_most[0][0] + 1)}')
35 52
36 53
37def main(input_file: list):
38 for shift in parse_shifts(sorted(input_file)):
39 print(shift)
diff --git a/2018/inputs/example.txt b/2018/inputs/example.txt
new file mode 100644
index 0000000..62c3a7a
--- /dev/null
+++ b/2018/inputs/example.txt
@@ -0,0 +1,17 @@
1[1518-11-01 00:00] Guard #10 begins shift
2[1518-11-01 00:05] falls asleep
3[1518-11-01 00:25] wakes up
4[1518-11-01 00:30] falls asleep
5[1518-11-01 00:55] wakes up
6[1518-11-01 23:58] Guard #99 begins shift
7[1518-11-02 00:40] falls asleep
8[1518-11-02 00:50] wakes up
9[1518-11-03 00:05] Guard #10 begins shift
10[1518-11-03 00:24] falls asleep
11[1518-11-03 00:29] wakes up
12[1518-11-04 00:02] Guard #99 begins shift
13[1518-11-04 00:36] falls asleep
14[1518-11-04 00:46] wakes up
15[1518-11-05 00:03] Guard #99 begins shift
16[1518-11-05 00:45] falls asleep
17[1518-11-05 00:55] wakes up \ No newline at end of file