Add points option to timer. Do iterations over solution to get averages
This commit is contained in:
@@ -66,43 +66,48 @@
|
||||
#include "timer.h"
|
||||
|
||||
int main() {
|
||||
Timer timer;
|
||||
|
||||
std::ifstream stream("input/day1/p1.txt", std::ios::binary | std::ios::ate);
|
||||
const auto size = stream.tellg();
|
||||
stream.seekg(0);
|
||||
std::vector<char> buffer(size);
|
||||
stream.read(buffer.data(), size);
|
||||
stream.close();
|
||||
|
||||
int state = 50;
|
||||
constexpr int iterations = 1000;
|
||||
Timer timer(iterations);
|
||||
int password = 0;
|
||||
|
||||
const char* ptr = buffer.data();
|
||||
const char* end = ptr + size;
|
||||
for (int i = 0; i < iterations; ++i) {
|
||||
std::ifstream stream("input/day1/p1.txt", std::ios::binary | std::ios::ate);
|
||||
const auto size = stream.tellg();
|
||||
stream.seekg(0);
|
||||
std::vector<char> buffer(size);
|
||||
stream.read(buffer.data(), size);
|
||||
|
||||
while (ptr < end) {
|
||||
const bool is_right = (*ptr == 'R');
|
||||
++ptr;
|
||||
int state = 50;
|
||||
|
||||
int number;
|
||||
auto [next, ec] = std::from_chars(ptr, end, number);
|
||||
ptr = next;
|
||||
|
||||
if (ptr < end && *ptr == '\r') ++ptr;
|
||||
if (ptr < end && *ptr == '\n') ++ptr;
|
||||
const char* ptr = buffer.data();
|
||||
const char* end = ptr + size;
|
||||
|
||||
if (is_right) {
|
||||
state = (state + number) % 100;
|
||||
} else {
|
||||
state = (state + 100 - number % 100) % 100;
|
||||
while (ptr < end) {
|
||||
const bool is_right = (*ptr == 'R');
|
||||
++ptr;
|
||||
|
||||
int number;
|
||||
auto [next, ec] = std::from_chars(ptr, end, number);
|
||||
ptr = next;
|
||||
|
||||
if (ptr < end && *ptr == '\r') ++ptr;
|
||||
if (ptr < end && *ptr == '\n') ++ptr;
|
||||
|
||||
if (is_right) {
|
||||
state = (state + number) % 100;
|
||||
} else {
|
||||
state = (state + 100 - number % 100) % 100;
|
||||
}
|
||||
|
||||
password += (state == 0);
|
||||
}
|
||||
|
||||
password += (state == 0);
|
||||
stream.close();
|
||||
}
|
||||
timer.stop();
|
||||
|
||||
std::cout << "Password: " << password << std::endl;
|
||||
std::cout << "Password: " << password / iterations << std::endl;
|
||||
|
||||
timer.print();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user