Created
January 21, 2026 18:18
-
-
Save icub3d/8e0a4d46e2aa526da8890d4a90faf3e5 to your computer and use it in GitHub Desktop.
Solution for Advent of Code 2017 Day 4
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| use std::time::Instant; | |
| use rustc_hash::FxHashSet; | |
| const INPUT: &str = include_str!("inputs/day04.txt"); | |
| fn parse(input: &str) -> impl Iterator<Item = Vec<&str>> { | |
| input.lines().map(|l| l.split_whitespace().collect()) | |
| } | |
| fn p1(input: &str) -> usize { | |
| parse(input) | |
| .filter(|items| { | |
| // Passphrase is OK if all items are "newly inserted". | |
| let mut set = FxHashSet::default(); | |
| items.iter().all(|i| set.insert(*i)) | |
| }) | |
| .count() | |
| } | |
| fn p2(input: &str) -> usize { | |
| parse(input) | |
| .filter(|items| { | |
| // Passphrase is OK if all items are "newly inserted". | |
| let mut set = FxHashSet::default(); | |
| items.iter().all(|i| { | |
| let mut v = i.chars().collect::<Vec<_>>(); | |
| v.sort(); | |
| set.insert(v) | |
| }) | |
| }) | |
| .count() | |
| } | |
| fn main() { | |
| let now = Instant::now(); | |
| let solution = p1(INPUT); | |
| println!("p1 {:?} {}", now.elapsed(), solution); | |
| let now = Instant::now(); | |
| let solution = p2(INPUT); | |
| println!("p2 {:?} {}", now.elapsed(), solution); | |
| } | |
| #[cfg(test)] | |
| mod tests { | |
| use super::*; | |
| #[test] | |
| fn test_p1() { | |
| let input = "aa bb cc dd ee\naa bb cc dd aa\naa bb cc dd aaa"; | |
| assert_eq!(p1(input), 2); | |
| } | |
| #[test] | |
| fn test_p2() { | |
| let input = "abcde fghij\nabcde xyz ecdab"; | |
| assert_eq!(p2(input), 1); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment