48 lines
1.3 KiB
Rust
48 lines
1.3 KiB
Rust
|
use std::time::{Duration, Instant};
|
||
|
|
||
|
pub fn run(print: bool) -> Duration {
|
||
|
let file_string = std::fs::read_to_string("inputs/day02").unwrap();
|
||
|
let instant = Instant::now();
|
||
|
|
||
|
let rules: Vec<(usize, usize, char, &str)> = file_string
|
||
|
.lines()
|
||
|
.flat_map(|rule| {
|
||
|
let mut splitc = rule.splitn(2, ' ');
|
||
|
let mut split_range = splitc.next()?.split('-');
|
||
|
|
||
|
let min: usize = split_range.next()?.parse().ok()?;
|
||
|
let max: usize = split_range.next()?.parse().ok()?;
|
||
|
|
||
|
let rest = splitc.next()?;
|
||
|
let c = rest.chars().next()?;
|
||
|
let pw = &rest[3..];
|
||
|
|
||
|
Some((min, max, c, pw))
|
||
|
})
|
||
|
.collect();
|
||
|
|
||
|
let part1 = rules
|
||
|
.iter()
|
||
|
.filter(|(min, max, c, pw)| {
|
||
|
let c = &pw.chars().filter(|n| n == c).count();
|
||
|
min <= c && c <= max
|
||
|
})
|
||
|
.count();
|
||
|
|
||
|
let part2 = rules
|
||
|
.iter()
|
||
|
.filter(|(min, max, c, pw)| {
|
||
|
let mut chars = pw.chars();
|
||
|
let a = chars.nth(min - 1) == Some(*c);
|
||
|
let b = chars.nth(max - min - 1) == Some(*c);
|
||
|
a ^ b
|
||
|
})
|
||
|
.count();
|
||
|
|
||
|
let elapsed = instant.elapsed();
|
||
|
if print {
|
||
|
dbg!(elapsed, part1, part2);
|
||
|
}
|
||
|
elapsed
|
||
|
}
|