from aoc.input import get_input import re from aoc.partselector import part_one, part_two def pw(line): return line.strip() inp = get_input(pw) fields = [ "byr", "iyr", "eyr", "hgt", "hcl", "ecl", "pid", ] def p1(): inp = get_input(pw) count = 0 passports = [] data = [] valid = 0 for i in inp: if i == '': d = ' '.join(data) pd = d.split(' ') lst = dict(map(lambda x: x.split(':'), pd)) v = True for f in fields: if f not in lst: v = False if v : valid += 1 print(v, sorted(lst.items(), key=lambda x:x[0])) passports.append(' '.join(data)) data = [] continue data.append(i) passports.append(' '.join(data)) data = [] print(valid) fields2 = {"byr": "[0-9]{4}", "iyr": "[0-9]{4}", "eyr": "[0-9]{4}", "hgt": "[0-9]*(in|cm)", "hcl": "#[0-9a-f]{6}", "ecl": "(amb|blu|brn|gry|grn|hzl|oth)", "pid": "[0-9]{9}", } def p2(): inp = get_input(pw) count = 0 passports = [] data = [] valid = 0 for i in inp: if i == '': d = ' '.join(data) pd = d.split(' ') lst = dict(map(lambda x: x.split(':'), pd)) v = True for f in fields2.keys(): print (f, end=" ") if f not in lst: v = False else: print("----->", fields2[f], lst[f]) if f in lst: v = v and re.match(fields2[f], lst[f]) is not None print (v, end=" ") if f == 'pid': v = v and len(lst[f]) == 9 print (v, end=" ") if f == 'byr': v = v and int(lst[f]) >= 1920 and int(lst[f]) <= 2002 print (v, end=" ") if f == 'iyr': v = v and int(lst[f]) >= 2010 and int(lst[f]) <= 2020 print ( v, end=" ") if f == 'eyr': v = v and int(lst[f]) >= 2020 and int(lst[f]) <= 2030 print ( v, end=" ") if f == 'hgt': m = re.match(fields2[f], lst[f]) if m is not None: if m.group(1) == 'in': v = v and int(lst[f][:-2]) <= 76 v = v and int(lst[f][:-2]) >= 59 if m.group(1) == 'cm': v = v and int(lst[f][:-2]) >= 150 v = v and int(lst[f][:-2]) <= 193 print ( v, end=" ") print() if v : valid += 1 if not v: print() print(v, sorted(lst.items(), key=lambda x:x[0])) print() passports.append(' '.join(data)) data = [] continue data.append(i) print(valid) print('done') if part_one(): p1() if part_two(): p2()