Compare commits

...

2 Commits

Author SHA1 Message Date
66c39108ce Add timer.h for benchmarking 2025-12-01 03:28:00 -05:00
5d5d24168c Cleanup - removing the overhead that my rust brain wants to add. 2025-12-01 03:09:53 -05:00
4 changed files with 78 additions and 107 deletions

View File

@@ -22,6 +22,7 @@ ENDIF()
### Copy the input files dir to build ### Copy the input files dir to build
file(COPY ${INPUT_DIR} DESTINATION "${CMAKE_CURRENT_BINARY_DIR}") file(COPY ${INPUT_DIR} DESTINATION "${CMAKE_CURRENT_BINARY_DIR}")
include_directories(src/common)
### EXECUTABLES ### EXECUTABLES

33
src/common/timer.h Normal file
View File

@@ -0,0 +1,33 @@
#ifndef ADVENTOFCODE2025_TIMER_H
#define ADVENTOFCODE2025_TIMER_H
#include <chrono>
#include <iostream>
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<std::chrono::seconds>(end - start);
const auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
const auto us = std::chrono::duration_cast<std::chrono::microseconds>(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<std::chrono::system_clock> start;
std::chrono::time_point<std::chrono::system_clock> end;
};
#endif //ADVENTOFCODE2025_TIMER_H

View File

@@ -61,92 +61,61 @@
#include <iostream> #include <iostream>
#include <string> #include <string>
#include <vector>
#include <fstream> #include <fstream>
#include <expected> #include <memory>
#include "timer.h"
struct instruction { bool get_dir(const std::string& line) {
// Left = false
// Right = true
bool dir;
int number;
};
auto get_dir(const std::string& line) -> std::expected<bool, std::string> {
if (line.find('R') != std::string::npos) { if (line.find('R') != std::string::npos) {
return true; return true;
} else if (line.find('L') != std::string::npos) { } else if (line.find('L') != std::string::npos) {
return false; return false;
} else { } else {
return std::unexpected("ERROR: Failed to read direction | " + line); std::cerr << "ERROR: Failed to read direction | " << line << std::endl;
exit(1);
} }
} }
auto get_number(const std::string& line) -> std::expected<int, std::string> { int get_number(const std::string& line) {
const std::string number = line.substr(1, line.size()); const std::string number = line.substr(1, line.size());
int ret; int ret;
try { try {
ret = std::stoi(number); ret = std::stoi(number);
} catch (const std::invalid_argument& e) { } catch (const std::invalid_argument& e) {
return std::unexpected("ERROR: Number was an invalid argument | " + number + " | " + e.what()); std::cerr << "ERROR: Number was an invalid argument | " << number << " | " << e.what() << std::endl;
exit(1);
} catch (const std::out_of_range& e) { } catch (const std::out_of_range& e) {
return std::unexpected("ERROR: Number out of range | " + number + " | " + e.what()); std::cerr << "ERROR: Number was out of range | " << number << " | " << e.what() << std::endl;
exit(1);
} }
return ret; return ret;
} }
auto parse_line(const std::string& line) -> std::expected<instruction, std::string> {
instruction ins{};
if (const auto dir = get_dir(line); dir.has_value()) {
ins.dir = dir.value();
} else {
return std::unexpected(dir.error());
}
if (const auto number = get_number(line); number.has_value()) {
ins.number = number.value();
} else {
return std::unexpected(number.error());
}
return ins;
}
auto parse(const std::string& filename) -> std::expected<std::vector<instruction>, std::string> {
std::ifstream stream(filename);
std::string line;
std::vector<instruction> instructions;
while (std::getline(stream, line)) {
if (const auto temp = parse_line(line); temp.has_value()) {
instructions.push_back(temp.value());
} else {
return std::unexpected("Failed to parse file | " + filename + " | " + temp.error());
}
}
return instructions;
}
int rotate (const int state, const bool direction, const int number) { int rotate (const int state, const bool direction, const int number) {
if (direction) { return (state + 100 + number) % 100; } else { return (state + 100 - number) % 100; } if (direction) { return (state + 100 + number) % 100; } else { return (state + 100 - number) % 100; }
} }
int main() { int main() {
std::vector<instruction> instructions; Timer timer;
if (auto temp = parse("input/day1/p1.txt")) {
instructions = temp.value();
} else {
std::cerr << temp.error() << std::endl;
}
std::ifstream stream("input/day1/p1.txt");
std::string line;
int state = 50; int state = 50;
int password = 0; int password = 0;
for (auto [dir, number] : instructions) {
state = rotate(state, dir, number); while (std::getline(stream, line)) {
state = rotate(state, get_dir(line), get_number(line));
if (state == 0) { if (state == 0) {
password++; password++;
} }
} }
timer.stop();
std::cout << "Password: " << password << std::endl; std::cout << "Password: " << password << std::endl;
timer.print();
return 0; return 0;
} }

