2024.01
This commit is contained in:
parent
507cd82fa4
commit
bf3083cc51
4 changed files with 72 additions and 1 deletions
1
challenges/2024/01-historianHysteria/README.md
Normal file
1
challenges/2024/01-historianHysteria/README.md
Normal file
|
@ -0,0 +1 @@
|
|||
# [Day 1: Historian Hysteria](https://adventofcode.com/2024/day/1)
|
56
challenges/2024/01-historianHysteria/main.py
Normal file
56
challenges/2024/01-historianHysteria/main.py
Normal 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))
|
14
challenges/2024/01-historianHysteria/tests.json
Normal file
14
challenges/2024/01-historianHysteria/tests.json
Normal 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"
|
||||
}
|
||||
]
|
||||
}
|
|
@ -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. |
|
Loading…
Add table
Add a link
Reference in a new issue