DEV Community

Discussion on: Advent of Code: 2020 Day 04

Collapse
 
gubesch profile image
Christian Gubesch

Hello Christopher,
I just wanted to let you know that in your section with validate height you would run into some edge cases with the last if statement.

For example for my quiz input this solution wouldn't work.

You should consider the if elif and append an else at the end to catch all remaining cases.

    # Validate Height
    if 'cm' in passport['hgt']:
            return False
    elif 'in' in passport['hgt']:
            return False
    else:
            return False
Enter fullscreen mode Exit fullscreen mode

If you want to highlight your python code here on dev.to just write python after the three backticks ;)

Cheers,
Christian

Collapse
 
cnille profile image
Christopher Nilsson

Thanks for the tip on highlighting! :D

An else wouldn't let the flow through to validate the Validate strings/enums, so would have to reorder the code then. Could you share what the case is in your input that I don't catch? :)

Collapse
 
gubesch profile image
Christian Gubesch

Hello Christopher,
I can only tell that your solution is off by 1 compared to mine at my input file.

I use this code right here:

def isValidPuzzle2(self):
        attributes = vars(self)
        if len(attributes) == 8 or (len(attributes) == 7 and "cid" not in attributes):
            if not (1920 <= int(self.byr) <= 2002):
                return False
            if not (2010 <= int(self.iyr) <= 2020):
                return False
            if not (2020 <= int(self.eyr) <= 2030):
                return False
            if "cm" in self.hgt:
                if not 150 <= int(self.hgt[:-2]) <= 193:
                    return False
            elif "in" in self.hgt:
                if not 59 <= int(self.hgt[0:-2]) <= 76:
                    return False
            else:
                return False
            patternHairColor = re.compile("^#[a-f0-9]{6}$")
            if not patternHairColor.match(self.hcl):
                return False
            eyeColors = ["amb","blu","brn","gry","grn","hzl","oth"]
            if self.ecl not in eyeColors:
                return False
            patternPid = re.compile("^\d{9}$")
            if not patternPid.match(self.pid):
                return False
            return True
        else:
            return False
Enter fullscreen mode Exit fullscreen mode

My console output:

PS C:\Users\christian.gubesch\Documents\DEV\AdventOfCode\Python\Day4> python.exe .\day4_devto.py
Solution part 1: 241
Solution part 2: 185
PS C:\Users\christian.gubesch\Documents\DEV\AdventOfCode\Python\Day4> python.exe .\day4.py      
Puzzle 1: 242
Puzzle 2: 186
Enter fullscreen mode Exit fullscreen mode

Maybe I have time tomorrow to find the specific case for you.

Cheers ;)