-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.py
More file actions
42 lines (32 loc) · 994 Bytes
/
solution.py
File metadata and controls
42 lines (32 loc) · 994 Bytes
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
def process_line_slow(s: str) -> int:
s = s[1:-1]
i = 0
memory_chars = 0
n = len(s)
while i < n:
if s[i] == "\\":
next_char = s[i+1]
if next_char in ["\\", '"']:
memory_chars += 1
i += 2
elif next_char == "x":
memory_chars += 1
i += 4
else:
memory_chars += 1
i += 1
return len(s) + 2 - memory_chars
def process_line_fast(s: str) -> int:
code_len = len(s)
in_memory = len(bytes(s[1:-1], "utf-8").decode("unicode_escape"))
return code_len - in_memory
def solve_part1_slow(input: str) -> int:
lines = input.splitlines()
return sum(map(process_line_slow, lines))
def solve_part1_fast(input: str) -> int:
lines = input.splitlines()
return sum(map(process_line_fast, lines))
def solve_part2_slow(input: str) -> int:
return 0
def solve_part2_fast(input: str) -> int:
return 0