File tree Expand file tree Collapse file tree 1 file changed +22
-5
lines changed Expand file tree Collapse file tree 1 file changed +22
-5
lines changed Original file line number Diff line number Diff line change 1
- def safe_count (reports ):
2
- return sum (
3
- is_safe (list (map (int , report .split ()))) for report in reports .splitlines ()
4
- )
1
+ def safe_count (reports , part = 1 ):
2
+ if part == 1 :
3
+ return sum (
4
+ is_safe (list (map (int , report .split ()))) for report in reports .splitlines ()
5
+ )
6
+ else :
7
+ safe_lines = 0
8
+ for report in reports .splitlines ():
9
+ report = list (map (int , report .split ()))
10
+ if is_safe (report ):
11
+ safe_lines += 1
12
+ else :
13
+ # brute force
14
+ safe_lines += any (
15
+ is_safe (report [:i ] + report [i + 1 :]) for i in range (len (report ))
16
+ )
17
+ return safe_lines
5
18
6
19
7
20
def is_safe (report ):
@@ -25,7 +38,7 @@ def is_gradually_chaging(report):
25
38
def main (part : int = 1 ) -> int :
26
39
with open ("2024/data/day02.txt" ) as f :
27
40
reports = f .read ()
28
- return safe_count (reports )
41
+ return safe_count (reports , part )
29
42
30
43
31
44
if __name__ == "__main__" :
@@ -39,3 +52,7 @@ def main(part: int = 1) -> int:
39
52
assert safe_count (reports ) == 2
40
53
41
54
print (main ())
55
+
56
+ assert safe_count (reports , part = 2 ) == 4
57
+
58
+ print (main (part = 2 ))
You can’t perform that action at this time.
0 commit comments