From 5deb351504bbdbbbd73d5ce4c72a577a212603fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anders=20Engl=C3=B6f=20Ytterstr=C3=B6m?= Date: Mon, 2 Dec 2024 06:46:41 +0100 Subject: [PATCH] Solve 2024:2 p1-2 "Red-Nosed Reports" DDOS of adventofcode.com, did not get to open the puzzle until 06:05 (five minutes local time). Screwed up the zip() and lost valuable minutes by trying to replace it with itertools, only to find I have made a typo (did AB CD EF instead of AB BC CD). I also lost valuable minutes by tring to solve p1 with one nested loop. Stupid. For part 2, I created the issafe() helper to be able to remove items from reports and test, one at the time. --- 2024-python/output/day_02.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 2024-python/output/day_02.py diff --git a/2024-python/output/day_02.py b/2024-python/output/day_02.py new file mode 100644 index 0000000..21a687c --- /dev/null +++ b/2024-python/output/day_02.py @@ -0,0 +1,34 @@ +from output import ints + + +def solve(data): + reports = [ints(line) for line in data.splitlines()] + p1 = 0 + p2 = 0 + + for report in reports: + if issafe(report): + p1 += 1 + + for i in range(len(report)): + if issafe(report[:i] + report[i + 1 :]): + p2 += 1 + break + + return p1, p2 + + +def issafe(report): + diffs = [a - b for a, b in zip(report, report[1:])] + + return all(-3 <= d <= -1 for d in diffs) or all(1 <= d <= 3 for d in diffs) + + +if __name__ == "__main__": + with open("./input/02.txt", "r") as f: + inp = f.read().strip() + + p1, p2 = solve(inp) + + print(p1) + print(p2)