Day02
This commit is contained in:
45
2024/gareth/day02/poc.py
Normal file
45
2024/gareth/day02/poc.py
Normal file
@@ -0,0 +1,45 @@
|
||||
def is_safe(report):
|
||||
increasing = report[1] > report[0]
|
||||
decreasing = report[1] < report[0]
|
||||
|
||||
for i in range(1, len(report)):
|
||||
diff = report[i] - report[i - 1]
|
||||
abs_diff = abs(diff)
|
||||
if abs_diff < 1 or abs_diff > 3:
|
||||
return False
|
||||
if (diff > 0 and not increasing) or (diff < 0 and not decreasing):
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
def is_safe_with_dampener(report):
|
||||
if is_safe(report):
|
||||
return True
|
||||
for i in range(len(report)):
|
||||
modified_report = report[:i] + report[i+1:]
|
||||
if is_safe(modified_report):
|
||||
return True
|
||||
|
||||
return False
|
||||
|
||||
def read_reports_from_file(filename):
|
||||
reports = []
|
||||
with open(filename, 'r') as file:
|
||||
for line in file:
|
||||
report = list(map(int, line.split()))
|
||||
reports.append(report)
|
||||
return reports
|
||||
|
||||
def main():
|
||||
filename = 'input.txt'
|
||||
reports = read_reports_from_file(filename)
|
||||
|
||||
safe_count = 0
|
||||
for report in reports:
|
||||
if is_safe_with_dampener(report):
|
||||
safe_count += 1
|
||||
|
||||
print("OUTPUT:", safe_count)
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user