Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 22d1836

Browse files
committed
Day 07: Solution of part 2
1 parent 25aaf73 commit 22d1836

File tree

3 files changed

+33
-4
lines changed

3 files changed

+33
-4
lines changed

‎07/log.txt‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ How many IPs in your puzzle input support TLS?
1414

1515
Your puzzle answer was 115.
1616

17-
The first half of this puzzle is complete! It provides one gold star: *
18-
1917
--- Part Two ---
2018

2119
You would also like to know which IPs support SSL (super-secret listening).
@@ -28,4 +26,8 @@ aba[bab]xyz supports SSL (aba outside square brackets with corresponding bab wit
2826
xyx[xyx]xyx does not support SSL (xyx, but no corresponding yxy).
2927
aaa[kek]eke supports SSL (eke in supernet with corresponding kek in hypernet; the aaa sequence is not related, because the interior character must be different).
3028
zazbz[bzb]cdb supports SSL (zaz has no corresponding aza, but zbz has a corresponding bzb, even though zaz and zbz overlap).
31-
How many IPs in your puzzle input support SSL?
29+
How many IPs in your puzzle input support SSL?
30+
31+
Your puzzle answer was 231.
32+
33+
Both parts of this puzzle are complete! They provide two gold stars: **

‎07/solution.py‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ def __init__(self, nr):
88
self.match_outside = re.compile(r'([^[\]]+)(?:$|\[)')
99
self.match_inside = re.compile(r'\[(.*?)\]')
1010
self.match_abba = re.compile(r'.*(.)(?!1円)(.)2円1円.*')
11+
self.match_aba = re.compile(r'(?=((.)(?!2円)(.)2円))\w')
1112
self.count1 = 0
13+
self.count2 = 0
1214

1315
def calculate(self, test=False):
1416
self.read_instructions()
@@ -23,7 +25,12 @@ def check_ips(self):
2325
words_inside = self.match_inside.findall(line)
2426
if self.contains_abba(words_outside) and not self.contains_abba(words_inside):
2527
self.count1 += 1
28+
aba_ouside = self.findall_aba(words_outside)
29+
aba_inside = self.findall_aba(words_inside)
30+
if self.exists_corresponding_aba(aba_ouside, aba_inside):
31+
self.count2 += 1
2632
self.set_solution(1, self.count1)
33+
self.set_solution(2, self.count2)
2734

2835
def contains_abba(self, words):
2936
contains = False
@@ -33,3 +40,23 @@ def contains_abba(self, words):
3340
contains = True
3441
break
3542
return contains
43+
44+
def findall_aba(self, words):
45+
list_aba = []
46+
if words:
47+
for word in words:
48+
match = self.match_aba.findall(word)
49+
if match:
50+
for m in match:
51+
list_aba.append(m[0])
52+
return list_aba
53+
54+
@staticmethod
55+
def exists_corresponding_aba(words1, words2):
56+
exists = False
57+
for word in words1:
58+
word_correspond = ""+word[1]+word[0]+word[1]
59+
if word_correspond in words2:
60+
exists = True
61+
break
62+
return exists

‎07/test.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from lib import test
22

33

4-
class Test(test.Test):
4+
class Test07(test.Test):
55
@classmethod
66
def setUpClass(cls):
77
cls.nr = '07'

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /