Repository navigation
Expand file tree
/
Copy pathmain.rs
More file actions
179 lines (155 loc) · 4.7 KB
/
Copy pathmain.rs
File metadata and controls
179 lines (155 loc) · 4.7 KB
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#[macro_use]
extern crate lazy_static;
use regex::Regex;
use std::collections::{HashMap, HashSet};
fn load_input() -> String {
std::fs::read_to_string("dec04/src/input.txt").expect("Unable to read input file")
}
fn puzzle_1() -> u32 {
let input = load_input();
let mut valid_passports = 0;
let required_fields: HashSet<&'static str> = ["byr", "iyr", "eyr", "hgt", "hcl", "ecl", "pid"]
.iter()
.cloned()
.collect();
for passport in input.split("\n\n") {
let fields: HashSet<&str> = passport
.split_ascii_whitespace()
.map(|field| field.split(':').nth(0).unwrap())
.collect();
let differences = required_fields.difference(&fields);
if differences.count() == 0 {
valid_passports += 1;
}
}
valid_passports
}
type Callback<'a> = Box<(dyn Fn(&'a str) -> bool + 'static)>;
fn make_callback<'a, F>(f: F) -> Callback<'a>
where
F: Fn(&'a str) -> bool + 'static,
{
Box::new(f) as Callback
}
struct FieldDefinition<'a> {
name: String,
validator: Callback<'a>,
}
impl<'a> FieldDefinition<'a> {
fn new<S: ToString>(name: S, validator: Callback<'a>) -> Self {
Self {
name: name.to_string(),
validator,
}
}
fn validate(&self, value: &'a str) -> bool {
(self.validator)(value)
}
}
fn puzzle_2() -> u32 {
let input = load_input();
let mut valid_passports = 0;
let eye_colors: HashSet<&'static str> = ["amb", "blu", "brn", "gry", "grn", "hzl", "oth"]
.iter()
.cloned()
.collect();
let required_fields = vec![
FieldDefinition::new(
"byr",
make_callback(|value| match value.parse::<u32>() {
Ok(byr) => byr >= 1920 && byr <= 2002,
Err(_) => false,
}),
),
FieldDefinition::new(
"iyr",
make_callback(|value| match value.parse::<u32>() {
Ok(iyr) => iyr >= 2010 && iyr <= 2020,
Err(_) => false,
}),
),
FieldDefinition::new(
"eyr",
make_callback(|value| match value.parse::<u32>() {
Ok(eyr) => eyr >= 2020 && eyr <= 2030,
Err(_) => false,
}),
),
FieldDefinition::new(
"hgt",
make_callback(|value| {
if value.contains("cm") {
return match value.split("cm").nth(0).unwrap().parse::<u32>() {
Ok(hgt) => hgt >= 150 && hgt <= 193,
Err(_) => false,
};
}
if value.contains("in") {
return match value.split("in").nth(0).unwrap().parse::<u32>() {
Ok(hgt) => hgt >= 59 && hgt <= 76,
Err(_) => false,
};
}
false
}),
),
FieldDefinition::new(
"hcl",
make_callback(|value| {
lazy_static! {
static ref RE: Regex = Regex::new(r"^#[0-9,a-f]{6}$").unwrap();
}
RE.is_match(value)
}),
),
FieldDefinition::new(
"ecl",
make_callback(move |value| eye_colors.contains(value)),
),
FieldDefinition::new(
"pid",
make_callback(|value| {
lazy_static! {
static ref RE:Regex = Regex::new(r"^[0-9]{9}$").unwrap();
}
RE.is_match(value)
}),
),
];
for passport in input.split("\n\n") {
let fields: HashMap<&str, &str> = passport
.split_ascii_whitespace()
.map(|field| {
let split: Vec<&str> = field.split(':').collect();
let key = split[0];
let value = split[1];
(key, value)
})
.collect();
let mut valid = true;
for field_definition in &required_fields {
let field_name = field_definition.name.as_str();
if fields.contains_key(field_name) {
let value = fields[field_name];
if !field_definition.validate(value) {
valid = false;
}
} else {
valid = false;
}
if !valid {
break;
}
}
if valid {
valid_passports += 1;
}
}
valid_passports
}
fn main() {
let result = puzzle_1();
println!("Puzzle 1 output: {}", result);
let result = puzzle_2();
println!("Puzzle 2 output: {}", result);
}