From c2297ba8495a44b75ce6cf112bf72ee472637e68 Mon Sep 17 00:00:00 2001 From: Antoine Martin Date: Tue, 3 Dec 2019 17:00:25 +0100 Subject: [PATCH] 2015: day05: part 2 --- aoc2015/src/day05.rs | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/aoc2015/src/day05.rs b/aoc2015/src/day05.rs index e7a9d6e..c54e60d 100644 --- a/aoc2015/src/day05.rs +++ b/aoc2015/src/day05.rs @@ -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); + } }