day 3 p1
This commit is contained in:
109
2025/Cpp/Day03/Day03.cpp
Normal file
109
2025/Cpp/Day03/Day03.cpp
Normal file
@@ -0,0 +1,109 @@
|
||||
#include "AdventHelpers/AdventOfCodeSolution.h"
|
||||
|
||||
#include <iostream>
|
||||
#include <cassert>
|
||||
#include <string>
|
||||
#include <cstring>
|
||||
#include <sstream>
|
||||
#include <unordered_map>
|
||||
|
||||
class BatteryBank {
|
||||
public:
|
||||
using joltage_t = uint64_t;
|
||||
using bank_t = std::vector<uint8_t>;
|
||||
|
||||
BatteryBank(std::string bankinput)
|
||||
{
|
||||
mBank.reserve(bankinput.length());
|
||||
|
||||
for (const auto& c : bankinput) {
|
||||
const int value = c - '0';
|
||||
|
||||
mBankCounts[value]++;
|
||||
mBank.push_back(value);
|
||||
}
|
||||
|
||||
mJoltage = calculateJoltage();
|
||||
}
|
||||
|
||||
inline std::vector<uint8_t> bank() const { return mBank; }
|
||||
inline joltage_t joltage() const { return mJoltage; }
|
||||
|
||||
void printMd() const {
|
||||
for (int i = 0; i < mBank.size(); i++) {
|
||||
if (i == mLeftIndex || i == mRightIndex) {
|
||||
std::cout << "**" << std::to_string(mBank[i]) << "**";
|
||||
}
|
||||
else {
|
||||
std::cout << std::to_string(mBank[i]);
|
||||
}
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
|
||||
private:
|
||||
joltage_t calculateJoltage() {
|
||||
// Find the max value, excluding the last
|
||||
const auto maxIter = std::max_element(mBank.begin(), mBank.end()-1);
|
||||
const auto maxVal = *maxIter;
|
||||
|
||||
// Find the farthest left that's the max
|
||||
auto leftIter = mBank.begin();
|
||||
while (*leftIter != maxVal) leftIter++;
|
||||
|
||||
// Roll right and figure out what's the next biggest and send that
|
||||
const auto rightIter = std::max_element(leftIter + 1, mBank.end());
|
||||
const auto rightVal = *rightIter;
|
||||
|
||||
mLeftIndex = leftIter - mBank.begin();
|
||||
mRightIndex = rightIter - mBank.begin();
|
||||
|
||||
return (maxVal * 10) + rightVal;
|
||||
}
|
||||
|
||||
joltage_t calculateJoltageWithFriction() {
|
||||
|
||||
|
||||
}
|
||||
|
||||
std::unordered_map<int, int> mBankCounts;
|
||||
bank_t mBank;
|
||||
joltage_t mJoltage;
|
||||
size_t mLeftIndex, mRightIndex;
|
||||
std::vector<bool> mEnabledBatteries;
|
||||
};
|
||||
|
||||
class Day03Solution : public AdventHelpers::AdventOfCodeSolution
|
||||
{
|
||||
public:
|
||||
|
||||
|
||||
virtual std::string SolveProblem01(AdventHelpers::InputFileHelper& inputFile) override {
|
||||
std::vector<BatteryBank> batteries;
|
||||
unsigned int joltage = 0;
|
||||
|
||||
for (const auto& line : inputFile) {
|
||||
batteries.push_back(line);
|
||||
}
|
||||
|
||||
for (const auto& battery : batteries) {
|
||||
battery.printMd();
|
||||
joltage += battery.joltage();
|
||||
}
|
||||
|
||||
return std::to_string(joltage);
|
||||
}
|
||||
|
||||
virtual std::string SolveProblem02(AdventHelpers::InputFileHelper& inputFile) override {
|
||||
return "todo";
|
||||
}
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
Day03Solution solution;
|
||||
solution.SolveAll();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user