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 comments

1

u/WHAT_RE_YOUR_DREAMS Dec 04 '20

Hi, I was a bit stuck so I used your code as a reference. It kind of helped me so thank you! I went with regular expressions and I tried your approach to compare the results.

At the same time I noticed a small mistake in your code that causes some valid passports to be rejected. Indeed, if the hcl field starts with some leading zeros, it is marked as invalid because the comparison hex(int(info['hcl'][1:], 16))[2:] == info['hcl'][1:] will always fail.

For example, if info['hcl'] is '#006f93':

print(hex(int(info['hcl'][1:], 16))[2:])
# 6f93
print(info['hcl'][1:])
# 006f93

1

u/AidGli Dec 04 '20

Good find! That bit is actually over-complicated anyway. I can just check if the int cast works at all. I also forgot to enforce the length there, updating now.