AdventOfCode2020/day4.py

107 lines
2.3 KiB
Python
Raw Normal View History

2022-12-14 19:48:19 +01:00
import re
import sys
2022-12-14 15:41:25 +01:00
day = "day4"
file = f"inputs/{day}.txt"
passp = ["byr","iyr","eyr","hgt","hcl","ecl","pid"] # "cid
2022-12-14 19:48:19 +01:00
def part_one(_file):
2022-12-14 15:41:25 +01:00
result = 0
input = open(file, "r")
input = input.read()
inputs = input.split("\n\n")
for item in inputs:
if(check(item)):
result += 1
print(f"Part 1: {result}")
def check(item):
bool = True
for ps in passp:
if(ps in item):
continue
else:
bool = False
break
return bool
2022-12-14 19:48:19 +01:00
def part_two(_file):
2022-12-14 15:41:25 +01:00
result = 0
2022-12-14 19:48:19 +01:00
input = open(_file, "r")
2022-12-14 15:41:25 +01:00
input = input.read().split("\n\n")
for line in input:
if line_check(line):
result += 1
2022-12-14 19:48:19 +01:00
2022-12-14 15:41:25 +01:00
print(f"Part 2: {result}")
def line_check(line):
dict = {}
line = line.replace("\n", " ").split(" ")
bool = True
for code in line:
key_value = code.split(":")
2022-12-14 19:48:19 +01:00
if key_value[0] != "cid":
dict[key_value[0]] = key_value[1]
try:
if (int(dict["byr"]) >= 1920) and (int(dict["byr"]) <= 2002):
None
else:
bool = False
if (int(dict["iyr"]) >= 2010) and (int(dict["iyr"]) <= 2020):
None
else:
bool = False
if (int(dict["eyr"]) >= 2020) and (int(dict["eyr"]) <= 2030):
None
else:
bool = False
if ("cm" in dict["hgt"]) or ("in" in dict["hgt"]):
if "cm" in dict["hgt"]:
if (int(dict["hgt"].replace("cm","")) >= 150) and (int(dict["hgt"].replace("cm","")) <= 193):
None
elif "in" in dict["hgt"]:
if (int(dict["hgt"].replace("in","")) >= 59) and (int(dict["hgt"].replace("in","")) <= 76):
None
2022-12-14 15:41:25 +01:00
else:
bool = False
2022-12-14 19:48:19 +01:00
if (dict["hcl"][0] == '#') and (len(dict["hcl"]) == 7):
None
2022-12-14 15:41:25 +01:00
else:
bool = False
2022-12-14 19:48:19 +01:00
if dict["ecl"] == "amb" or dict["ecl"] == "blu" or dict["ecl"] == "brn" or dict["ecl"] == "gry" or dict["ecl"] == "grn" or dict["ecl"] == "hzl" or dict["ecl"] == "oth":
None
else:
bool = False
if len(dict["pid"]) == 9:
None
else:
2022-12-14 15:41:25 +01:00
2022-12-14 19:48:19 +01:00
bool = False
2022-12-14 15:41:25 +01:00
2022-12-14 19:48:19 +01:00
except:
bool = False
return bool
2022-12-14 15:41:25 +01:00
2022-12-14 19:48:19 +01:00
part_one(file) # 222
part_two(file) # 140