Created
December 4, 2020 05:18
-
-
Save bluepichu/391dd7ed40684f0eda17c522815796e4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from advent import Input | |
import re | |
input = Input(day = 4).lines() | |
ans = 0 | |
keys = set() | |
for ln in input: | |
if ln == "": | |
if "byr" in keys and "iyr" in keys and "eyr" in keys and "hgt" in keys and "hcl" in keys and "ecl" in keys and "pid" in keys: | |
ans += 1 | |
keys = set() | |
else: | |
for part in ln.split(" "): | |
key = part.split(":")[0] | |
value = part.split(":")[1] | |
ok = False | |
if key == "byr": | |
if 1920 <= int(value) <= 2020: ok = True | |
elif key == "iyr": | |
if 2010 <= int(value) <= 2020: ok = True | |
elif key == "eyr": | |
if 2020 <= int(value) <= 2030: ok = True | |
elif key == "eyr": | |
if 2020 <= int(value) <= 2030: ok = True | |
elif key == "hgt": | |
if value[-2:] == "cm" and 150 <= int(value[:-2]) <= 193: ok = True | |
if value[-2:] == "in" and 59 <= int(value[:-2]) <= 76: ok = True | |
elif key == "hcl": | |
if re.compile(r"^#[0-9a-f]{6}$").match(value): ok = True | |
elif key == "ecl": | |
if value in ["amb", "blu", "brn", "gry", "grn", "hzl", "oth"]: | |
ok = True | |
elif key == "pid": | |
if re.compile(r"^\d{9}$").match(value): ok = True | |
if ok: | |
keys.add(key) | |
print(ans) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note that this fails if the last passport is valid... better lucky than good, I guess 😄