-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathday4.py
94 lines (72 loc) · 2.19 KB
/
day4.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
from datetime import datetime
from typing import List
from collections import defaultdict, Counter
GUARD = 0
SLEEP = 1
WAKE = 2
def parse_input(filename: str):
lines = [line.strip() for line in open(filename).readlines()]
log = []
for line in lines:
date_str, time_str, p1, p2, *p3 = line.split(' ')
time_stamp = datetime.strptime((date_str + ' ' + time_str)[1:-1], '%Y-%m-%d %H:%M')
if p1 == 'Guard':
action = GUARD
value = int(p2[1:])
elif p1 == 'falls':
action = SLEEP
value = time_stamp.minute
else:
action = WAKE
value = time_stamp.minute
log.append([time_stamp, action, value])
log.sort()
return log
def part1(guards: dict) -> int:
max_sleep = 0
sleepiest_guard = 0
for guard in guards:
if len(guards[guard]) > max_sleep:
max_sleep = len(guards[guard])
sleepiest_guard = guard
max_count = 0
max_value = 0
counts = Counter(guards[sleepiest_guard])
for val in counts:
if counts[val] > max_count:
max_count = counts[val]
max_value = val
return sleepiest_guard * max_value
def part2(guards: dict) -> int:
max_count = 0
max_guard = 0
for guard in guards:
counts = Counter(guards[guard])
max_sleep = max(counts.values())
if max_sleep > max_count:
max_count = max_sleep
max_guard = guard
counts = Counter(guards[max_guard])
for minute in counts:
if counts[minute] == max_count:
return minute * max_guard
return 0
def parse_log(log: List[List]) -> dict:
current_guard = 0
sleep = 0
guards = defaultdict(lambda: [])
for _, action, value in log:
if action == GUARD:
current_guard = value
elif action == SLEEP:
sleep = value
elif action == WAKE:
guards[current_guard] += [d for d in range(sleep, value)]
return dict(guards)
def main():
log = parse_input('input/day4.txt')
guards = parse_log(log)
print(f'Part 1: {part1(guards)}')
print(f'Part 2: {part2(guards)}')
if __name__ == "__main__":
main()