|
| 1 | +#include <fstream> |
| 2 | +#include <iostream> |
| 3 | +#include <string> |
| 4 | +#include <unordered_map> |
| 5 | + |
| 6 | +void print(const std::unordered_map<int, long long>& m) { |
| 7 | + for (const auto& p : m) { |
| 8 | + std::cout << p.first << ": " << p.second << '\n'; |
| 9 | + } |
| 10 | + std::cout << '\n'; |
| 11 | +} |
| 12 | + |
| 13 | +int main(int argc, char * argv[]) { |
| 14 | + std::string input = "../input/day_06_input"; |
| 15 | + if (argc > 1) { |
| 16 | + input = argv[1]; |
| 17 | + } |
| 18 | + |
| 19 | + std::string line; |
| 20 | + std::fstream file(input); |
| 21 | + std::getline(file, line); |
| 22 | + std::size_t start = 0; |
| 23 | + std::size_t end = line.find(',', start); |
| 24 | + std::unordered_map<int, long long> fish; |
| 25 | + for (int i = 0; i < 9; i++) { |
| 26 | + fish[i] = 0; |
| 27 | + } |
| 28 | + while (end != std::string::npos) { |
| 29 | + const auto val = std::stoll(line.substr(start, end - start)); |
| 30 | + fish[val]++; |
| 31 | + start = end + 1; |
| 32 | + end = line.find(',', start); |
| 33 | + } |
| 34 | + const auto val = std::stoll(line.substr(start, line.size() - start)); |
| 35 | + fish[val]++; |
| 36 | + // print(fish); |
| 37 | + |
| 38 | + for (int day = 0; day < 256; day++) { |
| 39 | + const long long to_add = fish[0]; |
| 40 | + for (int i = 1; i < 7; i++) { |
| 41 | + fish[i-1] = fish[i]; |
| 42 | + } |
| 43 | + fish[6] = to_add + fish[7]; |
| 44 | + fish[7] = fish[8]; |
| 45 | + fish[8] = to_add; |
| 46 | + // print(fish); |
| 47 | + } |
| 48 | + |
| 49 | + // std::cout << "Final numbers" << '\n'; |
| 50 | + // print(fish); |
| 51 | + long long sum = 0; |
| 52 | + for (const auto& p : fish) { |
| 53 | + sum += p.second; |
| 54 | + } |
| 55 | + std::cout << sum << '\n'; |
| 56 | + return 0; |
| 57 | +} |
0 commit comments