Added day 2 solution
This commit is contained in:
commit
35a8a47a84
3 changed files with 1057 additions and 0 deletions
1001
day2/input.txt
Normal file
1001
day2/input.txt
Normal file
File diff suppressed because it is too large
Load diff
23
day2/solution.py
Normal file
23
day2/solution.py
Normal file
|
@ -0,0 +1,23 @@
|
|||
def is_safe(report):
|
||||
levels = list(map(int, report.split()))
|
||||
differences = [levels[i+1] - levels[i] for i in range(len(levels) - 1)]
|
||||
|
||||
if all(-3 <= diff <= -1 for diff in differences) or all(1 <= diff <= 3 for diff in differences):
|
||||
return True
|
||||
return False
|
||||
|
||||
def count_safe_reports(data):
|
||||
reports = data.strip().split('\n')
|
||||
safe_count = sum(is_safe(report) for report in reports)
|
||||
return safe_count
|
||||
|
||||
data = """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"""
|
||||
data = open('input.txt', 'r').read()
|
||||
safe_reports = count_safe_reports(data)
|
||||
print(f"Number of safe reports: {safe_reports}")
|
||||
|
33
day2/solution2.py
Normal file
33
day2/solution2.py
Normal file
|
@ -0,0 +1,33 @@
|
|||
def is_safe(report):
|
||||
levels = list(map(int, report.split()))
|
||||
differences = [levels[i+1] - levels[i] for i in range(len(levels) - 1)]
|
||||
|
||||
if all(-3 <= diff <= -1 for diff in differences) or all(1 <= diff <= 3 for diff in differences):
|
||||
return True
|
||||
return False
|
||||
|
||||
def with_dampener_safe(report):
|
||||
levels = report.split()
|
||||
if is_safe(report):
|
||||
return True
|
||||
for i in range(len(levels)):
|
||||
modified_report = ' '.join(levels[:i] + levels[i+1:])
|
||||
if is_safe(modified_report):
|
||||
return True
|
||||
return False
|
||||
|
||||
def count_safe_reports_with_dampener(data):
|
||||
reports = data.strip().split('\n')
|
||||
safe_count = sum(with_dampener_safe(report) for report in reports)
|
||||
return safe_count
|
||||
|
||||
data = """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"""
|
||||
data = open('input.txt', 'r').read()
|
||||
safe_reports_with_dampener = count_safe_reports_with_dampener(data)
|
||||
print(f"Number of safe reports with the Problem Dampener: {safe_reports_with_dampener}")
|
||||
|
Loading…
Reference in a new issue