From f7f03278c811fa2a013a1b64150a184b59021692 Mon Sep 17 00:00:00 2001 From: Luke Hubmayer-Werner Date: Wed, 7 Dec 2022 17:08:58 +1030 Subject: [PATCH] port Rust to C++ --- 2022/day6.cpp | 70 +++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 65 insertions(+), 5 deletions(-) diff --git a/2022/day6.cpp b/2022/day6.cpp index 8861e0d..b977ed3 100644 --- a/2022/day6.cpp +++ b/2022/day6.cpp @@ -1,3 +1,4 @@ +#include #include #include #include @@ -24,6 +25,61 @@ size_t find_first_unique_run(std::vector *s, size_t num, size_t skip) { return 0; } + +std::pair find_first_unique_runs(std::vector *s) { + const size_t BATCH_SIZE = 8; + const size_t BATCH_NUM = 4096/BATCH_SIZE; + size_t four = 0; + size_t fourteen = 0; + uint32_t masks[4096]; + uint32_t scratch_masks[BATCH_SIZE]; + uint8_t scratch_masks_bits[BATCH_SIZE]; + for (int i=0; i<4096; i++) { + masks[i] = 1 << ((*s)[i]-'a'); + } + // Turn masks into masks2. Indices are now +1. + for (int i=0; i<4095; i++) { + masks[i] |= masks[i+1]; + } + for (int batch=1; batch(four+2, fourteen+8); +} + void run_once(std::vector *s) { size_t four = find_first_unique_run(s, 4, 4); size_t fourteen = find_first_unique_run(s, 14, four); @@ -32,11 +88,15 @@ void run_once(std::vector *s) { } void run_many(std::vector *s, size_t iterations) { - size_t four = 0; - size_t fourteen = 0; + size_t volatile four = 0; // Don't you dare optimize out the iterations! + size_t volatile fourteen = 0; + std::cout << "Running " << iterations << " iterations:\n"; for (size_t i = 0; i result = find_first_unique_runs(s); + four = result.first; + fourteen = result.second; } std::cout << "Part 1: " << four << '\n'; std::cout << "Part 2: " << fourteen << '\n'; @@ -51,5 +111,5 @@ int main() { input_file.read(reinterpret_cast(input.data()), eof_position); // run_once(&input); - run_many(&input, 1000000); + run_many(&input, 10000000); }