Compare commits
2 commits
2b223c0360
...
6e40648ec6
Author | SHA1 | Date | |
---|---|---|---|
6e40648ec6 | |||
c3796ec338 |
3 changed files with 129 additions and 8 deletions
|
@ -25,14 +25,6 @@ def is_report_safe(report: list[int]) -> bool:
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
def get_records(input_file: Path) -> list[list[int]]:
|
|
||||||
records = []
|
|
||||||
for line in input_file.open():
|
|
||||||
record = [int(i) for i in line.strip().split(" ")]
|
|
||||||
records.append(record)
|
|
||||||
return records
|
|
||||||
|
|
||||||
|
|
||||||
def get_reports(input_file: Path) -> list[list[int]]:
|
def get_reports(input_file: Path) -> list[list[int]]:
|
||||||
reports = []
|
reports = []
|
||||||
for line in input_file.open():
|
for line in input_file.open():
|
||||||
|
|
61
src/aoc_2024/day2/part2.py
Normal file
61
src/aoc_2024/day2/part2.py
Normal file
|
@ -0,0 +1,61 @@
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
|
||||||
|
def is_report_safe(report: list[int]) -> bool:
|
||||||
|
try:
|
||||||
|
is_increasing = True
|
||||||
|
is_decreasing = True
|
||||||
|
for i in range(len(report)):
|
||||||
|
shift = report[i + 1] - report[i]
|
||||||
|
if shift == 0:
|
||||||
|
is_increasing = False
|
||||||
|
is_decreasing = False
|
||||||
|
if shift > 0:
|
||||||
|
is_decreasing = False
|
||||||
|
if shift < 0:
|
||||||
|
is_increasing = False
|
||||||
|
|
||||||
|
if not is_decreasing and not is_increasing:
|
||||||
|
return False
|
||||||
|
|
||||||
|
if abs(shift) > 3:
|
||||||
|
return False
|
||||||
|
except IndexError:
|
||||||
|
pass
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
def is_report_safe_with_tolerance(report: list[int]) -> bool:
|
||||||
|
for i in range(len(report)):
|
||||||
|
tmp_report = list(report)
|
||||||
|
tmp_report.pop(i)
|
||||||
|
is_safe = is_report_safe(tmp_report)
|
||||||
|
if is_safe:
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
def get_reports(input_file: Path) -> list[list[int]]:
|
||||||
|
reports = []
|
||||||
|
for line in input_file.open():
|
||||||
|
report = [int(r) for r in line.strip().split(" ")]
|
||||||
|
reports.append(report)
|
||||||
|
return reports
|
||||||
|
|
||||||
|
|
||||||
|
def main(data_file: Path):
|
||||||
|
reports = get_reports(data_file)
|
||||||
|
score = 0
|
||||||
|
for report in reports:
|
||||||
|
if is_report_safe(report):
|
||||||
|
score += 1
|
||||||
|
else:
|
||||||
|
if is_report_safe_with_tolerance(report):
|
||||||
|
score += 1
|
||||||
|
print(score)
|
||||||
|
return score
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
file = Path(__file__).parent / "input-data"
|
||||||
|
main(file)
|
68
tests/aoc_2024/day2/test-part2.py
Normal file
68
tests/aoc_2024/day2/test-part2.py
Normal file
|
@ -0,0 +1,68 @@
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
from aoc_2024.day2 import part2
|
||||||
|
|
||||||
|
|
||||||
|
def test_report_with_tolerance_with_decreasing_levels_less_than_3_is_safe():
|
||||||
|
report = [7, 6, 4, 2, 1]
|
||||||
|
assert part2.is_report_safe_with_tolerance(report)
|
||||||
|
|
||||||
|
|
||||||
|
def test_report_with_tolerance_with_decreasing_levels_more_than_3_is_not_safe():
|
||||||
|
report = [9, 7, 6, 2, 1]
|
||||||
|
assert not part2.is_report_safe_with_tolerance(report)
|
||||||
|
|
||||||
|
|
||||||
|
def test_report_with_tolerance_without_increasing_or_decreasing_levels_is_not_safe():
|
||||||
|
report = [8, 6, 4, 4, 1]
|
||||||
|
assert part2.is_report_safe_with_tolerance(report)
|
||||||
|
|
||||||
|
|
||||||
|
def test_report_with_tolerance_with_increasing_and_decreasing_levels_is_not_safe():
|
||||||
|
report = [1, 3, 2, 4, 5]
|
||||||
|
assert part2.is_report_safe_with_tolerance(report)
|
||||||
|
|
||||||
|
|
||||||
|
def test_report_with_tolerance_with_increasing_levels_less_than_3_is_safe():
|
||||||
|
report = [1, 3, 6, 7, 9]
|
||||||
|
assert part2.is_report_safe_with_tolerance(report)
|
||||||
|
|
||||||
|
|
||||||
|
def test_report_with_tolerance_with_increasing_levels_more_than_3_is_not_safe():
|
||||||
|
report = [1, 2, 7, 8, 9]
|
||||||
|
assert not part2.is_report_safe_with_tolerance(report)
|
||||||
|
|
||||||
|
|
||||||
|
def test_report_with_decreasing_levels_less_than_3_is_safe():
|
||||||
|
report = [7, 6, 4, 2, 1]
|
||||||
|
assert part2.is_report_safe(report)
|
||||||
|
|
||||||
|
|
||||||
|
def test_report_with_decreasing_levels_more_than_3_is_not_safe():
|
||||||
|
report = [9, 7, 6, 2, 1]
|
||||||
|
assert not part2.is_report_safe(report)
|
||||||
|
|
||||||
|
|
||||||
|
def test_report_without_increasing_or_decreasing_levels_is_not_safe():
|
||||||
|
report = [8, 6, 4, 4, 1]
|
||||||
|
assert not part2.is_report_safe(report)
|
||||||
|
|
||||||
|
|
||||||
|
def test_report_with_increasing_and_decreasing_levels_is_not_safe():
|
||||||
|
report = [1, 3, 2, 4, 5]
|
||||||
|
assert not part2.is_report_safe(report)
|
||||||
|
|
||||||
|
|
||||||
|
def test_report_with_increasing_levels_less_than_3_is_safe():
|
||||||
|
report = [1, 3, 6, 7, 9]
|
||||||
|
assert part2.is_report_safe(report)
|
||||||
|
|
||||||
|
|
||||||
|
def test_report_with_increasing_levels_more_than_3_is_not_safe():
|
||||||
|
report = [1, 2, 7, 8, 9]
|
||||||
|
assert not part2.is_report_safe(report)
|
||||||
|
|
||||||
|
|
||||||
|
def test_read_records_file():
|
||||||
|
test_file = Path(__file__).parent / "test-data"
|
||||||
|
assert part2.main(test_file) == 4
|
Loading…
Add table
Add a link
Reference in a new issue