-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday15.py
More file actions
91 lines (64 loc) · 1.66 KB
/
day15.py
File metadata and controls
91 lines (64 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
from collections import defaultdict
from pathlib import Path
from loguru import logger
from typer import Typer
from utils import timer
main = Typer()
def hash(s: str) -> int:
"""Hash a string
Args:
s: Input string
Returns:
Hash value
"""
res = 0
for c in s:
res += ord(c)
res *= 17
res %= 256
return res
@timer
def task01(input: str) -> int:
"""Solution for task 01
Args:
input: Input string
Returns:
Sum of the hashes
"""
return sum(hash(entry) for entry in input.split(","))
def task02(input: str) -> int:
"""Solution for task 01
The method heavily exploits that python dicts are sorted
by insertion order.
Args:
input: Input string
Returns:
Sum of the focusing powers
"""
boxes = defaultdict(dict)
for entry in input.split(","):
if "-" in entry:
lens_name = entry[:-1]
focal = None
else:
lens_name, focal = entry.split("=")
box_id = hash(lens_name)
if focal is None:
if lens_name in boxes[box_id]:
del boxes[box_id][lens_name]
continue
boxes[box_id][lens_name] = int(focal)
res = 0
for box_id, box in boxes.items():
for lens_id, focal in enumerate(box.values()):
res += (1 + box_id) * (1 + lens_id) * focal
return res
@main.command()
def entrypoint(path: Path):
"""CLI entrypoint"""
with open(path) as f:
input = f.read().strip()
logger.info(f"Task 01: {task01(input)}")
logger.info(f"Task 02: {task02(input)}")
if __name__ == "__main__":
main()