rust(day06): yee

This commit is contained in:
Sijmen 2020-12-26 00:19:07 +01:00
parent a44bc3ea57
commit a4dda171b5
Signed by: vijfhoek
GPG Key ID: DAF7821E067D9C48
2 changed files with 42 additions and 1 deletions

41
rust/src/day06.rs Normal file
View File

@ -0,0 +1,41 @@
use std::time::{Duration, Instant};
fn count_bits(i: u32) -> u32 {
let i = i - ((i >> 1) & 0x55555555);
let i = (i & 0x33333333) + ((i >> 2) & 0x33333333);
(((i + (i >> 4)) & 0x0F0F0F0F) * 0x01010101) >> 24
}
pub fn run(print: bool) -> Duration {
let file_string = std::fs::read_to_string("../inputs/06").unwrap();
let instant = Instant::now();
let mut part1 = 0;
let mut part2 = 0;
for group in file_string.trim().split("\n\n") {
let mut set = 0u32;
for c in group.chars().filter(|c| c.is_alphabetic()) {
set |= 1 << (c as usize - 'a' as usize);
}
part1 += count_bits(set);
let mut line_set = 0u32;
for c in group.chars() {
if c == '\n' {
set &= line_set;
line_set = 0;
} else {
line_set |= 1 << (c as usize - 'a' as usize);
}
}
set &= line_set;
part2 += count_bits(set);
}
if print {
dbg!(part1, part2);
}
instant.elapsed()
}

View File

@ -7,7 +7,7 @@ mod day02;
mod day03; mod day03;
mod day04; mod day04;
mod day05; mod day05;
use nop as day06; mod day06;
use nop as day07; use nop as day07;
mod day08; mod day08;
mod day09; mod day09;