Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit aff2934

Browse files
author
sqdorte
committed
04/2 - python
1 parent fb5975c commit aff2934

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

2018/04_2/python/sqdorte.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
with open('input', 'r') as raw:
2+
i = sorted(raw.read().strip().split('\n'))
3+
4+
guard = None
5+
sleeping = False
6+
start = None
7+
change = False
8+
9+
guards = {}
10+
11+
for line in i:
12+
minute = int(line[15:17])
13+
14+
if line[19] == 'f':
15+
sleeping = True
16+
start = minute
17+
elif line[19] == 'w':
18+
sleeping = False
19+
change = True
20+
else:
21+
if sleeping:
22+
sleeping = False
23+
change = True
24+
25+
guard = line[26:].split(' ')[0]
26+
27+
if change:
28+
for i in range(start, minute):
29+
try:
30+
guards[guard][i] += 1
31+
except KeyError:
32+
try:
33+
guards[guard].update({i: 1})
34+
except KeyError:
35+
guards[guard] = {i: 1}
36+
change = False
37+
38+
h = []
39+
40+
for guard in guards:
41+
sleep = guards[guard]
42+
hour = list(zip(*sorted(zip(sleep.values(), sleep.keys()))))[1][-1]
43+
44+
h.append((sleep[hour], (hour, int(guard))))
45+
46+
guard,minute = list(zip(*sorted(h)))[-1][-1]
47+
print(guard*minute)

0 commit comments

Comments
 (0)