Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| bde96d0efa | |||
| 330f90181f |
@ -43,54 +43,47 @@ struct Policy {
|
|||||||
password: String,
|
password: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[aoc_generator(day2)]
|
fn parse_regex<'a>(input: &'a str) -> impl Iterator<Item = Policy> + 'a {
|
||||||
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')
|
||||||
.filter(|line| re.is_match(line))
|
.filter_map(move |line| match re.captures(line) {
|
||||||
.map(|line| {
|
Some(caps) => Some(Policy {
|
||||||
let caps = re.captures(line).expect("Failed to match pattern");
|
|
||||||
Policy {
|
|
||||||
min: caps.get(1).unwrap().as_str().parse().unwrap(),
|
min: caps.get(1).unwrap().as_str().parse().unwrap(),
|
||||||
max: caps.get(2).unwrap().as_str().parse().unwrap(),
|
max: caps.get(2).unwrap().as_str().parse().unwrap(),
|
||||||
letter: caps.get(3).unwrap().as_str().to_string(),
|
letter: caps.get(3).unwrap().as_str().to_string(),
|
||||||
password: caps.get(4).unwrap().as_str().to_string(),
|
password: caps.get(4).unwrap().as_str().to_string(),
|
||||||
}
|
}),
|
||||||
|
None => None,
|
||||||
})
|
})
|
||||||
.collect()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[aoc_generator(day2, part1, handrolled)]
|
fn parse_handrolled<'a>(input: &'a str) -> impl Iterator<Item = Policy> + 'a {
|
||||||
fn parse_handrolled(input: &str) -> Vec<Policy> {
|
|
||||||
// Example line:
|
// Example line:
|
||||||
// 1-3 a: abcde
|
// 1-3 a: abcde
|
||||||
input
|
input.split('\n').filter_map(|line| {
|
||||||
.split('\n')
|
let start = 0;
|
||||||
.filter_map(|line| {
|
let end = line.find('-')?;
|
||||||
let start = 0;
|
let min: usize = line[start..end].parse().ok()?;
|
||||||
let end = line.find('-')?;
|
|
||||||
let min: usize = line[start..end].parse().ok()?;
|
|
||||||
|
|
||||||
let start = end + 1;
|
let start = end + 1;
|
||||||
let end = line.find(' ')?;
|
let end = line.find(' ')?;
|
||||||
let max: usize = line[start..end].parse().ok()?;
|
let max: usize = line[start..end].parse().ok()?;
|
||||||
|
|
||||||
let start = end + 1;
|
let start = end + 1;
|
||||||
let end = line.find(':')?;
|
let end = line.find(':')?;
|
||||||
let letter = line[start..end].to_string();
|
let letter = line[start..end].to_string();
|
||||||
|
|
||||||
let start = end + 2;
|
let start = end + 2;
|
||||||
let password = line[start..].to_string();
|
let password = line[start..].to_string();
|
||||||
|
|
||||||
Some(Policy {
|
Some(Policy {
|
||||||
min,
|
min,
|
||||||
max,
|
max,
|
||||||
letter,
|
letter,
|
||||||
password,
|
password,
|
||||||
})
|
|
||||||
})
|
})
|
||||||
.collect()
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn is_valid_policy_part1(p: &Policy) -> bool {
|
fn is_valid_policy_part1(p: &Policy) -> bool {
|
||||||
@ -99,13 +92,15 @@ fn is_valid_policy_part1(p: &Policy) -> bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[aoc(day2, part1)]
|
#[aoc(day2, part1)]
|
||||||
fn valid_policy_count_part1(policies: &[Policy]) -> usize {
|
fn valid_policy_count_part1(input: &str) -> usize {
|
||||||
policies.iter().filter(|p| is_valid_policy_part1(p)).count()
|
let policies = parse_regex(input);
|
||||||
|
policies.filter(|p| is_valid_policy_part1(p)).count()
|
||||||
}
|
}
|
||||||
|
|
||||||
#[aoc(day2, part1, handrolled)]
|
#[aoc(day2, part1, handrolled)]
|
||||||
fn valid_policy_count_handrolled_part1(policies: &[Policy]) -> usize {
|
fn valid_policy_count_handrolled_part1(input: &str) -> usize {
|
||||||
policies.iter().filter(|p| is_valid_policy_part1(p)).count()
|
let policies = parse_handrolled(input);
|
||||||
|
policies.filter(|p| is_valid_policy_part1(p)).count()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn is_valid_policy_part2(p: &Policy) -> bool {
|
fn is_valid_policy_part2(p: &Policy) -> bool {
|
||||||
@ -119,8 +114,9 @@ fn is_valid_policy_part2(p: &Policy) -> bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[aoc(day2, part2)]
|
#[aoc(day2, part2)]
|
||||||
fn valid_policy_count_part2(policies: &[Policy]) -> usize {
|
fn valid_policy_count_part2(input: &str) -> usize {
|
||||||
policies.iter().filter(|p| is_valid_policy_part2(p)).count()
|
let policies = parse_regex(input);
|
||||||
|
policies.filter(|p| is_valid_policy_part2(p)).count()
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
@ -154,17 +150,17 @@ mod tests {
|
|||||||
password: "ccccccccc".to_string(),
|
password: "ccccccccc".to_string(),
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
assert_eq!(parse_regex(INPUT), want);
|
assert_eq!(parse_regex(INPUT).collect::<Vec<Policy>>(), want);
|
||||||
assert_eq!(parse_handrolled(INPUT), want);
|
assert_eq!(parse_handrolled(INPUT).collect::<Vec<Policy>>(), want);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn validate_count_part1() {
|
fn validate_count_part1() {
|
||||||
assert_eq!(valid_policy_count_part1(&parse_regex(INPUT)), 2);
|
assert_eq!(valid_policy_count_part1(INPUT), 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn validate_count_part2() {
|
fn validate_count_part2() {
|
||||||
assert_eq!(valid_policy_count_part2(&parse_regex(INPUT)), 1);
|
assert_eq!(valid_policy_count_part2(INPUT), 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user