View File

@@ -94,70 +94,36 @@
#include <iostream> #include <iostream>
#include <string> #include <string>
#include <vector>
#include <fstream> #include <fstream>
#include <expected> #include <chrono>
#include "timer.h"
struct instruction { bool get_dir(const std::string& line) {
// Left = false
// Right = true
bool dir;
int number;
};
auto get_dir(const std::string& line) -> std::expected<bool, std::string> {
if (line.find('R') != std::string::npos) { if (line.find('R') != std::string::npos) {
return true; return true;
} else if (line.find('L') != std::string::npos) {
return false;
} else {
return std::unexpected("ERROR: Failed to read direction | " + line);
} }
if (line.find('L') != std::string::npos) {
return false;
}
std::cerr << "ERROR: Failed to read direction | " << line << std::endl;
exit(1);
} }
auto get_number(const std::string& line) -> std::expected<int, std::string> { int get_number(const std::string& line) {
const std::string number = line.substr(1, line.size()); const std::string number = line.substr(1, line.size());
int ret; int ret;
try { try {
ret = std::stoi(number); ret = std::stoi(number);
} catch (const std::invalid_argument& e) { } catch (const std::invalid_argument& e) {
return std::unexpected("ERROR: Number was an invalid argument | " + number + " | " + e.what()); std::cerr << "ERROR: Number was an invalid argument | " << number << " | " << e.what() << std::endl;
exit(1);
} catch (const std::out_of_range& e) { } catch (const std::out_of_range& e) {
return std::unexpected("ERROR: Number out of range | " + number + " | " + e.what()); std::cerr << "ERROR: Number was out of range | " << number << " | " << e.what() << std::endl;
exit(1);
} }
return ret; return ret;
} }
auto parse_line(const std::string& line) -> std::expected<instruction, std::string> {
instruction ins{};
if (const auto dir = get_dir(line); dir.has_value()) {
ins.dir = dir.value();
} else {
return std::unexpected(dir.error());
}
if (const auto number = get_number(line); number.has_value()) {
ins.number = number.value();
} else {
return std::unexpected(number.error());
}
return ins;
}
auto parse(const std::string& filename) -> std::expected<std::vector<instruction>, std::string> {
std::ifstream stream(filename);
std::string line;
std::vector<instruction> instructions;
while (std::getline(stream, line)) {
if (const auto temp = parse_line(line); temp.has_value()) {
instructions.push_back(temp.value());
} else {
return std::unexpected("Failed to parse file | " + filename + " | " + temp.error());
}
}
return instructions;
}
std::tuple<int, int> rotate (const int state, const bool direction, const int number, const int password) { std::tuple<int, int> rotate (const int state, const bool direction, const int number, const int password) {
int new_password = password + (number / 100); int new_password = password + (number / 100);
const int remainder = number % 100; const int remainder = number % 100;
@@ -174,22 +140,24 @@ auto parse(const std::string& filename) -> std::expected<std::vector<instruction
} }
int main() { int main() {
std::vector<instruction> instructions; Timer timer;
if (auto temp = parse("input/day1/p1.txt")) {
instructions = temp.value();
} else {
std::cerr << temp.error() << std::endl;
}
std::ifstream stream("input/day1/p1.txt");
std::string line;
int state = 50; int state = 50;
int password = 0; int password = 0;
for (auto [dir, number] : instructions) {
std::tuple<int, int> temp = rotate(state, dir, number, password); while (std::getline(stream, line)) {
std::tuple<int, int> temp = rotate(state, get_dir(line), get_number(line), password);
state = std::get<0>(temp); state = std::get<0>(temp);
password = std::get<1>(temp); password = std::get<1>(temp);
} }
timer.stop();
std::cout << "Password: " << password << std::endl; std::cout << "Password: " << password << std::endl;
timer.print();
return 0; return 0;
} }