-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path02.rs
57 lines (49 loc) · 1.66 KB
/
02.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
extern crate regex;
use regex::Regex;
use std::fs;
/// Solve both part 1 and part 2.
///
/// # Arguments
/// * `lines`: The lines from the input file.
///
/// # Description
/// Check line by line if the conditions of part 1 and part 2 are fulfilled.
fn solve(lines: &[String]) -> (i32, i32) {
let re = Regex::new(r"(\d+)-(\d+) ([a-z]): ([a-z]+)").expect("Broken regex");
let (mut part_1, mut part_2) = (0, 0);
for line in lines.iter() {
// Parse line of the form "n_1-n_2 c: password"
let caps = re.captures(line).unwrap();
let n_1: usize = (&caps[1]).parse().unwrap();
let n_2: usize = (&caps[2]).parse().unwrap();
let c: char = (&caps[3]).parse().unwrap();
let password: String = (&caps[4]).parse().unwrap();
// Part 1
// Count occurrences of c and check n_1 <= count <= n_2
let count = password.matches(c).count();
if count >= n_1 && count <= n_2 {
part_1 += 1;
}
// Part 2
// Check that (password[n_1 - 1] == c) xor (password[n_2 - 1] == c)
let has_1 = c == password.chars().nth(n_1 - 1).unwrap();
let has_2 = c == password.chars().nth(n_2 - 1).unwrap();
if (has_1 && !has_2) || (!has_1 && has_2) {
part_2 += 1;
}
}
(part_1, part_2)
}
#[doc(hidden)]
fn main() {
// Read input
let lines: Vec<String> = fs::read_to_string("input/02.txt")
.expect("Can't read input file")
.lines()
.map(|x| x.parse().expect("Can't parse line"))
.collect();
// Solve
let (part_1, part_2) = solve(&lines);
println!("Part 1: {}", part_1);
println!("Part 2: {}", part_2);
}