Add solution for 2016 Day 4

This commit is contained in:
Thraix
2026-08-14 16:09:32 +02:00
parent fc76349588
commit c228e8e230
3 changed files with 97 additions and 15 deletions
+4
View File
@@ -0,0 +1,4 @@
aaaaa-bbb-z-y-x-123[abxyz]
a-b-c-d-e-f-g-h-987[abcde]
not-a-real-room-404[oarel]
totally-real-room-200[decoy]
+85 -7
View File
@@ -2,30 +2,108 @@
namespace y2016::day04
{
REGISTER_DAY(2016, Day04, std::vector<int>, int);
struct Room
{
std::string name;
int sectorId;
std::string checksum;
};
REGISTER_TEST_EXAMPLE(2016, Day04, ExampleInput, 1, 0);
REGISTER_TEST(2016, Day04, Input, 1, 0);
REGISTER_TEST_EXAMPLE(2016, Day04, ExampleInput, 2, 0);
REGISTER_TEST(2016, Day04, Input, 2, 0);
REGISTER_DAY(2016, Day04, std::vector<Room>, int);
REGISTER_TEST_EXAMPLE(2016, Day04, ExampleInput, 1, 1514);
REGISTER_TEST(2016, Day04, Input, 1, 278221);
REGISTER_TEST(2016, Day04, Input, 2, 267);
READ_INPUT(input)
{
std::vector<int> vec;
std::vector<Room> vec;
std::string str;
while (getline(input, str))
{
Room room;
std::stringstream ss{str};
getline(ss, room.name, '[');
size_t pos = room.name.find_last_of('-');
room.sectorId = std::strtoll(room.name.c_str() + pos + 1, nullptr, 10);
room.name = room.name.substr(0, pos);
getline(ss, room.checksum, ']');
vec.emplace_back(room);
}
return vec;
}
struct CharCount
{
char c;
int count;
bool operator<(const CharCount& rhs) const
{
if (count != rhs.count)
return count > rhs.count;
return c < rhs.c;
}
};
bool ValidRoom(const Room& room)
{
std::array<int, 'z' - 'a' + 1> counts;
counts.fill(0);
for (auto c : room.name)
{
if (c >= 'a' && c <= 'z')
counts[c - 'a']++;
}
std::set<CharCount> mostUsed;
for (int i = 0; i < counts.size(); i++)
{
if (counts[i] > 0)
mostUsed.emplace(CharCount{(char)('a' + i), counts[i]});
}
int i = 0;
for (auto it = mostUsed.begin(); i < 5; it++, i++)
{
if (room.checksum[i] != it->c)
{
return false;
}
}
return true;
}
OUTPUT1(input)
{
return 0;
int count = 0;
for (const auto& room : input)
{
if (ValidRoom(room))
count += room.sectorId;
}
return count;
}
OUTPUT2(input)
{
for (const auto& room : input)
{
if (!ValidRoom(room))
continue;
std::string newName = room.name;
for (auto& c : newName)
{
if (c >= 'a' && c <= 'z')
c = ((c - 'a') + room.sectorId) % ('z' - 'a' + 1) + 'a';
}
if (newName == "northpole-object-storage")
return room.sectorId;
}
return 0;
}
}
+8 -8
View File
@@ -18,11 +18,11 @@
#define BIT(x) (1 << (x))
template <typename T>
static std::ostream& operator<<(std::ostream& stream, const std::vector<T>& container);
template <typename... Args>
static std::ostream& operator<<(std::ostream& stream, const std::vector<Args...>& container);
template <typename T>
static std::ostream& operator<<(std::ostream& stream, const std::set<T>& container);
template <typename... Args>
static std::ostream& operator<<(std::ostream& stream, const std::set<Args...>& container);
template <typename T, typename S>
static std::ostream& operator<<(std::ostream& stream, const std::map<T, S>& container);
@@ -730,14 +730,14 @@ public:
inline bool Helper::traceErrors = true;
template <typename T>
static std::ostream& operator<<(std::ostream& stream, const std::vector<T>& container)
template <typename... Args>
static std::ostream& operator<<(std::ostream& stream, const std::vector<Args...>& container)
{
return Helper::Print(stream, container);
}
template <typename T>
static std::ostream& operator<<(std::ostream& stream, const std::set<T>& container)
template <typename... Args>
static std::ostream& operator<<(std::ostream& stream, const std::set<Args...>& container)
{
return Helper::Print(stream, container);
}