Skip to content

Commit a0eddda

Browse files
author
Chris Poch
committed
Completed Day 19
1 parent 94a516f commit a0eddda

File tree

1 file changed

+48
-3
lines changed

1 file changed

+48
-3
lines changed

19.py

+48-3
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,58 @@ def checkAllChars(list):
8787
part1Pattern = re.compile(strRule0)
8888

8989
#check remaining strings
90+
matchLength = 0
9091
matches = 0
92+
possiblePart2 = []
9193
for msg in messages:
9294
if part1Pattern.match(msg) != None:
9395
matches += 1
96+
matchLength = len(msg)
97+
else:
98+
if len(msg) > matchLength:
99+
possiblePart2.append(msg)
100+
94101
print(matches)
95102

96103
#implement part 2 changes
97-
rules[8] = ["(",42,"|",42,8,")"]
98-
rules[11] = ["(",42,31,"|",42,11,31,")"]
99-
104+
#rules[8] = ["(",42,"|",42,8,")"] # was 42; now produces 42 42 42 42...
105+
#rules[11] = ["(",42,31,"|",42,11,31,")"] # was 42 31; now produces 42 ... 31 ...
106+
#rule 0 is 8 11 so produces 42 n * 31 m, n > m
107+
108+
#need to find 42 and 31
109+
rule42 = rules[42]
110+
allChars = False
111+
while not allChars:
112+
for i in range(len(rule42)):
113+
if isinstance(rule42[i], int):
114+
rule42 = rule42[0:i] + rules[rule42[i]] + rule42[i+1:]
115+
allChars = checkAllChars(rule42)
116+
strRule42 = ""
117+
for item in rule42:
118+
strRule42 += str(item)
119+
120+
rule31 = rules[31]
121+
allChars = False
122+
while not allChars:
123+
for i in range(len(rule31)):
124+
if isinstance(rule31[i], int):
125+
rule31 = rule31[0:i] + rules[rule31[i]] + rule31[i+1:]
126+
allChars = checkAllChars(rule31)
127+
strRule31 = ""
128+
for item in rule31:
129+
strRule31 += str(item)
130+
131+
#check possible part 2
132+
strRule8 = strRule42 + '+'
133+
strRule11 = r'(' + '|'.join([f'{strRule42}{{{n}}}{strRule31}{{{n}}}' for n in range(1,5)]) + ')'
134+
strRule0 = '^' + strRule8 + strRule11 + '$'
135+
136+
part2Pattern = re.compile(strRule0)
137+
138+
#check remaining strings
139+
matches = 0
140+
for msg in messages:
141+
if part2Pattern.match(msg) != None:
142+
matches += 1
143+
144+
print(matches)

0 commit comments

Comments
 (0)