Added template (see 2022) + Day 2 completed
This commit is contained in:
20
day2/common.py
Normal file
20
day2/common.py
Normal file
@@ -0,0 +1,20 @@
|
||||
type Report = tuple[int, ...]
|
||||
type ReportList = list[Report]
|
||||
|
||||
|
||||
def parse(filename: str) -> ReportList:
|
||||
report_list: ReportList = []
|
||||
with open(filename) as f:
|
||||
while line := f.readline().strip("\n"):
|
||||
report: Report = tuple(int(digit) for digit in line.split())
|
||||
report_list.append(report)
|
||||
return report_list
|
||||
|
||||
|
||||
def is_safe(report: Report) -> bool:
|
||||
if not (report == tuple(sorted(report)) or report == tuple(sorted(report, reverse=True))):
|
||||
return False
|
||||
for i, value in enumerate(report[:-1]):
|
||||
if abs(value - report[i + 1]) > 3 or value == report[i + 1]:
|
||||
return False
|
||||
return True
|
||||
14
day2/part1.py
Executable file
14
day2/part1.py
Executable file
@@ -0,0 +1,14 @@
|
||||
#!/usr/bin/env python
|
||||
from common import ReportList, is_safe, parse
|
||||
|
||||
|
||||
def solve(input: ReportList) -> int:
|
||||
return len(list(filter(is_safe, input)))
|
||||
|
||||
|
||||
def main():
|
||||
print(solve(parse("input")))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
22
day2/part2.py
Executable file
22
day2/part2.py
Executable file
@@ -0,0 +1,22 @@
|
||||
#!/usr/bin/env python
|
||||
from common import Report, ReportList, is_safe, parse
|
||||
|
||||
|
||||
def report_remove(report: Report, index: int) -> Report:
|
||||
return report[:index] + report[index + 1 :]
|
||||
|
||||
|
||||
def is_safe_dampened(report: Report) -> bool:
|
||||
return is_safe(report) or any(map(is_safe, map(lambda x: report_remove(report, x[0]), enumerate(report))))
|
||||
|
||||
|
||||
def solve(input: ReportList) -> int:
|
||||
return len(list(filter(is_safe_dampened, input)))
|
||||
|
||||
|
||||
def main():
|
||||
print(solve(parse("input")))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user