2015: day05: part 2

This commit is contained in:
Antoine Martin 2019-12-03 17:00:25 +01:00
parent b023546c8c
commit c2297ba849

View file

@ -4,6 +4,7 @@ const INPUT: &str = include_str!("../input/day05.txt");
pub fn run() -> Result<()> {
println!("part 1: {}", part1(INPUT));
println!("part 2: {}", part2(INPUT));
Ok(())
}
@ -43,6 +44,33 @@ fn part1(input: &str) -> usize {
.count()
}
fn part2(input: &str) -> usize {
input
.lines()
.map(|line| line.trim_end())
.filter(|line| {
for i in 0..(line.chars().count() - 3) {
let seq = &line[i..(i + 2)];
let line = &line[(i + 2)..];
if line.find(seq).is_some() {
return true;
}
}
false
})
.filter(|line| {
for i in 0..(line.chars().count() - 2) {
if line.chars().nth(i).unwrap() == line.chars().nth(i + 2).unwrap() {
return true;
}
}
false
})
.count()
}
#[cfg(test)]
mod tests {
use super::*;
@ -60,4 +88,17 @@ mod tests {
fn part1_real() {
assert_eq!(part1(INPUT), 258);
}
#[test]
fn part2_provided() {
assert_eq!(part2("qjhvhtzxzqqjkmpb"), 1);
assert_eq!(part2("xxyxx"), 1);
assert_eq!(part2("uurcxstgmygtbstg"), 0);
assert_eq!(part2("ieodomkazucvgmuy"), 0);
}
#[test]
fn part2_real() {
assert_eq!(part2(INPUT), 53);
}
}