mirror of
https://github.com/thib8956/advent-of-code.git
synced 2025-08-24 08:21:57 +00:00
import 2024 aoc
This commit is contained in:
36
2024/day2/day2.py
Normal file
36
2024/day2/day2.py
Normal file
@@ -0,0 +1,36 @@
|
||||
from itertools import pairwise
|
||||
|
||||
|
||||
def is_safe(report):
|
||||
diffs = [b - a for a, b in pairwise(report)]
|
||||
return all(1 <= d <= 3 for d in diffs) or all(-3 <= d <= -1 for d in diffs)
|
||||
|
||||
|
||||
def is_safe_with_problem_damper(report):
|
||||
for i, _ in enumerate(report):
|
||||
copy = report[::]
|
||||
del copy[i]
|
||||
if is_safe(copy):
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
def main(reports):
|
||||
safe_reports = sum(1 for x in reports if is_safe(x))
|
||||
print("Part 1: ", safe_reports)
|
||||
|
||||
safe_reports = 0
|
||||
for report in reports:
|
||||
if is_safe_with_problem_damper(report):
|
||||
safe_reports += 1
|
||||
print("Part 2: ", safe_reports)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
import sys
|
||||
infile = sys.argv[1]
|
||||
with open(infile) as f:
|
||||
lines = f.readlines()
|
||||
lines = [list(map(int, x.rstrip().split())) for x in lines]
|
||||
main(lines)
|
||||
|
6
2024/day2/example.txt
Normal file
6
2024/day2/example.txt
Normal file
@@ -0,0 +1,6 @@
|
||||
7 6 4 2 1
|
||||
1 2 7 8 9
|
||||
9 7 6 2 1
|
||||
1 3 2 4 5
|
||||
8 6 4 4 1
|
||||
1 3 6 7 9
|
1000
2024/day2/input.txt
Normal file
1000
2024/day2/input.txt
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user