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