Cleanup - removing the overhead that my rust brain wants to add.
This commit is contained in:
@@ -61,86 +61,48 @@
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <fstream>
|
||||
#include <expected>
|
||||
#include <memory>
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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++;
|
||||
}
|
||||
|
||||
@@ -94,70 +94,36 @@
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <fstream>
|
||||
#include <expected>
|
||||
#include <vector>
|
||||
|
||||
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,17 +140,13 @@ 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;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user