Skip to content

Commit e960e8e

Browse files
committed
setup 2023
1 parent b9c2ae1 commit e960e8e

File tree

6 files changed

+125
-0
lines changed

6 files changed

+125
-0
lines changed

.python-version

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
3.11.4

2023/start.sh

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
NUMBER=$(printf "%02d" $1)
2+
NAME="$2"
3+
FILENAME="${NUMBER}/$NAME"
4+
SESSION_COOKIE=$(cat .sessioncookie)
5+
6+
if [ ! -d $NUMBER ]
7+
then
8+
mkdir $NUMBER
9+
10+
cp template.py $FILENAME.py
11+
sed -i '' -e 's|DAY|'"$NUMBER"'|g' $FILENAME.py
12+
13+
curl 'https://adventofcode.com/2022/day/'$1'/input' -H 'Cookie: session='$SESSION_COOKIE > $NUMBER/input.txt
14+
else
15+
echo "Directory $NUMBER already exists"
16+
fi

2023/stats.py

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import sys
2+
from datetime import datetime
3+
4+
import requests
5+
6+
7+
with open('.sessioncookie', 'r') as f:
8+
session_token = f.read().strip()
9+
10+
11+
member_id = "1194494"
12+
url = "https://adventofcode.com/2023/leaderboard/private/view/212116.json"
13+
headers = {"Cookie": "session=" + session_token}
14+
15+
data = None
16+
if (r := requests.get(url, headers=headers)).status_code == 200:
17+
data = r.json()
18+
else:
19+
sys.exit(f"Response {r.status_code}: {r.reason} \n{r.content}")
20+
21+
me = data["members"][member_id]
22+
23+
print("My Stats:")
24+
for key in sorted(me["completion_day_level"]):
25+
print(f"\tDay {key}:")
26+
for star in sorted(me["completion_day_level"][key]):
27+
star_time = datetime.fromtimestamp(
28+
me["completion_day_level"][key][star]["get_star_ts"]
29+
)
30+
print(f"\t\tStar {star} Time: {star_time}")

2023/template.py

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from utils import read_input, run
2+
3+
4+
FNAME = "DAY/input.txt"
5+
6+
7+
##########
8+
# PART 1 #
9+
##########
10+
11+
12+
def part_one(input_file):
13+
data = read_input(input_file)
14+
print(data)
15+
16+
17+
##########
18+
# PART 2 #
19+
##########
20+
21+
22+
def part_two(input_file):
23+
pass
24+
25+
26+
if __name__ == '__main__':
27+
run(part_one, part_two, FNAME)

2023/utils.py

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
from timeit import default_timer as timer
2+
from itertools import tee
3+
from typing import Callable, TypeVar, Any
4+
5+
6+
T = TypeVar('T')
7+
8+
9+
def default_parse_line(line: str) -> list[str]:
10+
return line.strip().split()
11+
12+
13+
def read_input(fname: str, separator: str = '\n', parse_chunk: Callable[[str], T] = default_parse_line) -> list[T]:
14+
with open(fname) as f:
15+
return [parse_chunk(line) for line in f.read().rstrip().split(separator)]
16+
17+
18+
def timed(f, *args, **kwargs):
19+
t1 = timer()
20+
result = f(*args, **kwargs)
21+
t2 = timer()
22+
return result, t2 - t1
23+
24+
25+
def sliding_window(iterable, size):
26+
iterables = tee(iterable, size)
27+
for i, iterator in enumerate(iterables):
28+
for _ in range(i):
29+
next(iterator)
30+
return zip(*iterables)
31+
32+
33+
def run(part_one, part_two, input_file):
34+
print("PART 1")
35+
result, time = timed(part_one, input_file)
36+
print(f"Answer:\t{result}\nTime:\t{time*1000}ms")
37+
print()
38+
print(f"PART 2")
39+
result, time = timed(part_two, input_file)
40+
print(f"Answer:\t{result}\nTime:\t{time*1000}ms")
41+
42+
43+
def run_test(part, test_input_file, expected, test_name: str = "", exit_on_fail: bool = True):
44+
result, time = timed(part, test_input_file)
45+
if result == expected:
46+
print(f"Passed: {test_name} in {time*1000}ms")
47+
else:
48+
print(f"Failed: {test_name or test_input_file}\nExpected {expected}, got {result} in {time*1000}ms")
49+
if exit_on_fail:
50+
exit(1)

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ My solutions to [Advent of Code](https://adventofcode.com/).
44

55
- [2021](./2021/)
66
- [2022](./2022/)
7+
- [2023](./2023/)

0 commit comments

Comments
 (0)