-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path04.rs
141 lines (131 loc) · 4.47 KB
/
04.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
extern crate regex;
use regex::Regex;
use std::collections::HashMap;
use std::fs;
/// Parse a passport entry.
///
/// First we extract a vector with all key-value pairs of the form
/// `key:value` by splitting the data on spaces and new lines. Then
/// all such pairs are split on `:` and inserted into a hash map.
///
/// # Arguments
/// * `passport_data` -- Part of the input data representing one passport.
///
/// # Returns
/// * `HashMap<String, String>` -- The parsed passport data as a key-value map.
fn parse_passport(passport_data: &str) -> HashMap<String, String> {
let entries: Vec<String> = passport_data
.trim()
.to_string()
.split(|x| x == '\n' || x == ' ')
.map(|x| x.to_string())
.collect();
let mut passport: HashMap<String, String> = HashMap::new();
for entry in entries.iter() {
let pair: Vec<&str> = entry.split(':').collect();
passport.insert(pair[0].to_string(), pair[1].to_string());
}
passport
}
/// Check passport for part 1 and 2.
///
/// For part one (`check_values = false`) we only need to check for
/// the presence of all required keys in the hash map.
///
/// For part two (`check_values = true`) we need to perform a specific
/// check for each of the values. The requirements are stated in the
/// problem text as follows:
///
/// * byr (Birth Year) -- four digits; at least 1920 and at most 2002.
/// * iyr (Issue Year) -- four digits; at least 2010 and at most 2020.
/// * eyr (Expiration Year) -- four digits; at least 2020 and at most 2030.
/// * hgt (Height) -- a number followed by either cm or in:
/// * If `cm`, the number must be at least 150 and at most 193.
/// * If `in`, the number must be at least 59 and at most 76.
/// * hcl (Hair Color) -- a # followed by exactly six characters 0-9 or a-f.
/// * ecl (Eye Color) -- exactly one of: `amb` `blu` `brn` `gry` `gr`n `hzl` `oth`.
/// * pid (Passport ID) -- a nine-digit number, including leading zeroes.
/// * cid (Country ID) -- ignored, missing or not.
///
/// # Arguments
/// * `passport` -- Passport data produced by [parse_passport](parse_passport).
/// * `check_values` -- If `false` then only the keys are checked (part 1),
/// otherwise also the values (part 2).
///
/// # Returns
/// * `u8` -- Return 1 if the check passes, otherwise 0;
fn check_passport(passport: &HashMap<String, String>, check_values: bool) -> u8 {
for mandatory_field in ["byr", "iyr", "eyr", "hgt", "hcl", "ecl", "pid"].iter() {
if !passport.contains_key(&mandatory_field.to_string()) {
return 0;
}
}
if !check_values {
return 1;
}
// Part 2 only
// byr
let byr: i32 = passport.get("byr").unwrap().parse().unwrap();
if !(1920..=2002).contains(&byr) {
return 0;
}
// iyr
let iyr: i32 = passport.get("iyr").unwrap().parse().unwrap();
if !(2010..=2020).contains(&iyr) {
return 0;
}
// eyr
let eyr: i32 = passport.get("eyr").unwrap().parse().unwrap();
if !(2020..=2030).contains(&eyr) {
return 0;
}
// hgt
let hgt = passport.get("hgt").unwrap();
let re = Regex::new(r"^(\d+)(cm|in)$").expect("Broken regex");
if !re.is_match(hgt) {
return 0;
}
let caps = re.captures(hgt).unwrap();
let n: i32 = (&caps[1]).parse().unwrap();
let unit: &str = &caps[2];
if !((unit == "cm" && n >= 150 && n <= 193) || (unit == "in" && n >= 59 && n <= 76)) {
return 0;
}
// hcl
let hcl = passport.get("hcl").unwrap();
let re = Regex::new(r"^#[0-9a-f]{6}$").expect("Broken regex");
if !re.is_match(hcl) {
return 0;
}
// ecl
let ecl = passport.get("ecl").unwrap();
let colors = vec!["amb", "blu", "brn", "gry", "grn", "hzl", "oth"];
if !colors.contains(&ecl.as_str()) {
return 0;
}
// pid
let pid = passport.get("pid").unwrap();
let re = Regex::new(r"^\d\d\d\d\d\d\d\d\d$").expect("Broken regex");
if !re.is_match(pid) {
return 0;
}
1
}
#[doc(hidden)]
fn main() {
// Read data
let passports: Vec<HashMap<String, String>> = fs::read_to_string("input/04.txt")
.expect("Can't read input file.")
.split("\n\n")
.map(parse_passport)
.collect();
// Solutions
let mut part_1 = 0;
let mut part_2 = 0;
for passport in passports.iter() {
part_1 += check_passport(passport, false);
part_2 += check_passport(passport, true);
}
println!("Part 1: {}", part_1);
println!("Part 2: {}", part_2);
}