diff --git a/challenges/2024/02-redNosedReports/README.md b/challenges/2024/02-redNosedReports/README.md new file mode 100644 index 0000000..af62b55 --- /dev/null +++ b/challenges/2024/02-redNosedReports/README.md @@ -0,0 +1 @@ +# [Day 2: Red Nosed Reports](https://adventofcode.com/2024/day/2) diff --git a/challenges/2024/02-redNosedReports/main.py b/challenges/2024/02-redNosedReports/main.py new file mode 100644 index 0000000..88ef61a --- /dev/null +++ b/challenges/2024/02-redNosedReports/main.py @@ -0,0 +1,87 @@ +import sys +from typing import Optional + + +def parse(instr: str) -> list[list[int]]: + res = [] + for line in instr.splitlines(): + res.append(list(map(int, line.split(" ")))) + return res + + +def test_pair( + a: int, b: int, sequence_is_negative: Optional[bool] +) -> tuple[bool, bool]: + diff = b - a + this_is_negative = diff < 0 + + if sequence_is_negative is not None and this_is_negative != sequence_is_negative: + return False, this_is_negative + + return 1 <= abs(diff) <= 3, this_is_negative + + +def test_report(rep: list[int]) -> bool: + should_be_negative: Optional[bool] = None + ok = False + + for i, v in enumerate(rep[:-1]): + w = rep[i + 1] + + ok, neg = test_pair(v, w, should_be_negative) + if should_be_negative is None: + should_be_negative = neg + + if not ok: + break + + return ok + + +def one(instr: str): + reports = parse(instr) + + n = 0 + for rep in reports: + if test_report(rep): + n += 1 + + return n + + +def two(instr: str): + reports = parse(instr) + + n = 0 + for rep in reports: + if test_report(rep): + n += 1 + else: + ok = False + for i in range(len(rep)): + r = rep.copy() + r.pop(i) + if test_report(r): + ok = True + break + + if ok: + n += 1 + + return n + + +def _debug(*args, **kwargs): + kwargs["file"] = sys.stderr + print(*args, **kwargs) + + +if __name__ == "__main__": + if len(sys.argv) < 2 or sys.argv[1] not in ["1", "2"]: + print("Missing day argument", file=sys.stderr) + sys.exit(1) + inp = sys.stdin.read().strip() + if sys.argv[1] == "1": + print(one(inp)) + else: + print(two(inp)) diff --git a/challenges/2024/02-redNosedReports/tests.json b/challenges/2024/02-redNosedReports/tests.json new file mode 100644 index 0000000..54d412d --- /dev/null +++ b/challenges/2024/02-redNosedReports/tests.json @@ -0,0 +1,14 @@ +{ + "1": [ + { + "is": "2", + "input": "7 6 4 2 1\n1 2 7 8 9\n9 7 6 2 1\n1 3 2 4 5\n8 6 4 4 1\n1 3 6 7 9\n" + } + ], + "2": [ + { + "is": "4", + "input": "7 6 4 2 1\n1 2 7 8 9\n9 7 6 2 1\n1 3 2 4 5\n8 6 4 4 1\n1 3 6 7 9\n" + } + ] +} \ No newline at end of file diff --git a/challenges/2024/README.md b/challenges/2024/README.md index e487fcc..303c06e 100644 --- a/challenges/2024/README.md +++ b/challenges/2024/README.md @@ -14,4 +14,5 @@ A day denoted with a star means it has a visualisation. | Day | Status | Solutions | Notes | |-------------------------------------|--------|----------------------|-------| -| 01 - Historian Hysteria | ★ ★ | Python | The reading comprehension was the hardest part of this. | \ No newline at end of file +| 01 - Historian Hysteria | ★ ★ | Python | The reading comprehension was the hardest part of this. | +| 02 - Red-Nosed Reindeer | ★ ★ | Python || diff --git a/challenges/2024/benchmark-graph.png b/challenges/2024/benchmark-graph.png index cea0877..b7d39c4 100644 Binary files a/challenges/2024/benchmark-graph.png and b/challenges/2024/benchmark-graph.png differ diff --git a/challenges/2024/benchmarks.jsonl b/challenges/2024/benchmarks.jsonl index 44fbce0..9a3f7af 100644 --- a/challenges/2024/benchmarks.jsonl +++ b/challenges/2024/benchmarks.jsonl @@ -1,2 +1,4 @@ {"day": 1, "part": 1, "runner": "py", "min": 0.016046524047851562, "max": 0.025307178497314453, "avg": 0.016975646018981935, "n": 100} {"day": 1, "part": 2, "runner": "py", "min": 0.0161440372467041, "max": 0.019031047821044922, "avg": 0.016828114986419677, "n": 100} +{"day": 2, "part": 1, "runner": "py", "min": 0.021907567977905273, "max": 0.02720332145690918, "avg": 0.023289167881011964, "n": 100} +{"day": 2, "part": 2, "runner": "py", "min": 0.02489948272705078, "max": 0.0319674015045166, "avg": 0.026371052265167238, "n": 100}