day 2 - part 2

This commit is contained in:
Stanislas Jouffroy 2024-12-02 22:12:10 +01:00
parent eac54767b5
commit 2b223c0360
5 changed files with 1100 additions and 0 deletions

1000
src/aoc_2024/day2/input-data Normal file

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,56 @@
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 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]]:
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
print(score)
return score
if __name__ == "__main__":
file = Path(__file__).parent / "input-data"
main(file)