Rust regular expressions
So the remainder of Day4 problem looked like so:
———-
byr
(Birth Year) - four digits; at least1920
and at most2002
.
iyr
(Issue Year) - four digits; at least2010
and at most2020
.eyr
(Expiration Year) - four digits; at least2020
and at most2030
.hgt
(Height) - a number followed by eithercm
orin
:If
cm
, the number must be at least150
and at most193
.If
in
, the number must be at least59
and at most76
.
hcl
(Hair Color) - a#
followed by exactly six characters0
-9
ora
-f
.ecl
(Eye Color) - exactly one of:amb blu brn gry grn hzl oth
.pid
(Passport ID) - a nine-digit number, including leading zeroes.cid
(Country ID) - ignored, missing or not.
———-
While the numbers are easier to handle, if the parsing from string to number works, then it’s just a matter of checking the ranges.
The eye colour is easy too, just check if the value is present in a set.
For the hgt,hcl and pid - seems more appropriate to use a regular expression matcher.
https://docs.rs/regex/1.4.5/regex/
The regular expressions for hcl and pid are straightforward
let rehcl = Regex::new(r"^#[[:xdigit:]]{6}$").unwrap();
let repid = Regex::new(r"^[0-9]{9}$").unwrap();
While the hgt can be matched with single regular expression, could not get how to extract the numeric part for further validation
let re = Regex::new(r"(?P<cmh>^[0-9]{3}cm$)|(?P<inh>^[0-9]{2}in$)").unwrap();
So I’ll match them separately.
As an optimization, I set the regular expressions and the hash set under lazy static.
https://docs.rs/lazy_static/1.4.0/lazy_static/
Code here : https://github.com/aksharau/RustyAdventOfCode/tree/main/Day4/day4Part2
There is a lambda there, don’t kill me for instantiating it per call.
Also I came across this bizarre issue ( well, bizarre to me at-least ) : https://stackoverflow.com/questions/24502282/why-does-removing-return-give-me-an-error-expected-type-but-found-type
Neways, brag time folks!!