Skip to content

Instantly share code, notes, and snippets.

@bluepichu
Created December 4, 2020 05:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bluepichu/391dd7ed40684f0eda17c522815796e4 to your computer and use it in GitHub Desktop.
Save bluepichu/391dd7ed40684f0eda17c522815796e4 to your computer and use it in GitHub Desktop.
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)
@bluepichu
Copy link
Author

Note that this fails if the last passport is valid... better lucky than good, I guess 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment