49 lines
1.1 KiB
Python
Executable File
49 lines
1.1 KiB
Python
Executable File
#!/usr/bin/env python
|
|
|
|
import re
|
|
import argparse
|
|
|
|
|
|
RE_ABA = re.compile(r'(?=((\w)(?!\2)\w\2))')
|
|
RE_HYPERNET = re.compile(r'\[([^\]]+)\]')
|
|
|
|
def load_file(filename):
|
|
with open(filename) as f:
|
|
for line in f:
|
|
yield line.strip()
|
|
|
|
def check_ip(data):
|
|
def reverse_aba(aba):
|
|
return "{}{}{}".format(aba[1], aba[0], aba[1])
|
|
|
|
aba_all = []
|
|
for aba in RE_ABA.findall(data):
|
|
aba_all.append(aba[0])
|
|
|
|
aba_hyper = []
|
|
for hyper in RE_HYPERNET.findall(data):
|
|
for aba in RE_ABA.findall(hyper):
|
|
aba_hyper.append(aba[0])
|
|
|
|
for aba in aba_hyper:
|
|
aba_all.remove(aba)
|
|
|
|
for aba in aba_hyper:
|
|
if reverse_aba(aba) in aba_all:
|
|
return 1
|
|
return 0
|
|
|
|
def main(args):
|
|
count = 0
|
|
for LINE in load_file(args.input):
|
|
count += check_ip(LINE)
|
|
|
|
print(count)
|
|
|
|
if __name__ == "__main__":
|
|
parser = argparse.ArgumentParser(description='AdventOfCode 2016 Day 01')
|
|
parser.add_argument('--input', '-i', action='store', required=True, help='input file')
|
|
args = parser.parse_args()
|
|
|
|
main(args)
|