This commit is contained in:
akp 2024-12-03 09:37:30 +00:00
parent ee3a3f9233
commit 7166572ccc
No known key found for this signature in database
GPG key ID: CF8D58F3DEB20755
6 changed files with 106 additions and 1 deletions

View file

@ -0,0 +1 @@
# [Day 2: Red Nosed Reports](https://adventofcode.com/2024/day/2)

View file

@ -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))

View file

@ -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"
}
]
}

View file

@ -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. |
| 01 - Historian Hysteria | ★ ★ | Python | The reading comprehension was the hardest part of this. |
| 02 - Red-Nosed Reindeer | ★ ★ | Python ||

Binary file not shown.

Before

Width:  |  Height:  |  Size: 24 KiB

After

Width:  |  Height:  |  Size: 26 KiB

Before After
Before After

View file

@ -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}