solve day 2 part 1

This commit is contained in:
Heiko Ludwig 2024-12-02 07:13:34 +01:00
parent 40bc8ca23e
commit 8e4cf522d0
4 changed files with 1060 additions and 0 deletions

41
day_02/program.py Executable file
View file

@ -0,0 +1,41 @@
#!/usr/bin/env python3
# https://adventofcode.com/2024/day/2
def get_lines(filename: str) -> list:
with open(filename, "r") as file:
return [line.strip() for line in file.readlines()]
def part_one(lines: list) -> int:
safe_reports = 0
for line in lines:
reports = [int(x) for x in line.split(" ")]
# print(reports)
increasing = True
decreasing = True
safe = True
for i in range(len(reports) - 1):
if reports[i] >= reports[i + 1]:
increasing = False
if reports[i] <= reports[i + 1]:
decreasing = False
distance = abs(reports[i] - reports[i + 1])
if distance < 1 or distance > 3:
safe = False
pass
if safe and (increasing or decreasing):
safe_reports += 1
return safe_reports
def main():
# lines = get_lines("sample-input.txt")
lines = get_lines("input.txt")
safe_reports = part_one(lines)
print(f"Part 1: There are {safe_reports} safe reports.")
if __name__ == '__main__':
main()