solve day 2 part 2
This commit is contained in:
parent
8e4cf522d0
commit
89d34ce450
1 changed files with 44 additions and 18 deletions
|
|
@ -7,24 +7,46 @@ def get_lines(filename: str) -> list:
|
|||
return [line.strip() for line in file.readlines()]
|
||||
|
||||
|
||||
def part_one(lines: list) -> int:
|
||||
safe_reports = 0
|
||||
def get_report_entries(lines: list) -> list:
|
||||
report_entries = []
|
||||
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):
|
||||
entry = [int(x) for x in line.split(" ")]
|
||||
report_entries.append(entry)
|
||||
return report_entries
|
||||
|
||||
|
||||
def is_safe_single(reports: list) -> bool:
|
||||
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):
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
def is_safe_full(reports: list, dampener: bool) -> bool:
|
||||
if is_safe_single(reports):
|
||||
return True
|
||||
if dampener:
|
||||
for i in range(len(reports)):
|
||||
if is_safe_single(reports[:i] + reports[i + 1:]):
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
def get_safe_reports(report_entries: list, dampener: bool) -> int:
|
||||
safe_reports = 0
|
||||
for entry in report_entries:
|
||||
if is_safe_full(entry, dampener):
|
||||
safe_reports += 1
|
||||
return safe_reports
|
||||
|
||||
|
|
@ -32,10 +54,14 @@ def part_one(lines: list) -> int:
|
|||
def main():
|
||||
# lines = get_lines("sample-input.txt")
|
||||
lines = get_lines("input.txt")
|
||||
report_entries = get_report_entries(lines)
|
||||
|
||||
safe_reports = part_one(lines)
|
||||
safe_reports = get_safe_reports(report_entries, dampener=False)
|
||||
print(f"Part 1: There are {safe_reports} safe reports.")
|
||||
|
||||
safe_reports = get_safe_reports(report_entries, dampener=True)
|
||||
print(f"Part 2: There are {safe_reports} safe reports.")
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue