Skip to content

Instantly share code, notes, and snippets.

@willkill07
Created December 5, 2016 03:30
Show Gist options
  • Save willkill07/9fd38ed934c3eb7d60d16dcae6518226 to your computer and use it in GitHub Desktop.
Save willkill07/9fd38ed934c3eb7d60d16dcae6518226 to your computer and use it in GitHub Desktop.
Day04 AoC 2016
#include <algorithm>
#include <iostream>
#include <vector>
#include <chrono>
int main() {
int index{-1};
int sum{0};
std::string line;
auto timeStart = std::chrono::high_resolution_clock::now();
while (std::getline(std::cin, line)) {
int lastDash = line.find_last_of('-');
int onlyBracket = line.find_last_of('[');
std::string input{line.begin(), line.begin() + lastDash};
std::string check{line.begin() + onlyBracket + 1, line.begin() + onlyBracket + 6};
std::string sec{line.begin() + lastDash + 1, line.begin() + onlyBracket};
int sector{std::stoi(sec)};
if (index == -1) {
for (char &c : input)
c = (c=='-') ? c : ((c - 'a' + sector) % 26) + 'a';
if (input.find("north") != std::string::npos)
index = sector;
}
std::vector<std::pair<char, int>> s(26);
for(int i{0}; i < 26; ++i)
s[i] = {'a' + i, 0};
for (char c : input)
(c != '-') && ++s[c - 'a'].second;
std::stable_sort(s.begin(), s.end(), [](const auto& p1, const auto& p2) {
return p1.second > p2.second;
});
int i{0};
while (i < 5 && s[i].first == check[i])
++i;
sum += (i == 5) ? sector : 0;
}
auto timeStop = std::chrono::high_resolution_clock::now();
std::cout << sum << '\n' << index << std::endl;
std::cout << std::chrono::duration<double>{timeStop - timeStart}.count() << 's' << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment