This commit is contained in:
Feiko Wielsma 2025-12-04 11:58:32 +00:00
parent a4dd7b6e5b
commit 1fe4ecca8a
5 changed files with 313 additions and 0 deletions

106
day3/main.cpp Normal file
View file

@ -0,0 +1,106 @@
#include <iostream>
#include <fstream>
#include <iterator>
#include <string>
#include <string_view>
#include <vector>
#include <expected>
#include <print>
#include <ranges>
#include <array>
#include <map>
#include <numeric>
#include <functional>
#include <algorithm>
struct IDRange {
long leftRange;
long rightRange;
};
auto parseBanks(const std::string& filename) -> std::expected<std::vector<std::vector<int>>, std::string> {
std::ifstream inputF {filename};
if (!inputF){
return std::unexpected{"Some file open error.\n"};
}
std::vector<std::vector<int>> banks{};
std::string bankLine{};
while(std::getline(inputF,bankLine)){
banks.emplace_back(bankLine
| std::views::transform([](char c){ return c - '0';})
| std::ranges::to<std::vector<int>>());
}
inputF.close();
return std::move(banks);
}
auto calculateJoltage(const std::vector<int>& bank) -> long{
auto curIt = bank.begin();
auto rightIt = bank.end();
//int leftMax = 0;
auto leftBound = bank.begin();
// First find biggest number with that is not the last
while(curIt != rightIt-1){
if(*curIt > *leftBound ){
leftBound = curIt;
}
//std::println("Cur: {}, lMax: {}", *curIt, *leftBound);
curIt++;
}
int rightMax = 0;
while(leftBound != rightIt) {
rightMax = std::max(rightMax, *rightIt);
rightIt--;
}
std::println("LMax: {}, rMax: {}", *leftBound, rightMax);
return (*leftBound)*10 +rightMax;
}
auto countJoltages(const std::vector<std::vector<int>>& banks) {
long totalJoltage{};
for(const auto& bank : banks){
std::println("Bank: {}", bank);
totalJoltage += calculateJoltage(bank);
}
return totalJoltage;
}
auto main() -> int {
auto testCase = parseBanks("test_input");
if(testCase) {
auto testResult = countJoltages(*testCase);
// auto testResultP2 = countRepeats(*testCase);
std::println("P1 Testcase result: {}", testResult);
// std::println("P2 Testcase result: {}", testResultP2);
}
else{
std::print("{}\n", testCase.error());
}
auto realPuzzle = parseBanks("puzzle_input");
if(realPuzzle) {
auto realResult = countJoltages(*realPuzzle);
// auto realResultP2 = countRepeats(*realRanges);
std::println("P1 Real result: {}", realResult);
// std::println("P2 Real result: {}", realResultP2);
}
else{
std::print("{}\n", realPuzzle.error());
}
return 0;
}