This commit is contained in:
akp 2024-12-01 11:41:20 +00:00
parent 507cd82fa4
commit bf3083cc51
No known key found for this signature in database
GPG key ID: CF8D58F3DEB20755
4 changed files with 72 additions and 1 deletions

View file

@ -0,0 +1 @@
# [Day 1: Historian Hysteria](https://adventofcode.com/2024/day/1)

View file

@ -0,0 +1,56 @@
import sys
from collections import defaultdict
def parse(instr: str) -> tuple[list[int], list[int]]:
a, b = [], []
for line in instr.splitlines():
ai, bi = line.split(" ")
a.append(int(ai))
b.append(int(bi))
return a, b
def one(instr: str) -> int:
a, b = parse(instr)
a = sorted(a)
b = sorted(b)
acc = 0
for (x, y) in zip(a, b):
acc += abs(y - x)
return acc
def two(instr: str):
a, b = parse(instr)
counts = defaultdict(lambda: 0)
for val in b:
counts[val] = counts[val] + 1
acc = 0
for val in a:
acc += counts[val] * val
return acc
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": "11",
"input": "3 4\n4 3\n2 5\n1 3\n3 9\n3 3\n\n"
}
],
"2": [
{
"is": "31",
"input": "3 4\n4 3\n2 5\n1 3\n3 9\n3 3\n\n"
}
]
}

View file

@ -14,4 +14,4 @@ A day denoted with a star means it has a visualisation.
| Day | Status | Solutions | Notes |
|-------------------------------------|--------|----------------------|-------|
| 01 - Name | ✗ ✗ | | Nothing here... yet! |
| 01 - Historian Hysteria | ★ ★ | Python | The reading comprehension was the hardest part of this. |