File tree Expand file tree Collapse file tree 4 files changed +87
-0
lines changed Expand file tree Collapse file tree 4 files changed +87
-0
lines changed Original file line number Diff line number Diff line change
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 )
Original file line number Diff line number Diff line change
1
+ with open ('input' , 'r' ) as raw :
2
+ polymer = raw .read ().strip ()
3
+
4
+ i = 0
5
+
6
+ while i != len (polymer ) - 1 :
7
+ x ,y = polymer [i ], polymer [i + 1 ]
8
+ if x .lower () == y .lower () and x != y :
9
+ polymer = polymer [:i ] + polymer [i + 2 :]
10
+ i -= 1
11
+ else :
12
+ i += 1
13
+
14
+ print (len (polymer ))
Original file line number Diff line number Diff line change
1
+ from math import inf
2
+
3
+ with open ('input' , 'r' ) as raw :
4
+ polymer = raw .read ().strip ()
5
+
6
+ l = inf
7
+
8
+ for char in set (polymer .lower ()):
9
+ poly = polymer .replace (char , '' ).replace (char .upper (), '' )
10
+
11
+ i = 0
12
+
13
+ while i != len (poly ) - 1 :
14
+ x ,y = poly [i ], poly [i + 1 ]
15
+ if x .lower () == y .lower () and x != y :
16
+ poly = poly [:i ] + poly [i + 2 :]
17
+ i -= 1
18
+ else :
19
+ i += 1
20
+
21
+ h = len (poly )
22
+
23
+ if h < l :
24
+ l = h
25
+
26
+ print (l )
File renamed without changes.
You can’t perform that action at this time.
0 commit comments