File tree Expand file tree Collapse file tree 1 file changed +34
-6
lines changed Expand file tree Collapse file tree 1 file changed +34
-6
lines changed Original file line number Diff line number Diff line change 1
- with open ('test.txt' ) as file :
2
- data = file .read ().strip ().splitlines ()
3
- data = [[int (y ) for y in x .split ()] for x in data ]
1
+ with open ('input.txt' ) as file :
2
+ data = [[int (y ) for y in x .split ()] for x in file .read ().strip ().splitlines ()]
4
3
5
- for row in data :
6
- increasing = None
4
+
5
+ def is_safe (row ):
6
+ last = None
7
7
increment = None
8
8
for each in row :
9
- if increasing is None and increment is None :
9
+ if last is None :
10
+ last = each
11
+ continue
12
+ elif increment is None :
13
+ inc = each - last
14
+ increment = inc > 0
15
+ if not 0 < abs (inc ) < 4 :
16
+ return False
17
+ else :
18
+ inc = each - last
19
+ if (inc > 0 ) != increment or not 0 < abs (inc ) < 4 :
20
+ return False
21
+ last = each
22
+ return True
23
+
24
+
25
+ total = 0
26
+ save_with_removals = 0
27
+ for row in data :
28
+ if is_safe (row ):
29
+ total += 1
30
+ else :
31
+ for i in range (len (row )):
32
+ if is_safe ([row [y ] for y in range (len (row )) if y != i ]):
33
+ save_with_removals += 1
34
+ break
35
+
36
+ print (total )
37
+ print (total + save_with_removals )
You can’t perform that action at this time.
0 commit comments