Skip to content

Commit 9e9135b

Browse files
committed
2024 day 2 part 2
1 parent 1c9a07c commit 9e9135b

File tree

1 file changed

+22
-5
lines changed

1 file changed

+22
-5
lines changed

2024/day02.py

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,20 @@
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

720
def is_safe(report):
@@ -25,7 +38,7 @@ def is_gradually_chaging(report):
2538
def 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

3144
if __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))

0 commit comments

Comments
 (0)