diff --git a/challenges/2024/01-historianHysteria/README.md b/challenges/2024/01-historianHysteria/README.md new file mode 100644 index 0000000..9860443 --- /dev/null +++ b/challenges/2024/01-historianHysteria/README.md @@ -0,0 +1 @@ +# [Day 1: Historian Hysteria](https://adventofcode.com/2024/day/1) diff --git a/challenges/2024/01-historianHysteria/main.py b/challenges/2024/01-historianHysteria/main.py new file mode 100644 index 0000000..b958160 --- /dev/null +++ b/challenges/2024/01-historianHysteria/main.py @@ -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)) \ No newline at end of file diff --git a/challenges/2024/01-historianHysteria/tests.json b/challenges/2024/01-historianHysteria/tests.json new file mode 100644 index 0000000..304ada4 --- /dev/null +++ b/challenges/2024/01-historianHysteria/tests.json @@ -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" + } + ] +} \ No newline at end of file diff --git a/challenges/2024/README.md b/challenges/2024/README.md index 91cdbff..e487fcc 100644 --- a/challenges/2024/README.md +++ b/challenges/2024/README.md @@ -14,4 +14,4 @@ A day denoted with a star means it has a visualisation. | Day | Status | Solutions | Notes | |-------------------------------------|--------|----------------------|-------| -| 01 - Name | ✗ ✗ | | Nothing here... yet! | \ No newline at end of file +| 01 - Historian Hysteria | ★ ★ | Python | The reading comprehension was the hardest part of this. | \ No newline at end of file