diff --git a/2025/Makefile b/2025/Makefile index 0de7060..6854251 100644 --- a/2025/Makefile +++ b/2025/Makefile @@ -1,5 +1,5 @@ CXX := clang++ -CXXFLAGS := -std=c++17 -Wall -Wextra -I. +CXXFLAGS := -std=c++17 -Wall -Wextra -I. -O3 TARGET := aoc SRC := aoc.cpp diff --git a/2025/aoc b/2025/aoc index 9017751..d83ea8e 100755 Binary files a/2025/aoc and b/2025/aoc differ diff --git a/2025/aoc.cpp b/2025/aoc.cpp index 31fee50..990f830 100644 --- a/2025/aoc.cpp +++ b/2025/aoc.cpp @@ -79,7 +79,7 @@ int main(int argc, char** argv) File file{path}; File file1{path}; - int partOne = day->PartOne(file); + uint64_t partOne = day->PartOne(file); int partTwo = day->PartTwo(file1); std::cout << "Part 1: " << partOne << "\n"; @@ -100,12 +100,12 @@ int main(int argc, char** argv) auto start = std::chrono::high_resolution_clock::now(); - int partOne = day->PartOne(file); + uint64_t partOne = day->PartOne(file); auto endpart1 = std::chrono::high_resolution_clock::now(); auto startpart2 = std::chrono::high_resolution_clock::now(); - int partTwo = day->PartTwo(file1); + uint64_t partTwo = day->PartTwo(file1); auto end = std::chrono::high_resolution_clock::now(); diff --git a/2025/aoc.hpp b/2025/aoc.hpp index 7c339eb..a8be3f8 100644 --- a/2025/aoc.hpp +++ b/2025/aoc.hpp @@ -179,8 +179,8 @@ class AOCDay // return 1 -> 1.txt virtual int Day() = 0; - virtual int PartOne(File&) = 0; - virtual int PartTwo(File&) = 0; + virtual uint64_t PartOne(File&) = 0; + virtual uint64_t PartTwo(File&) = 0; }; inline std::unordered_map& GetRegisteredDays(int day = 0) diff --git a/2025/day1.hpp b/2025/day1.hpp index f426b2b..05bfc50 100644 --- a/2025/day1.hpp +++ b/2025/day1.hpp @@ -17,7 +17,7 @@ public: if (dial < 0) dial += 100; } - int PartOne(File& f) override + uint64_t PartOne(File& f) override { f.SplitByIndex(1); @@ -41,7 +41,7 @@ public: return res; } - int PartTwo(File& f) override + uint64_t PartTwo(File& f) override { f.SplitByIndex(1); diff --git a/2025/day2.hpp b/2025/day2.hpp index bfd0e00..5e8004a 100644 --- a/2025/day2.hpp +++ b/2025/day2.hpp @@ -10,7 +10,7 @@ public: ~Day02() {} int Day() override {return 2;} - int PartOne(File& f) override + uint64_t PartOne(File& f) override { f.SplitBy(","); @@ -43,16 +43,16 @@ public: } } - std::cout << "The answer doesn't fit in int : "<< res << std::endl; return res; } - int PartTwo(File& f) override + uint64_t PartTwo(File& f) override { f.SplitBy(","); uint64_t res = 0; + // For every range like "11-22" in line 0 for (auto range : f.TokensForLine(0)) { int dash = range.Data.find('-', 0); @@ -60,56 +60,49 @@ public: uint64_t a = std::stoll(range.Data.substr(0, dash)); uint64_t b = std::stoll(range.Data.substr(dash + 1)); - for (uint64_t i = a; i <= b; i++) + // Loop every ID in the range + for (uint64_t id = a; id <= b; ++id) { - std::string current = std::to_string(i); - int n = current.length(); + std::string s = std::to_string(id); + int digits = s.length(); bool invalid = false; - // Try every possible substring start (i) - for (int start = 0; start < n && !invalid; start++) + for (int L = 1; L <= digits / 2 && !invalid; ++L) { - std::string accum; + // MUST divide evenly, otherwise blocks won't align + if (digits % L != 0) + continue; - // Build accum char-by-char - for (int end = start; end < n && !invalid; end++) + uint64_t base = 1; + for (int i = 0; i < L; i++) + base *= 10; // base = 10^L + + uint64_t block = id % base; + uint64_t tail = id; + int count = 0; + + while (tail > 0) { - accum.push_back(current[end]); - - int len = accum.length(); - int remaining = n - start; - - // Now check if repeats match - bool matches = true; - - for (int rep = 0; rep < remaining / len; rep++) - { - if (current.compare(start + rep * len, len, accum) != 0) - { - matches = false; - break; - } - } - - // Must be at least two repeats - if (matches && remaining / len >= 2) - { - invalid = true; + if (tail % base != block) break; - } + + tail /= base; + count++; } + + if (tail == 0 && count >= 2) + invalid = true; } - if (invalid) - res += i; + if (invalid) + res += id; + } } - } - - std::cout << "The answer doesn't fit in int : "<< res << std::endl; return res; } + }; ADD_AOC_DAY(Day02);