From a4dda171b5f81f900e4d617b08ae8c0e2d374e37 Mon Sep 17 00:00:00 2001 From: Sijmen Schoon Date: Sat, 26 Dec 2020 00:19:07 +0100 Subject: [PATCH] rust(day06): yee --- rust/src/day06.rs | 41 +++++++++++++++++++++++++++++++++++++++++ rust/src/main.rs | 2 +- 2 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 rust/src/day06.rs diff --git a/rust/src/day06.rs b/rust/src/day06.rs new file mode 100644 index 0000000..45dd10b --- /dev/null +++ b/rust/src/day06.rs @@ -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() +} diff --git a/rust/src/main.rs b/rust/src/main.rs index efad8b2..896ce08 100644 --- a/rust/src/main.rs +++ b/rust/src/main.rs @@ -7,7 +7,7 @@ mod day02; mod day03; mod day04; mod day05; -use nop as day06; +mod day06; use nop as day07; mod day08; mod day09;