-
Notifications
You must be signed in to change notification settings - Fork 0
/
part_b.py
executable file
·67 lines (49 loc) · 1.63 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
#!/usr/bin/env python3
import re
from abc import ABC
from typing import List
import utils
from year_2017.day_09 import part_a
class Challenge(utils.BaseChallenge):
part_a_for_testing = part_a
def solve(self, _input, debug=False):
"""
>>> Challenge().default_solve()
7314
"""
return NodeExtended.from_node_text(_input.strip()).get_garbage_count()
class NodeExtended(part_a.Node, ABC):
garbage_class = NotImplemented
group_class = NotImplemented
def get_garbage_count(self):
raise NotImplementedError()
class GarbageExtended(NodeExtended, part_a.Garbage):
def get_garbage_count(self):
"""
>>> GarbageExtended.parse('<>')[0].get_garbage_count()
0
>>> GarbageExtended.parse('<random characters>')[0].get_garbage_count()
17
>>> GarbageExtended.parse('<<<<>')[0].get_garbage_count()
3
>>> GarbageExtended.parse('<{!>}>')[0].get_garbage_count()
2
>>> GarbageExtended.parse('<!!>')[0].get_garbage_count()
0
>>> GarbageExtended.parse('<!!!>>')[0].get_garbage_count()
0
>>> GarbageExtended.parse('<{o"i!a,<{i<a>')[0].get_garbage_count()
10
"""
return len(re.sub(rf"{self.ESCAPE}.", '', self.contents))
NodeExtended.garbage_class = GarbageExtended
class GroupExtended(NodeExtended, part_a.Group):
contents: List['GroupExtended']
def get_garbage_count(self):
return sum(
content.get_garbage_count()
for content in self.contents
)
NodeExtended.group_class = GroupExtended
Challenge.main()
challenge = Challenge()