advent-of-code/2019/04/src/main.rs

26 lines
846 B
Rust
Raw Normal View History

2019-12-04 20:47:17 +01:00
#![feature(is_sorted)]
use itertools::Itertools;
pub fn main() {
2019-12-04 21:46:37 +01:00
let (lower, upper) = (172_930, 683_082);
2019-12-04 20:47:17 +01:00
let valid_part1 = (lower..upper)
.map(|n| n.to_string().chars().collect::<Vec<_>>())
.filter(|n| n.is_sorted())
.filter(|v| v.clone().iter().dedup().count() != v.len());
println!("Part 1: {}", valid_part1.clone().count());
2019-12-04 21:46:37 +01:00
2019-12-04 20:47:17 +01:00
let part2 = valid_part1
2019-12-04 21:46:37 +01:00
.filter(|vec| {
vec.iter()
.group_by(move |n| *n)
.into_iter()
.filter_map(|(_, v)| if v.count() == 2 { Some(true) } else { None })
// can’t do this because I’m too smol brained for the borrow checker
//.filter(|(_, v)| v.count() == 2)
.count()
> 0
2019-12-04 20:47:17 +01:00
})
.count();
println!("Part 2: {}", part2);
}