Skip to content

Commit bf3083c

Browse files
committed
2024.01
1 parent 507cd82 commit bf3083c

File tree

4 files changed

+72
-1
lines changed

4 files changed

+72
-1
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# [Day 1: Historian Hysteria](https://adventofcode.com/2024/day/1)
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import sys
2+
from collections import defaultdict
3+
4+
5+
def parse(instr: str) -> tuple[list[int], list[int]]:
6+
a, b = [], []
7+
8+
for line in instr.splitlines():
9+
ai, bi = line.split(" ")
10+
a.append(int(ai))
11+
b.append(int(bi))
12+
13+
return a, b
14+
15+
16+
def one(instr: str) -> int:
17+
a, b = parse(instr)
18+
19+
a = sorted(a)
20+
b = sorted(b)
21+
22+
acc = 0
23+
for (x, y) in zip(a, b):
24+
acc += abs(y - x)
25+
26+
return acc
27+
28+
29+
def two(instr: str):
30+
a, b = parse(instr)
31+
32+
counts = defaultdict(lambda: 0)
33+
for val in b:
34+
counts[val] = counts[val] + 1
35+
36+
acc = 0
37+
for val in a:
38+
acc += counts[val] * val
39+
40+
return acc
41+
42+
43+
def _debug(*args, **kwargs):
44+
kwargs["file"] = sys.stderr
45+
print(*args, **kwargs)
46+
47+
48+
if __name__ == "__main__":
49+
if len(sys.argv) < 2 or sys.argv[1] not in ["1", "2"]:
50+
print("Missing day argument", file=sys.stderr)
51+
sys.exit(1)
52+
inp = sys.stdin.read().strip()
53+
if sys.argv[1] == "1":
54+
print(one(inp))
55+
else:
56+
print(two(inp))
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"1": [
3+
{
4+
"is": "11",
5+
"input": "3 4\n4 3\n2 5\n1 3\n3 9\n3 3\n\n"
6+
}
7+
],
8+
"2": [
9+
{
10+
"is": "31",
11+
"input": "3 4\n4 3\n2 5\n1 3\n3 9\n3 3\n\n"
12+
}
13+
]
14+
}

challenges/2024/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@ A day denoted with a star means it has a visualisation.
1414

1515
| Day | Status | Solutions | Notes |
1616
|-------------------------------------|--------|----------------------|-------|
17-
| 01 - Name | ✗ ✗ | | Nothing here... yet! |
17+
| 01 - Historian Hysteria | ★ ★ | Python | The reading comprehension was the hardest part of this. |

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy