-
Notifications
You must be signed in to change notification settings - Fork 0
/
part_b.py
executable file
·77 lines (65 loc) · 2.08 KB
/
part_b.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
71
72
73
74
75
76
77
#!/usr/bin/env python3
import re
from dataclasses import dataclass
from aox.challenge import Debugger
from utils import BaseChallenge
from . import part_a
class Challenge(BaseChallenge):
def solve(self, _input, debugger: Debugger):
"""
>>> Challenge().default_solve()
55
"""
return StringSetExtended.from_strings_text(_input)\
.get_nice_string_count()
class StringSetExtended(part_a.StringSet['StringExtended']):
pass
@dataclass
class StringExtended(part_a.String):
re_two_letters_repeated = re.compile(r"(\w\w).*\1")
re_sandwiched_letter = re.compile(r"(\w)\w\1")
def is_nice(self) -> bool:
"""
>>> StringExtended("xyxy").is_nice()
True
>>> StringExtended("aabcdefgaa").is_nice()
False
>>> StringExtended("aaa").is_nice()
False
>>> StringExtended("qjhvhtzxzqqjkmpb").is_nice()
True
>>> StringExtended("xxyxx").is_nice()
True
>>> StringExtended("uurcxstgmygtbstg").is_nice()
False
>>> StringExtended("ieodomkazucvgmuy").is_nice()
False
"""
return bool(
self.has_two_letters_repeated()
and self.has_sandwiched_letter()
)
def has_two_letters_repeated(self) -> bool:
"""
>>> StringExtended("aabcdefgaa").has_two_letters_repeated()
True
>>> StringExtended("xyxy").has_two_letters_repeated()
True
>>> StringExtended("aabcdefgaa").has_two_letters_repeated()
True
>>> StringExtended("aaa").has_two_letters_repeated()
False
"""
return bool(self.re_two_letters_repeated.findall(self.string))
def has_sandwiched_letter(self) -> bool:
"""
>>> StringExtended("xyx").has_sandwiched_letter()
True
>>> StringExtended("abcdefeghi").has_sandwiched_letter()
True
>>> StringExtended("aaa").has_sandwiched_letter()
True
"""
return bool(self.re_sandwiched_letter.findall(self.string))
Challenge.main()
challenge = Challenge()