Day2Part1
This commit is contained in:
parent
86dcbd3aaf
commit
5d1d878d78
7 changed files with 117 additions and 18 deletions
2
day2/CMakeLists.txt
Normal file
2
day2/CMakeLists.txt
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
# Use top-level helper to add the target and copy input files
|
||||
aoc_add_day(day2 "${CMAKE_CURRENT_SOURCE_DIR}" main.cpp)
|
||||
83
day2/main.cpp
Normal file
83
day2/main.cpp
Normal file
|
|
@ -0,0 +1,83 @@
|
|||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <expected>
|
||||
#include <print>
|
||||
#include <ranges>
|
||||
|
||||
struct IDRange {
|
||||
std::string leftRange;
|
||||
std::string rightRange;
|
||||
};
|
||||
|
||||
auto parseRanges(const std::string& filename) -> std::expected<std::vector<IDRange>, std::string> {
|
||||
std::ifstream inputF {filename};
|
||||
|
||||
if (!inputF){
|
||||
return std::unexpected{"Some file open error.\n"};
|
||||
}
|
||||
std::vector<IDRange> idRanges{};
|
||||
|
||||
std::string bigString{};
|
||||
std::getline(inputF,bigString);
|
||||
auto ranges = std::ranges::to<std::vector<std::string>>(std::views::split(bigString, ','));
|
||||
|
||||
for(const auto& range : ranges){
|
||||
auto subParts = std::ranges::to<std::vector<std::string>>(std::views::split(range, '-'));
|
||||
idRanges.emplace_back(subParts[0], subParts[1]);
|
||||
}
|
||||
|
||||
inputF.close();
|
||||
|
||||
return std::move(idRanges);
|
||||
}
|
||||
|
||||
|
||||
|
||||
auto countDoubles(const std::vector<IDRange>& idRanges) {
|
||||
long doubles{};
|
||||
for(const auto& rng : idRanges){
|
||||
//std::println("{}-{}", rng.leftRange, rng.rightRange);
|
||||
for( long i = std::stol(rng.leftRange); i <= std::stol(rng.rightRange); i++){
|
||||
auto str_version = std::to_string(i) ;
|
||||
auto str_len = str_version.size();
|
||||
//std::print("Checking {}. Length: {}", str_version, str_version.size());
|
||||
if (str_len % 2 == 0) {
|
||||
//std::print(" Divisible by two, splitting...");
|
||||
std::string_view left_half = std::string_view(str_version).substr(0, str_len/2);
|
||||
std::string_view right_half = std::string_view(str_version).substr(str_len/2, str_len);
|
||||
//std::print("Halves: {}/{}",left_half, right_half);
|
||||
if(left_half == right_half){
|
||||
//std::print(" EQUAL!");
|
||||
doubles += i;
|
||||
}
|
||||
}
|
||||
//std::println();
|
||||
}
|
||||
}
|
||||
return doubles;
|
||||
}
|
||||
|
||||
auto main() -> int {
|
||||
auto testCase = parseRanges("test_input");
|
||||
if(testCase) {
|
||||
auto testResult = countDoubles(*testCase);
|
||||
std::println("Testcase result: {}", testResult);
|
||||
}
|
||||
else{
|
||||
std::print("{}\n", testCase.error());
|
||||
}
|
||||
|
||||
|
||||
auto realRanges = parseRanges("puzzle_input");
|
||||
if(realRanges) {
|
||||
auto realResult = countDoubles(*realRanges);
|
||||
std::println("Real result: {}", realResult);
|
||||
}
|
||||
else{
|
||||
std::print("{}\n", realRanges.error());
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
1
day2/puzzle_input
Normal file
1
day2/puzzle_input
Normal file
|
|
@ -0,0 +1 @@
|
|||
9191906840-9191941337,7671-13230,2669677096-2669816099,2-12,229599-392092,48403409-48523311,96763-229430,1919163519-1919240770,74928-96389,638049-668065,34781-73835,736781-819688,831765539-831907263,5615884-5749554,14101091-14196519,7134383-7169141,413340-625418,849755289-849920418,7745350-7815119,16717-26267,4396832-4549887,87161544-87241541,4747436629-4747494891,335-549,867623-929630,53-77,1414-3089,940604-1043283,3444659-3500714,3629-7368,79-129,5488908-5597446,97922755-98097602,182-281,8336644992-8336729448,24-47,613-1077
|
||||
1
day2/test_input
Normal file
1
day2/test_input
Normal file
|
|
@ -0,0 +1 @@
|
|||
11-22,95-115,998-1012,1188511880-1188511890,222220-222224,1698522-1698528,446443-446449,38593856-38593862,565653-565659,824824821-824824827,2121212118-2121212124
|
||||
Loading…
Add table
Add a link
Reference in a new issue