Only with regex
import re def is_valid(credentials): regex = { 'byr': r'^(19[2-9][0-9]|200[0-2])$', 'iyr': r'^(201[0-9]|2020)$', 'eyr': r'^(202[0-9]|2030)$', 'hgt': r'^((1[5-8][0-9]|19[0-3])cm|(59|6[0-9]|7[0-6])in)$', 'hcl': r'^#[0-9a-f]{6}$', 'ecl': r'^(amb|blu|brn|gry|grn|hzl|oth)$', 'pid': r'^\d{9}$'} if regex.keys() <= credentials.keys(): for key in credentials: if key != 'cid': comp = re.compile(regex[key]) if not comp.match(credentials[key]): return False return True return False def part2(file): valid_credentials = 0 credentials = {} for line in file: line_withou_break = line.replace('\n', '') if len(line_withou_break) > 0: pairs = line_withou_break.split(' ') for pair in pairs: key, value = pair.split(':') credentials[key] = value else: valid_credentials += 1 if is_valid(credentials) else 0 credentials = {} return valid_credentials
Really clean solution! Thanks for sharing! 🦄
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Only with regex
Really clean solution! Thanks for sharing! 🦄