-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodyssi12.py
More file actions
84 lines (66 loc) · 2.15 KB
/
Copy pathcodyssi12.py
File metadata and controls
84 lines (66 loc) · 2.15 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
import sys
from enum import Enum
from enum import auto
from enum import unique
from codyssi.common import InputData
from codyssi.common import SolutionBase
from codyssi.common import codyssi_samples
TEST = """\
tv8cmj0i2951190z5w44fe205k542l5818ds05ib425h9lj260ud38-l6a06
a586m0eeuqqvt5-k-8434hb27ytha3i75-lw23-0cj856l7zn8234a05eron
"""
Output1 = int
Output2 = int
Output3 = int
@unique
class Reduction(Enum):
MODE_1 = auto()
MODE_2 = auto()
def test(self, ch: str) -> bool:
match self:
case Reduction.MODE_1:
return ch.isalpha() or ch == "-"
case Reduction.MODE_2:
return ch.isalpha()
def reduce(self, line: str) -> list[str]:
ans = list(line)
while True:
remove = set[int]()
for n in [i for i, ch in enumerate(ans) if ch.isnumeric()]:
if (
n > 0
and len(remove & {n, n - 1}) == 0
and self.test(ans[n - 1])
):
remove |= {n, n - 1}
if (
n < len(ans) - 1
and len(remove & {n, n + 1}) == 0
and self.test(ans[n + 1])
):
remove |= {n, n + 1}
if len(remove) == 0:
break
ans = [ch for i, ch in enumerate(ans) if i not in remove]
return ans
class Solution(SolutionBase[Output1, Output2, Output3]):
def part_1(self, input_data: InputData) -> Output1:
return sum(1 for line in input_data for ch in line if ch.isalpha())
def part_2(self, input_data: InputData) -> Output2:
return sum(len(Reduction.MODE_1.reduce(line)) for line in input_data)
def part_3(self, input_data: InputData) -> Output3:
return sum(len(Reduction.MODE_2.reduce(line)) for line in input_data)
@codyssi_samples(
(
("part_1", TEST, 52),
("part_2", TEST, 18),
("part_3", TEST, 26),
)
)
def samples(self) -> None:
pass
solution = Solution(12)
def main() -> None:
solution.run(sys.argv)
if __name__ == "__main__":
main()