File tree 2 files changed +49
-0
lines changed
2 files changed +49
-0
lines changed Original file line number Diff line number Diff line change
1
+ #!/usr/bin/env python
2
+
3
+ class Report :
4
+ def __init__ (self , line ):
5
+ self .levels = [int (i ) for i in line .split ()]
6
+ self .gen_differences ()
7
+
8
+ def gen_differences (self ):
9
+ self .differences = []
10
+ for i in range (len (self .levels ) - 1 ):
11
+ self .differences .append (self .levels [i + 1 ] - self .levels [i ])
12
+
13
+ def safe_p (self ):
14
+ if not (all ([d > 0 for d in self .differences ]) or
15
+ all ([d < 0 for d in self .differences ])):
16
+ return False
17
+
18
+ return all ([abs (d ) >= 1 and abs (d ) <= 3 for d in self .differences ])
19
+
20
+
21
+ lines = [row .strip () for row in open ('02.input' ).readlines ()]
22
+
23
+ print ([Report (line ).safe_p () for line in lines ].count (True ))
Original file line number Diff line number Diff line change
1
+ #!/usr/bin/env python
2
+
3
+ from itertools import combinations
4
+
5
+ class Report :
6
+ def __init__ (self , line ):
7
+ self .levels = [int (i ) for i in line .split ()]
8
+
9
+ def safe_levels_p (self , levels ):
10
+ differences = []
11
+ for i in range (len (levels ) - 1 ):
12
+ differences .append (levels [i + 1 ] - levels [i ])
13
+ if not (all ([d > 0 for d in differences ]) or
14
+ all ([d < 0 for d in differences ])):
15
+ return False
16
+ return all ([abs (d ) >= 1 and abs (d ) <= 3 for d in differences ])
17
+
18
+ def safe_p (self ):
19
+ if self .safe_levels_p (self .levels ):
20
+ return True
21
+ return any ([self .safe_levels_p (l ) for l in combinations (self .levels , len (self .levels ) - 1 )])
22
+
23
+
24
+ lines = [row .strip () for row in open ('02.input' ).readlines ()]
25
+
26
+ print ([Report (line ).safe_p () for line in lines ].count (True ))
You can’t perform that action at this time.
0 commit comments