blob: 51a569fce4af9732175328496b9a16245342c1a2 (
plain)
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
|
from collections import Counter
def look_and_say(n: str) -> str:
"""
for every consecutive chunk of characters, count the number of characters, concatenate the chunk length and the character
"""
res = []
char = n[0]
count = 1
for c in n[1:]:
if c == char:
count += 1
else:
res.append(str(count) + char)
char = c
count = 1
res.append(str(count) + char)
return ''.join(res)
def part1(n: str) -> str:
for i in range(40):
n = look_and_say(n)
return len(n)
def part2(n: str) -> str:
for i in range(50):
n = look_and_say(n)
return len(n)
if __name__ == '__main__':
inp = '1113222113'
print(part1(inp))
print(part2(inp))
|