DEV Community

Discussion on: Advent of Code: 2020 Day 04

Collapse
 
sergiogragera profile image
Sergio Gragera • Edited

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
Enter fullscreen mode Exit fullscreen mode
Collapse
 
cnille profile image
Christopher Nilsson

Really clean solution! Thanks for sharing! 🦄