Cleanup - removing the overhead that my rust brain wants to add.
This commit is contained in:
@@ -61,86 +61,48 @@
|
|||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <expected>
|
#include <memory>
|
||||||
|
|
||||||
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;
|
std::ifstream stream("input/day1/p1.txt");
|
||||||
if (auto temp = parse("input/day1/p1.txt")) {
|
std::string line;
|
||||||
instructions = temp.value();
|
|
||||||
} else {
|
|
||||||
std::cerr << temp.error() << std::endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
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++;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -94,70 +94,36 @@
|
|||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
|
||||||
#include <fstream>
|
#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) {
|
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,17 +140,13 @@ auto parse(const std::string& filename) -> std::expected<std::vector<instruction
|
|||||||
}
|
}
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
std::vector<instruction> instructions;
|
std::ifstream stream("input/day1/p1.txt");
|
||||||
if (auto temp = parse("input/day1/p1.txt")) {
|
std::string line;
|
||||||
instructions = temp.value();
|
|
||||||
} else {
|
|
||||||
std::cerr << temp.error() << std::endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
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);
|
||||||
}
|
}
|
||||||
@@ -192,4 +154,4 @@ int main() {
|
|||||||
std::cout << "Password: " << password << std::endl;
|
std::cout << "Password: " << password << std::endl;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user