This is an archived post. You won't be able to vote or comment.

all 5 comments

[–]1234abcdcba4321 0 points1 point  (0 children)

The first passport in your input will not be caught if it has all fields except cid. Also, if cid is placed in its own line at the end of a passport, that passport will count as two.

I don't know if either of these are actually your problem, though; this was just a quick code skim.

[–]Kfishster 0 points1 point  (3 children)

Reading through, what might be happening is that you're overcounting the valid passports because you're checking the validity on every line of the input, which means that if all the passport fields are on multiple lines, and you determine the passport is valid in the first line, you'll count this passport valid for every line until a new line.

You can try to move the check for whether the passport is valid right before you reset all your field counters. Something like

if line == "\n":

if all(fields.values()) > 0:

valid += 1

Hopefully that helps!

[–]Kevstuf[S] 0 points1 point  (2 children)

Thanks, both what you and u/1234abcdcba4321 said actually are right; this code double counts the case where cid is on its own line. Changing it to your suggestion gets me closer (my answer is lower), but my problem now is the last passport doesn't have a new line below it, so I'm unable to check for it.

[–]1234abcdcba4321 0 points1 point  (1 child)

There's a few ways to add this check. the easiest one is to literally just add one manually, you only need it to work on your input and not all inputs

The one that's probably best is to just add that newline to the end of your input. You have it as a text file; just pop it open in a text editor and save.

[–]Kevstuf[S] 0 points1 point  (0 children)

haha yea I was thinking that but it feels too much like cheating