Add Rust and C++ sliding XOR window versions of Day6

This commit is contained in:
Luke Hubmayer-Werner 2022-12-30 20:06:01 +10:30
parent 8bdb3ff89d
commit 3711935b4f
2 changed files with 71 additions and 2 deletions

View File

@ -82,6 +82,40 @@ fourteen_found:
return std::pair<size_t, size_t>(four+2, fourteen+8);
}
std::pair<size_t, size_t> find_first_unique_runs_xor(std::vector<uint8_t> *s) {
size_t four = 0;
size_t fourteen = 0;
uint32_t masks[4096];
for (int i=0; i<4096; i++) {
masks[i] = 1 << ((*s)[i]-'a');
}
uint32_t bitset = 0;
for (int i = 0; i < 4; i++) { // No old entries to remove
bitset ^= masks[i];
}
for (int i = 4; i < 4096; i++) {
bitset ^= masks[i];
bitset ^= masks[i-4];
if (std::popcount(bitset) >= 4) {
four = i;
break;
}
}
for (int i = four+1; i < four+11; i++) { // No old entries to remove while readjusting base
bitset ^= masks[i];
}
for (int i = four+11; i < 4096; i++) {
bitset ^= masks[i];
bitset ^= masks[i-14];
if (std::popcount(bitset) >= 14) {
fourteen = i;
break;
}
}
return std::pair<size_t, size_t>(four, fourteen);
}
int main(int argc, char **argv) {
size_t iterations = 1000000;
auto filename = "input/6";
@ -110,7 +144,8 @@ int main(int argc, char **argv) {
for (size_t i = 0; i<iterations; i++) {
// four = find_first_unique_run(&s, 4, 4);
// fourteen = find_first_unique_run(&s, 14, four);
auto result = find_first_unique_runs(&s);
// auto result = find_first_unique_runs(&s);
auto result = find_first_unique_runs_xor(&s);
four = result.first;
fourteen = result.second;
}

View File

@ -87,6 +87,39 @@ fn find_first_unique_runs(s: &Vec<u8>) -> (Int, Int) {
return (four+4, fourteen+8);
}
fn find_first_unique_runs_xor(s: &Vec<u8>) -> (Int, Int) {
let mut four: Int = 0;
let mut fourteen: Int = 0;
let mut masks: [u32; 4096] = [0; 4096];
for i in 0..4096 {
masks[i] = 1 << (s[i]-b'a');
}
let mut bitset: u32 = 0;
for i in 0..4 { // No old entries to remove
bitset ^= masks[i];
}
for i in 4..4096 {
bitset ^= masks[i];
bitset ^= masks[i-4];
if bitset.count_ones() >= 4 {
four = i;
break;
}
}
for i in (four+1)..(four+11) { // No old entries to remove while readjusting base
bitset ^= masks[i];
}
for i in (four+11)..4096 {
bitset ^= masks[i];
bitset ^= masks[i-14];
if bitset.count_ones() >= 14 {
fourteen = i;
break;
}
}
return (four, fourteen);
}
fn main() {
let mut filename = "input/6";
let mut iterations = 1_000_000;
@ -105,7 +138,8 @@ fn main() {
for _ in 0..iterations {
// four = find_first_unique_run(&s, 4, 0);
// fourteen = find_first_unique_run(&s, 14, four);
(four, fourteen) = find_first_unique_runs(&s);
// (four, fourteen) = find_first_unique_runs(&s);
(four, fourteen) = find_first_unique_runs_xor(&s);
}
let duration = t0.elapsed();
let per_iteration = duration / iterations;