aoc25/day1/main.cpp
2025-12-03 21:28:53 +00:00

111 lines
3 KiB
C++

#include <iostream>
#include <fstream>
#include <vector>
#include <expected>
#include <print>
struct DialRotation {
char direction;
int distance;
};
auto parseRotations(const std::string& filename) -> std::expected<std::vector<DialRotation>, std::string> {
std::ifstream inputF {filename};
if (!inputF){
return std::unexpected{"Some file open error.\n"};
}
std::vector<DialRotation> dialRotations{};
DialRotation curDial{};
while ( inputF >> curDial.direction >> curDial.distance)
{
//std::println("{} : {}", curDial.direction, curDial.distance);
dialRotations.push_back(curDial);
}
return std::move(dialRotations);
}
constexpr int dialStart = 50;
constexpr int maxDial = 100 ;
auto executeSafeCrack(const std::vector<DialRotation>& dialRotations){
int dial {dialStart};
std::cout << "The dial starts by pointing at " << dial << "\n";
int countZero = 0;
for (const auto& curDial : dialRotations){
int fullRotations = curDial.distance / maxDial;
int modulo = curDial.distance % maxDial;
if (fullRotations > 0){
std::cout << "Big number! Passed " << "" << fullRotations << " times through 0!\n";
countZero += fullRotations;
}
if(curDial.direction == 'L'){
int prevDial = dial;
dial -= modulo;
if (dial < 0){
dial += maxDial;
if (prevDial != 0) {
std::cout << "Passed through 0 from a L rotation! Count++!\n";
countZero++;
}
}
if (dial == 0){
std::cout << "Stopped at 0! Count++!\n";
countZero++;
}
}
else{
dial += modulo;
if (dial >= maxDial){
dial -= maxDial;
countZero++;
std::cout << "Passed through 0 from a R rotation! Count++!\n";
}
}
std::println("{} : {:3}, Dial now at: {:5} cnt: {}", curDial.direction, curDial.distance, dial, countZero);
if (dial < 0){
std::cout << "Shit's fucked!\n";
break;
}
if (dial >= maxDial){
std::cout << "Shit's fucked!\n";
break;
}
}
std::print("Zeros: {}\n", countZero);
return countZero;
}
auto main(int, char**) -> int {
auto retval = parseRotations("day1_input");
std::vector<DialRotation> testCase = {
{'L', 68},
{'L', 30},
{'R', 48},
{'L', 5},
{'R', 60},
{'L', 55},
{'L', 1},
{'L', 99},
{'R', 14},
{'L', 82},
{'L', 1000},
};
if(retval) {
auto realResult = executeSafeCrack(*retval);
auto result = executeSafeCrack(testCase);
std::println("Testcase result: {}", result);
std::println("Total result: {}", realResult);
}
else{
std::print("{}\n", retval.error());
}
return 0;
}