Day 2, add handrolled parse to compare against regex.
This commit is contained in:
parent
bc8de05d69
commit
80b7312bf8
@ -24,10 +24,14 @@ Day 1 - Part 2 : 165026160
|
|||||||
```
|
```
|
||||||
AOC 2020
|
AOC 2020
|
||||||
Day 2 - Part 1 : 640
|
Day 2 - Part 1 : 640
|
||||||
generator: 1.821498ms,
|
generator: 1.732103ms,
|
||||||
runner: 115.525µs
|
runner: 100.802µs
|
||||||
|
|
||||||
|
Day 2 - Part 1 - handrolled : 640
|
||||||
|
generator: 157.527µs,
|
||||||
|
runner: 97.775µs
|
||||||
|
|
||||||
Day 2 - Part 2 : 472
|
Day 2 - Part 2 : 472
|
||||||
generator: 1.52241ms,
|
generator: 1.374162ms,
|
||||||
runner: 10.459µs
|
runner: 10.461µs
|
||||||
```
|
```
|
||||||
|
|||||||
@ -44,7 +44,7 @@ struct Policy {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[aoc_generator(day2)]
|
#[aoc_generator(day2)]
|
||||||
fn parse(input: &str) -> Vec<Policy> {
|
fn parse_regex(input: &str) -> Vec<Policy> {
|
||||||
let re = Regex::new(r"(\d+)-(\d+) (\w): (.*)").expect("Failed to compile regex");
|
let re = Regex::new(r"(\d+)-(\d+) (\w): (.*)").expect("Failed to compile regex");
|
||||||
input
|
input
|
||||||
.split('\n')
|
.split('\n')
|
||||||
@ -61,6 +61,38 @@ fn parse(input: &str) -> Vec<Policy> {
|
|||||||
.collect()
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[aoc_generator(day2, part1, handrolled)]
|
||||||
|
fn parse_handrolled(input: &str) -> Vec<Policy> {
|
||||||
|
// Example line:
|
||||||
|
// 1-3 a: abcde
|
||||||
|
input
|
||||||
|
.split('\n')
|
||||||
|
.filter_map(|line| {
|
||||||
|
let start = 0;
|
||||||
|
let end = line.find('-')?;
|
||||||
|
let min: usize = line[start..end].parse().ok()?;
|
||||||
|
|
||||||
|
let start = end + 1;
|
||||||
|
let end = line.find(' ')?;
|
||||||
|
let max: usize = line[start..end].parse().ok()?;
|
||||||
|
|
||||||
|
let start = end + 1;
|
||||||
|
let end = line.find(':')?;
|
||||||
|
let letter = line[start..end].to_string();
|
||||||
|
|
||||||
|
let start = end + 2;
|
||||||
|
let password = line[start..].to_string();
|
||||||
|
|
||||||
|
Some(Policy {
|
||||||
|
min,
|
||||||
|
max,
|
||||||
|
letter,
|
||||||
|
password,
|
||||||
|
})
|
||||||
|
})
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
|
|
||||||
fn is_valid_policy_part1(p: &Policy) -> bool {
|
fn is_valid_policy_part1(p: &Policy) -> bool {
|
||||||
let c = p.password.matches(&p.letter).count();
|
let c = p.password.matches(&p.letter).count();
|
||||||
p.min <= c && c <= p.max
|
p.min <= c && c <= p.max
|
||||||
@ -71,6 +103,11 @@ fn valid_policy_count_part1(policies: &[Policy]) -> usize {
|
|||||||
policies.iter().filter(|p| is_valid_policy_part1(p)).count()
|
policies.iter().filter(|p| is_valid_policy_part1(p)).count()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[aoc(day2, part1, handrolled)]
|
||||||
|
fn valid_policy_count_handrolled_part1(policies: &[Policy]) -> usize {
|
||||||
|
policies.iter().filter(|p| is_valid_policy_part1(p)).count()
|
||||||
|
}
|
||||||
|
|
||||||
fn is_valid_policy_part2(p: &Policy) -> bool {
|
fn is_valid_policy_part2(p: &Policy) -> bool {
|
||||||
let letter = Some(p.letter.as_str());
|
let letter = Some(p.letter.as_str());
|
||||||
// Password system uses it 1 based numbering, so we -1 to get zero based.
|
// Password system uses it 1 based numbering, so we -1 to get zero based.
|
||||||
@ -97,9 +134,7 @@ mod tests {
|
|||||||
"#;
|
"#;
|
||||||
#[test]
|
#[test]
|
||||||
fn parse_policies() {
|
fn parse_policies() {
|
||||||
assert_eq!(
|
let want = vec![
|
||||||
parse(INPUT),
|
|
||||||
vec![
|
|
||||||
Policy {
|
Policy {
|
||||||
min: 1,
|
min: 1,
|
||||||
max: 3,
|
max: 3,
|
||||||
@ -118,17 +153,18 @@ mod tests {
|
|||||||
letter: "c".to_string(),
|
letter: "c".to_string(),
|
||||||
password: "ccccccccc".to_string(),
|
password: "ccccccccc".to_string(),
|
||||||
},
|
},
|
||||||
]
|
];
|
||||||
);
|
assert_eq!(parse_regex(INPUT), want);
|
||||||
|
assert_eq!(parse_handrolled(INPUT), want);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn validate_count_part1() {
|
fn validate_count_part1() {
|
||||||
assert_eq!(valid_policy_count_part1(&parse(INPUT)), 2);
|
assert_eq!(valid_policy_count_part1(&parse_regex(INPUT)), 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn validate_count_part2() {
|
fn validate_count_part2() {
|
||||||
assert_eq!(valid_policy_count_part2(&parse(INPUT)), 1);
|
assert_eq!(valid_policy_count_part2(&parse_regex(INPUT)), 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user