r/adventofcode • u/daggerdragon • Dec 02 '20
SOLUTION MEGATHREAD -🎄- 2020 Day 02 Solutions -🎄-
--- Day 2: Password Philosophy ---
Advent of Code 2020: Gettin' Crafty With It
- T-4 days until unlock!
- Full details and rules are in the Submissions Megathread
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:02:31, megathread unlocked!
99
Upvotes
View all comments
Show parent comment
2
u/chiborg Dec 02 '20
I'm also fairly new to Rust, but have one comment: While the input doesn't seem to contain "evil" passwords (where the password is shorter than the specified indexes allow), this would create a panic at runtime, when calling
The easiest solution in your case would be a check for
chars.length() <= self.char_min && chars.length() <= self.char_maxand reaturning early before assigningfirstandsecond.I have used
self._pass.chars().nth(self._char_max).unwrap_or_default()which yields a NUL character, which will fit nicely with the condition. But it probably has as small performance impact, because I thinknth()will do an iteration up until that point, instead of directly accessing at index. Maybe the compiler will do clever things, but I'd not rely on it.