diff --git a/.gitignore b/.gitignore index a40797e..950eb09 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,5 @@ # Created by venv; see https://docs.python.org/3/library/venv.html cookies.txt -**/input +**/*input __pycache__ .venv diff --git a/day2/common.py b/day2/common.py new file mode 100644 index 0000000..e194428 --- /dev/null +++ b/day2/common.py @@ -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 diff --git a/day2/part1.py b/day2/part1.py new file mode 100755 index 0000000..39ee740 --- /dev/null +++ b/day2/part1.py @@ -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() diff --git a/day2/part2.py b/day2/part2.py new file mode 100755 index 0000000..edd2d29 --- /dev/null +++ b/day2/part2.py @@ -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() diff --git a/get_input.sh b/get_input.sh index d417e06..4ca569d 100755 --- a/get_input.sh +++ b/get_input.sh @@ -1,4 +1,5 @@ #!/bin/bash day=$1 mkdir day${day} +cp template/* day${day} curl -o day${day}/input --cookie ~/AdventOfCode/2024/cookies.txt https://adventofcode.com/2024/day/$day/input diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..4473938 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +ruff +mypy diff --git a/template/common.py b/template/common.py new file mode 100644 index 0000000..18487f9 --- /dev/null +++ b/template/common.py @@ -0,0 +1,3 @@ +def parse(filename: str): + with open(filename) as f: + return [line[:-1] for line in f.readlines()] diff --git a/template/part1.py b/template/part1.py new file mode 100755 index 0000000..6692927 --- /dev/null +++ b/template/part1.py @@ -0,0 +1,14 @@ +#!/usr/bin/env python +from common import parse + + +def solve(input): + pass + + +def main(): + print(solve(parse("input"))) + + +if __name__ == "__main__": + main()