From 05174e324e96a780fc057771a67a642a388c704f Mon Sep 17 00:00:00 2001 From: Holly McFarland Date: Wed, 4 Dec 2024 18:23:53 -0500 Subject: [PATCH] day 2 --- day_02/part_a.py | 19 +++++++++++++++++++ day_02/part_b.py | 23 +++++++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 day_02/part_a.py create mode 100644 day_02/part_b.py diff --git a/day_02/part_a.py b/day_02/part_a.py new file mode 100644 index 0000000..1907b3c --- /dev/null +++ b/day_02/part_a.py @@ -0,0 +1,19 @@ +import aocd + +def parse_reports(data): + for line in data.split('\n'): + yield [int(level) for level in line.split()] + +def is_safe(report): + pairs = list(zip(report[:-1], report[1:])) + diffs = [a-b for a,b in pairs] + return all(diff in (1, 2, 3) for diff in diffs) or all(diff in (-1, -2, -3) for diff in diffs) + +def solve(data): + return sum(is_safe(report) for report in parse_reports(data)) + +if __name__ == '__main__': + solution = solve(aocd.get_data(year=2024, day=2)) + + print(solution) + aocd.submit(solution, year=2024, day=2, part='a') \ No newline at end of file diff --git a/day_02/part_b.py b/day_02/part_b.py new file mode 100644 index 0000000..50a96ec --- /dev/null +++ b/day_02/part_b.py @@ -0,0 +1,23 @@ +import aocd + +def parse_reports(data): + for line in data.split('\n'): + yield [int(level) for level in line.split()] + +def is_safe(orig_report): + modified = [orig_report[:i] + orig_report[i+1:] for i in range(len(orig_report))] + for report in [orig_report] + modified: + pairs = list(zip(report[:-1], report[1:])) + diffs = [a-b for a,b in pairs] + if all(diff in (1, 2, 3) for diff in diffs) or all(diff in (-1, -2, -3) for diff in diffs): + return True + return False + +def solve(data): + return sum(is_safe(report) for report in parse_reports(data)) + +if __name__ == '__main__': + solution = solve(aocd.get_data(year=2024, day=2)) + + print(solution) + aocd.submit(solution, year=2024, day=2, part='b') \ No newline at end of file