-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path08.py
executable file
·70 lines (59 loc) · 1.4 KB
/
08.py
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
def calc(line):
i = 0
_code = 0
_string = 0
while i < len(line):
c = line[i]
if c == '"':
_code += 1
i += 1
elif c == '\\':
if line[i + 1] == '\\' or line[i + 1] == '"':
_code += 2
_string += 1
i += 2
elif line[i + 1] == 'x':
_code += 4
_string += 1
i += 4
else:
_code += 1
_string += 1
i += 1
#print(line, _code, _string)
return (_code, _string)
def encode(line):
e = []
for c in line:
if c == '"':
e.append('\\')
e.append('"')
elif c == '\\':
e.append('\\')
e.append('\\')
else:
e.append(c)
return '"' + ''.join(e) + '"'
#print(encode('"\\x27"'))
#exit()
with open('08.txt' ) as file:
code = 0
string = 0
for line in (l.strip() for l in file):
r = calc(line)
code += r[0]
string += r[1]
#print(code, string, line)
print('part1', code - string)
orig = code
with open('08.txt' ) as file:
code = 0
string = 0
for line in (l.strip() for l in file):
r = calc(encode(line))
code += r[0]
string += r[1]
#print('cs', code, string)
n = code
#print(n, orig)
print('part2', n - orig)