File tree 3 files changed +1049
-0
lines changed
3 files changed +1049
-0
lines changed Load Diff Large diffs are not rendered by default.
Original file line number Diff line number Diff line change
1
+ #!/usr/bin/python3
2
+
3
+ def parse_input (path :str ) -> list [tuple [int ]]:
4
+ with open (path ) as file :
5
+ data = file .read ().splitlines ()
6
+
7
+ reports = [tuple (int (i ) for i in line .split ()) for line in data ]
8
+
9
+ return reports
10
+
11
+ positive = lambda n : True if n > 0 else False
12
+
13
+ def count_safe (reports : list [tuple [int ]]) -> int :
14
+ safe = 0
15
+
16
+ for report in reports :
17
+ prev = report [0 ]
18
+ sign = None
19
+
20
+ for pos in report [1 :]:
21
+ dif = pos - prev
22
+
23
+ if abs (dif ) > 3 :
24
+ break
25
+ elif dif == 0 :
26
+ break
27
+ elif positive (dif ) != sign and sign is not None :
28
+ break
29
+
30
+ prev = pos
31
+ sign = positive (dif )
32
+ else :
33
+ safe += 1
34
+
35
+ return safe
36
+
37
+ if __name__ == '__main__' :
38
+ # reports = parse_input('test-input')
39
+ reports = parse_input ('input' )
40
+
41
+ # Part 1
42
+ safe = count_safe (reports )
43
+ print (f"{ safe } reports are safe" )
Original file line number Diff line number Diff line change
1
+ 7 6 4 2 1
2
+ 1 2 7 8 9
3
+ 9 7 6 2 1
4
+ 1 3 2 4 5
5
+ 8 6 4 4 1
6
+ 1 3 6 7 9
You can’t perform that action at this time.
0 commit comments