Compare commits
2 Commits
23e3872453
...
66c39108ce
| Author | SHA1 | Date | |
|---|---|---|---|
| 66c39108ce | |||
| 5d5d24168c |
@@ -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
|
||||
|
||||
|
||||
33
src/common/timer.h
Normal file
33
src/common/timer.h
Normal 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
|
||||
@@ -61,92 +61,61 @@
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <fstream>
|
||||
#include <expected>
|
||||
#include <memory>
|
||||
#include "timer.h"
|
||||
|
||||
struct instruction {
|
||||
// Left = false
|
||||
// Right = true
|
||||
bool dir;
|
||||
int number;
|
||||
};
|
||||
|
||||
auto get_dir(const std::string& line) -> std::expected<bool, std::string> {
|
||||
bool get_dir(const std::string& line) {
|
||||
if (line.find('R') != std::string::npos) {
|
||||
return true;
|
||||
} else if (line.find('L') != std::string::npos) {
|
||||
return false;
|
||||
} 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());
|
||||
int ret;
|
||||
try {
|
||||
ret = std::stoi(number);
|
||||
} 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) {
|
||||
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;
|
||||
}
|
||||
|
||||
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) {
|
||||
if (direction) { return (state + 100 + number) % 100; } else { return (state + 100 - number) % 100; }
|
||||
}
|
||||
|
||||
int main() {
|
||||
std::vector<instruction> instructions;
|
||||
if (auto temp = parse("input/day1/p1.txt")) {
|
||||
instructions = temp.value();
|
||||
} else {
|
||||
std::cerr << temp.error() << std::endl;
|
||||
}
|
||||
Timer timer;
|
||||
|
||||
std::ifstream stream("input/day1/p1.txt");
|
||||
std::string line;
|
||||
int state = 50;
|
||||
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) {
|
||||
password++;
|
||||
}
|
||||
}
|
||||
|
||||
timer.stop();
|
||||
|
||||
std::cout << "Password: " << password << std::endl;
|
||||
|
||||
timer.print();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -94,70 +94,36 @@
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <fstream>
|
||||
#include <expected>
|
||||
#include <chrono>
|
||||
#include "timer.h"
|
||||
|
||||
struct instruction {
|
||||
// Left = false
|
||||
// Right = true
|
||||
bool dir;
|
||||
int number;
|
||||
};
|
||||
|
||||
auto get_dir(const std::string& line) -> std::expected<bool, std::string> {
|
||||
bool get_dir(const std::string& line) {
|
||||
if (line.find('R') != std::string::npos) {
|
||||
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());
|
||||
int ret;
|
||||
try {
|
||||
ret = std::stoi(number);
|
||||
} 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) {
|
||||
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;
|
||||
}
|
||||
|
||||
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) {
|
||||
int new_password = password + (number / 100);
|
||||
const int remainder = number % 100;
|
||||
@@ -174,22 +140,24 @@ auto parse(const std::string& filename) -> std::expected<std::vector<instruction
|
||||
}
|
||||
|
||||
int main() {
|
||||
std::vector<instruction> instructions;
|
||||
if (auto temp = parse("input/day1/p1.txt")) {
|
||||
instructions = temp.value();
|
||||
} else {
|
||||
std::cerr << temp.error() << std::endl;
|
||||
}
|
||||
Timer timer;
|
||||
|
||||
std::ifstream stream("input/day1/p1.txt");
|
||||
std::string line;
|
||||
int state = 50;
|
||||
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);
|
||||
password = std::get<1>(temp);
|
||||
}
|
||||
|
||||
timer.stop();
|
||||
|
||||
std::cout << "Password: " << password << std::endl;
|
||||
|
||||
timer.print();
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user