-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path07_Internet_Protocol_Version_7.py
55 lines (39 loc) · 1.26 KB
/
07_Internet_Protocol_Version_7.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
from aoc import *
import re
from itertools import chain
import more_itertools as mtls
from typing import Iterable
ips = read(sep=r'\[|\]')
def has_abba(s: str) -> bool:
for a, b, c, d in mtls.windowed(s, 4):
if a == d and b == c and a != b:
return True
total = 0
for ip in ips:
total += any(has_abba(s) for s in ip[::2]) and not any(has_abba(s) for s in ip[1::2])
print('Star 1:', total)
def find_all_aba(s: str) -> Iterable[str]:
for a, b, c in mtls.windowed(s, 3):
if a == c and a != b:
yield a, b
total = 0
for ip in ips:
for a, b in chain.from_iterable(find_all_aba(s) for s in ip[::2]):
tmp = ''.join([b, a, b])
if any(tmp in s for s in ip[1::2]):
total += 1
break
print('Star 2:', total)
### Alternative solution with re ###
total = 0
for ip in ips:
total += any(re.search(r'(.)((?!\1).)\2\1', s) for s in ip[::2]) and not any(re.search(r'(.)((?!\1).)\2\1', s) for s in ip[1::2])
print('Star 1 (re):', total)
total = 0
for ip in ips:
for _, a, b in chain.from_iterable(re.findall(r'(?=((.)((?!\2).)\2))', x) for x in ip[::2]):
tmp = b+a+b
if any(tmp in x for x in ip[1::2]):
total += 1
break
print('Star 2 (re):', total)