-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathday11.py
77 lines (61 loc) · 1.89 KB
/
day11.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
from common.aoc import AoCDay
def increment_string(source: str) -> str:
reversed_source = source[::-1]
incremented = []
act = 1
for i in reversed_source:
# print(i)
if not act:
incremented.append(i)
continue
if i == "z":
incremented.append("a")
else:
incremented.append(chr(ord(i) + 1))
act = 0
if act:
incremented.append("a")
return "".join(incremented[::-1])
def increasing(source) -> bool:
consec = 0
for c, i in enumerate(source):
if consec == 0:
consec = 1
continue
if chr(ord(source[c - 1]) + 1) == i:
consec += 1
else:
consec = 1
if consec == 3:
return True
return False
def not_iol(source) -> bool:
return "i" not in source and "o" not in source and "l" not in source
def pairs(source) -> bool:
first_pair = None
for c, i in enumerate(source):
if c == 0:
continue
if source[c - 1] == i:
if first_pair and first_pair != i:
return True
first_pair = i
return False
def valid(source: str) -> bool:
return increasing(source) and not_iol(source) and pairs(source)
class Day(AoCDay):
def __init__(self, test=0):
super().__init__(__name__, test)
def _preprocess_input(self) -> None:
self.__input_data = self._input_data[0][0]
def _calculate_1(self) -> str:
proposed_pwd = increment_string(self.__input_data)
while not valid(proposed_pwd):
proposed_pwd = increment_string(proposed_pwd)
return proposed_pwd
def _calculate_2(self) -> str:
x = self._calculate_1()
proposed_pwd = increment_string(x)
while not valid(proposed_pwd):
proposed_pwd = increment_string(proposed_pwd)
return proposed_pwd