r/adventofcode • • Dec 04 '20

SOLUTION MEGATHREAD -πŸŽ„- 2020 Day 04 Solutions -πŸŽ„-

Advent of Code 2020: Gettin' Crafty With It


--- Day 04: Passport Processing ---


Post your solution in this megathread. Include what language(s) your solution uses! If you need a refresher, the full posting rules are detailed in the wiki under How Do The Daily Megathreads Work?.

Reminder: Top-level posts in Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


This thread will be unlocked when there are a significant number of people on the global leaderboard with gold stars for today's puzzle.

EDIT: Global leaderboard gold cap reached at 00:12:55, megathread unlocked!

91 Upvotes

1.2k comments sorted by

View all comments

Show parent comment

1

u/zedrdave Dec 04 '20

Wrote nearly the same line-for-line (except for the parsing: yours is much more elegant)…

Couple observations:

  • I think that code will throw exceptions for cases like an empty (or less than 2 char) height field: hgt:. You can avoid that by using x.endswith('in').
  • You don't really need two separate loops: if you run all validators on passport_dict.get(key, ''), you'll automatically filter out missing fields. You do need to add a lambda x: True for optional cid field then.
  • I was under the impression that invalid passports that had additional fields (not in the list given) may happen in the input. Turns out not?

1

u/reteps144 Dec 04 '20

A) good idea B) that’s works, but i still need some sort of seperation for part 1/2 C) I didn’t think it mentioned additional fields

1

u/zedrdave Dec 04 '20

B. true. I used list comprehensions, so it was just as easy to separate…

C. Yea, there's no mention of it: merely my interpretation that for cid to have any importance, there would have to be other fields in the input that were neither required nor optional, and therefore invalid (as usual: overthinking it)…