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
518
619
720def is_safe (report ):
@@ -25,7 +38,7 @@ def is_gradually_chaging(report):
2538def main (part : int = 1 ) -> int :
2639 with open ("2024/data/day02.txt" ) as f :
2740 reports = f .read ()
28- return safe_count (reports )
41+ return safe_count (reports , part )
2942
3043
3144if __name__ == "__main__" :
@@ -39,3 +52,7 @@ def main(part: int = 1) -> int:
3952 assert safe_count (reports ) == 2
4053
4154 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