This commit is contained in:
Marcus Ferl 2022-12-14 19:48:19 +01:00
parent 60b66e6a06
commit 15ae53cd69
3 changed files with 1030 additions and 49 deletions

View File

@ -1,4 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.10 (AdventOfCode2020)" project-jdk-type="Python SDK" />
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.10" project-jdk-type="Python SDK" />
</project>

89
day4.py
View File

@ -1,9 +1,12 @@
import re
import sys
day = "day4"
file = f"inputs/{day}.txt"
passp = ["byr","iyr","eyr","hgt","hcl","ecl","pid"] # "cid
def part_one(file):
def part_one(_file):
result = 0
input = open(file, "r")
input = input.read()
@ -26,12 +29,13 @@ def check(item):
bool = False
break
return bool
part_one(file)
def part_two(file):
def part_two(_file):
result = 0
input = open(file, "r")
input = open(_file, "r")
input = input.read().split("\n\n")
for line in input:
@ -39,6 +43,8 @@ def part_two(file):
result += 1
print(f"Part 2: {result}")
def line_check(line):
@ -47,38 +53,55 @@ def line_check(line):
bool = True
for code in line:
key_value = code.split(":")
if key_value[0] != "cid":
dict[key_value[0]] = key_value[1]
while bool is not False:
if (int(dict["byr"]) >= 1920) and (int(dict["byr"]) <= 2020):
continue
elif (int(dict["iyr"]) >= 2010) and (int(dict["iyr"]) <= 2020):
continue
elif (int(dict["eyr"]) >= 2020) and (int(dict["eyr"]) <= 2030):
continue
elif ("cm" in dict["hgt"]) or ("in" in dict["hgt"]):
if("cm" in dict["hgt"]):
if (int(dict["hgt"]) >= 150) and (int(dict["hgt"]) <= 193):
continue
elif ("in" in dict["hgt"]):
if (int(dict["hgt"]) >= 59) and (int(dict["hgt"]) <= 76):
continue
else:
bool = False
break
elif (dict["hcl"][0] == '#') and (len(dict["hcl"]) == 6):
continue
elif 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":
continue
elif dict["pid"].count() == 9:
continue
else:
bool = False
break
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
else:
bool = False
if (dict["hcl"][0] == '#') and (len(dict["hcl"]) == 7):
None
else:
bool = False
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:
bool = False
except:
bool = False
return bool
part_two(file)
part_one(file) # 222
part_two(file) # 140

File diff suppressed because it is too large Load Diff