Solve 2020 day 4

This commit is contained in:
felixm 2024-08-26 21:23:57 -04:00
parent 04d8c44d66
commit 631205086d
2 changed files with 108 additions and 1 deletions

106
2020/d4.py Normal file
View File

@ -0,0 +1,106 @@
from lib import get_data, Grid2D
required = [
"byr",
"iyr",
"eyr",
"hgt",
"hcl",
"ecl",
"pid",]
# hgt (Height) - a number followed by either cm or in:
#
# If cm, the number must be at least 150 and at most 193.
# If in, the number must be at least 59 and at most 76.
def part_1(data):
passwords = [""]
for line in data.splitlines():
if line.strip() == "":
passwords.append("")
else:
passwords[-1] = passwords[-1] + " " + line.strip()
valid = 0
for pw in passwords:
for r in required:
if r not in pw:
break
else:
valid += 1
print(valid)
kws = []
for pw in passwords:
d = {}
for word in pw.split():
key, value = word.split(":")
d[key] = value
kws.append(d)
valid = 0
for kw in kws:
for r in required:
if r not in kw:
break
match r:
case "hgt":
v = int(kw[r][:-2])
if kw[r].endswith("cm"):
if v < 150 or v > 193:
break
elif kw[r].endswith("in"):
if v < 59 or v > 76:
break
else:
break
case "byr":
v = int(kw[r])
if v < 1920 or v > 2002:
break
case "iyr":
v = int(kw[r])
if v < 2010 or v > 2020:
break
case "eyr":
v = int(kw[r])
if v < 2020 or v > 2030:
break
case "hcl":
v = kw[r]
if len(v) != 7 or not v.startswith("#"):
break
try:
int(v.replace("#", ""), 16)
except ValueError:
break
case "pid":
v = kw[r]
if len(v) != 9:
break
try:
int(v, 10)
except ValueError:
break
case "ecl":
v = kw[r]
if not v in ["amb", "blu", "brn", "gry", "grn", "hzl", "oth"]:
break
case _:
assert False
else:
valid += 1
print(valid)
def main():
data = get_data(__file__)
part_1(data)
if __name__ == "__main__":
main()

View File

@ -136,7 +136,8 @@ Solutions and utility script for Advent of Code challenges in Python.
- Day 1: 2:48 (people weren't able to submit because of a website outage)
- Day 2: 4:47 (no leaderboard, you can tell it's getting faster)
- Day 3: 7:06 (way too slow, lol; time to take it seriously)
- Day 4:
- Day 4: 14:30 (yo, I am just too slow)
- Day 5:
## AoC 2022