2025 day 2 part 1
This commit is contained in:
1
2025/2.txt
Normal file
1
2025/2.txt
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
|
||||||
@@ -3,6 +3,7 @@
|
|||||||
|
|
||||||
#include "aoc.hpp"
|
#include "aoc.hpp"
|
||||||
#include "day1.hpp"
|
#include "day1.hpp"
|
||||||
|
#include "day2.hpp"
|
||||||
|
|
||||||
int main(int argc, char** argv)
|
int main(int argc, char** argv)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -12,6 +12,11 @@ struct FileFragment
|
|||||||
std::string Data;
|
std::string Data;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
inline std::pair<FileFragment, FileFragment> SplitToken(std::string del)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
class File
|
class File
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|||||||
116
2025/day2.hpp
Normal file
116
2025/day2.hpp
Normal file
@@ -0,0 +1,116 @@
|
|||||||
|
#include "aoc.hpp"
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
|
#include <unordered_map>
|
||||||
|
|
||||||
|
class Day02 : public AOCDay
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
Day02() {}
|
||||||
|
~Day02() {}
|
||||||
|
int Day() override {return 2;}
|
||||||
|
|
||||||
|
int PartOne(File& f) override
|
||||||
|
{
|
||||||
|
f.SplitBy(",");
|
||||||
|
|
||||||
|
uint64_t res = 0;
|
||||||
|
|
||||||
|
// Range of ID's (11-22)
|
||||||
|
for (auto range : f.TokensForLine(0))
|
||||||
|
{
|
||||||
|
int dash = range.Data.find('-', 0);
|
||||||
|
|
||||||
|
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++)
|
||||||
|
{
|
||||||
|
// Convert number back into string
|
||||||
|
std::string current = std::to_string(i);
|
||||||
|
// import 'is-odd' lol
|
||||||
|
if (current.length() & 1) continue;
|
||||||
|
|
||||||
|
int mid = current.length() / 2;
|
||||||
|
|
||||||
|
uint64_t l = std::stoll(current.substr(0, mid));
|
||||||
|
uint64_t r = std::stoll(current.substr(mid));
|
||||||
|
|
||||||
|
if (l == r)
|
||||||
|
{
|
||||||
|
res += i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
std::cout << "The answer doesn't fit in int : "<< res << std::endl;
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
int PartTwo(File& f) override
|
||||||
|
{
|
||||||
|
f.SplitBy(",");
|
||||||
|
|
||||||
|
uint64_t res = 0;
|
||||||
|
|
||||||
|
for (auto range : f.TokensForLine(0))
|
||||||
|
{
|
||||||
|
int dash = range.Data.find('-', 0);
|
||||||
|
|
||||||
|
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++)
|
||||||
|
{
|
||||||
|
std::string current = std::to_string(i);
|
||||||
|
int n = current.length();
|
||||||
|
|
||||||
|
bool invalid = false;
|
||||||
|
|
||||||
|
// Try every possible substring start (i)
|
||||||
|
for (int start = 0; start < n && !invalid; start++)
|
||||||
|
{
|
||||||
|
std::string accum;
|
||||||
|
|
||||||
|
// Build accum char-by-char
|
||||||
|
for (int end = start; end < n && !invalid; end++)
|
||||||
|
{
|
||||||
|
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;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (invalid)
|
||||||
|
res += i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
std::cout << "The answer doesn't fit in int : "<< res << std::endl;
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
ADD_AOC_DAY(Day02);
|
||||||
|
|
||||||
Reference in New Issue
Block a user