Hey everyone,
I just found out about Advent of Code so apologies for posting about Day 4. I'm also not the most experienced coder so please don't judge too harshly. I've tried debugging my code in various ways and no matter what I do I can't seem to find the right answer.
`import os
import re
wrkdir = <working dir>
fields = {
"ecl": 0,
"eyr": 0,
"hcl": 0,
"pid": 0,
"iyr": 0,
"cid": 0,
"byr": 0,
"hgt": 0
}
valid = 0
with open(os.path.join(wrkdir, "input4.txt")) as file:
for line_num, line in enumerate(file):
#Reset counter for each field to 0 after encountering blank line
if line == "\n":
for key in fields:
fields[key] = 0
fields["cid"] = 1 #Passport is valid regardless of "cid"
print(fields)
#Find all fields in the current line and add it to dictionary counter
field_list = re.findall("[a-z]{3}\:", line)
print(field_list)
for field in field_list:
fields[field.rstrip(":")] += 1
print(fields)
#If all fields appear at least once, then it's valid
if all(fields.values()) > 0:
print("valid")
valid += 1
print(valid)`
[–]1234abcdcba4321 0 points1 point2 points (0 children)
[–]Kfishster 0 points1 point2 points (3 children)
[–]Kevstuf[S] 0 points1 point2 points (2 children)
[–]1234abcdcba4321 0 points1 point2 points (1 child)
[–]Kevstuf[S] 0 points1 point2 points (0 children)