[Python/2016] Move solutions into .py files
This commit is contained in:
parent
0269ad8fc3
commit
2514b1d11f
50 changed files with 1172 additions and 3386 deletions
59
Python/2016/07.py
Normal file
59
Python/2016/07.py
Normal file
|
@ -0,0 +1,59 @@
|
|||
from lib import *
|
||||
|
||||
input = read_input(2016, 7)
|
||||
|
||||
|
||||
def check(ip):
|
||||
tmp = ""
|
||||
c = 0
|
||||
ok = False
|
||||
for x in ip:
|
||||
tmp += x
|
||||
if x == "[":
|
||||
c += 1
|
||||
tmp = ""
|
||||
elif x == "]":
|
||||
c -= 1
|
||||
tmp = ""
|
||||
elif len(tmp) >= 4:
|
||||
if tmp[-1] == tmp[-4] and tmp[-2] == tmp[-3] and tmp[-1] != tmp[-2]:
|
||||
if c:
|
||||
return False
|
||||
ok = True
|
||||
return ok
|
||||
|
||||
|
||||
print(sum(check(line) for line in input.splitlines()))
|
||||
|
||||
|
||||
def check(ip):
|
||||
supernet = []
|
||||
hypernet = []
|
||||
tmp = ""
|
||||
c = 0
|
||||
for x in ip:
|
||||
k = tmp
|
||||
tmp += x
|
||||
if x == "[":
|
||||
if k:
|
||||
[hypernet, supernet][not c].append(k)
|
||||
c += 1
|
||||
tmp = ""
|
||||
elif x == "]":
|
||||
if k:
|
||||
[hypernet, supernet][not c].append(k)
|
||||
c -= 1
|
||||
tmp = ""
|
||||
if tmp:
|
||||
[hypernet, supernet][not c].append(tmp)
|
||||
|
||||
for sup in supernet:
|
||||
for i in range(len(sup) - 2):
|
||||
if sup[i] != sup[i + 2] or sup[i] == sup[i + 1]:
|
||||
continue
|
||||
if any(sup[i + 1] + sup[i] + sup[i + 1] in hyp for hyp in hypernet):
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
print(sum(check(line) for line in input.splitlines()))
|
Loading…
Add table
Add a link
Reference in a new issue