diff --git a/res/2016/day04/test_input.txt b/res/2016/day04/test_input.txt index e69de29..1d296af 100644 --- a/res/2016/day04/test_input.txt +++ b/res/2016/day04/test_input.txt @@ -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] diff --git a/src/2016/Day04.cpp b/src/2016/Day04.cpp index d7a4de3..4799c0a 100644 --- a/src/2016/Day04.cpp +++ b/src/2016/Day04.cpp @@ -2,30 +2,108 @@ namespace y2016::day04 { - REGISTER_DAY(2016, Day04, std::vector, 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, 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 vec; + std::vector 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 counts; + counts.fill(0); + for (auto c : room.name) + { + if (c >= 'a' && c <= 'z') + counts[c - 'a']++; + } + + std::set 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; } } diff --git a/src/common/lib/Helper.h b/src/common/lib/Helper.h index 19a529b..c4db010 100644 --- a/src/common/lib/Helper.h +++ b/src/common/lib/Helper.h @@ -18,11 +18,11 @@ #define BIT(x) (1 << (x)) -template -static std::ostream& operator<<(std::ostream& stream, const std::vector& container); +template +static std::ostream& operator<<(std::ostream& stream, const std::vector& container); -template -static std::ostream& operator<<(std::ostream& stream, const std::set& container); +template +static std::ostream& operator<<(std::ostream& stream, const std::set& container); template static std::ostream& operator<<(std::ostream& stream, const std::map& container); @@ -730,14 +730,14 @@ public: inline bool Helper::traceErrors = true; -template -static std::ostream& operator<<(std::ostream& stream, const std::vector& container) +template +static std::ostream& operator<<(std::ostream& stream, const std::vector& container) { return Helper::Print(stream, container); } -template -static std::ostream& operator<<(std::ostream& stream, const std::set& container) +template +static std::ostream& operator<<(std::ostream& stream, const std::set& container) { return Helper::Print(stream, container); }