diff --git a/CMakeLists.txt b/CMakeLists.txt index 8d6e0db..cf7ce2a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -22,6 +22,7 @@ ENDIF() ### Copy the input files dir to build file(COPY ${INPUT_DIR} DESTINATION "${CMAKE_CURRENT_BINARY_DIR}") +include_directories(src/common) ### EXECUTABLES diff --git a/src/common/timer.h b/src/common/timer.h new file mode 100644 index 0000000..5ba0956 --- /dev/null +++ b/src/common/timer.h @@ -0,0 +1,33 @@ +#ifndef ADVENTOFCODE2025_TIMER_H +#define ADVENTOFCODE2025_TIMER_H + +#include +#include + +class Timer { +public: + Timer() { + running = true; + start = std::chrono::high_resolution_clock::now(); + } + + void stop() { + end = std::chrono::high_resolution_clock::now(); + running = false; + }; + + void print () { + if (running) { stop(); }; + const auto seconds = std::chrono::duration_cast(end - start); + const auto ms = std::chrono::duration_cast(end - start); + const auto us = std::chrono::duration_cast(end - start); + std::cout << "Execution time: \n\t" << seconds.count() << " s\n\t" << ms.count() << " ms\n\t" << us.count() << " us" << std::endl; + } + +private: + bool running = false; + std::chrono::time_point start; + std::chrono::time_point end; +}; + +#endif //ADVENTOFCODE2025_TIMER_H \ No newline at end of file diff --git a/src/day1/p1/main.cpp b/src/day1/p1/main.cpp index 56766cd..998e0b1 100644 --- a/src/day1/p1/main.cpp +++ b/src/day1/p1/main.cpp @@ -63,6 +63,7 @@ #include #include #include +#include "timer.h" bool get_dir(const std::string& line) { if (line.find('R') != std::string::npos) { @@ -96,6 +97,8 @@ int rotate (const int state, const bool direction, const int number) { } int main() { + Timer timer; + std::ifstream stream("input/day1/p1.txt"); std::string line; int state = 50; @@ -108,7 +111,11 @@ int main() { } } + timer.stop(); + std::cout << "Password: " << password << std::endl; + timer.print(); + return 0; } diff --git a/src/day1/p2/main.cpp b/src/day1/p2/main.cpp index 3509c6f..a93864d 100644 --- a/src/day1/p2/main.cpp +++ b/src/day1/p2/main.cpp @@ -95,8 +95,8 @@ #include #include #include -#include - +#include +#include "timer.h" bool get_dir(const std::string& line) { if (line.find('R') != std::string::npos) { @@ -140,6 +140,8 @@ int get_number(const std::string& line) { } int main() { + Timer timer; + std::ifstream stream("input/day1/p1.txt"); std::string line; int state = 50; @@ -151,7 +153,11 @@ int main() { password = std::get<1>(temp); } + timer.stop(); + std::cout << "Password: " << password << std::endl; + timer.print(); + return 0; } \ No newline at end of file