diff --git a/06/CMakeLists.txt b/06/CMakeLists.txt new file mode 100644 index 0000000..a1778f8 --- /dev/null +++ b/06/CMakeLists.txt @@ -0,0 +1,22 @@ +cmake_minimum_required(VERSION 3.10) +set(CMAKE_CXX_STANDARD 14) +set(CMAKE_CXX_STANDARD_REQUIRED True) +set(CMAKE_EXPORT_COMPILE_COMMANDS ON) +enable_language(CXX) + +project(AoC05) + +if(APPLE) + include_directories(/usr/local/include) + link_directories(/usr/local/lib) +endif() + +if(WIN32) + add_executable(${CMAKE_PROJECT_NAME} WIN32) +else() + add_executable(${CMAKE_PROJECT_NAME}) +endif() + +target_sources(${CMAKE_PROJECT_NAME} + PRIVATE main.cpp + ) diff --git a/06/input b/06/input new file mode 100644 index 0000000..1907feb --- /dev/null +++ b/06/input @@ -0,0 +1 @@ +3,4,3,1,2,1,5,1,1,1,1,4,1,2,1,1,2,1,1,1,3,4,4,4,1,3,2,1,3,4,1,1,3,4,2,5,5,3,3,3,5,1,4,1,2,3,1,1,1,4,1,4,1,5,3,3,1,4,1,5,1,2,2,1,1,5,5,2,5,1,1,1,1,3,1,4,1,1,1,4,1,1,1,5,2,3,5,3,4,1,1,1,1,1,2,2,1,1,1,1,1,1,5,5,1,3,3,1,2,1,3,1,5,1,1,4,1,1,2,4,1,5,1,1,3,3,3,4,2,4,1,1,5,1,1,1,1,4,4,1,1,1,3,1,1,2,1,3,1,1,1,1,5,3,3,2,2,1,4,3,3,2,1,3,3,1,2,5,1,3,5,2,2,1,1,1,1,5,1,2,1,1,3,5,4,2,3,1,1,1,4,1,3,2,1,5,4,5,1,4,5,1,3,3,5,1,2,1,1,3,3,1,5,3,1,1,1,3,2,5,5,1,1,4,2,1,2,1,1,5,5,1,4,1,1,3,1,5,2,5,3,1,5,2,2,1,1,5,1,5,1,2,1,3,1,1,1,2,3,2,1,4,1,1,1,1,5,4,1,4,5,1,4,3,4,1,1,1,1,2,5,4,1,1,3,1,2,1,1,2,1,1,1,2,1,1,1,1,1,4 diff --git a/06/main.cpp b/06/main.cpp new file mode 100644 index 0000000..4752493 --- /dev/null +++ b/06/main.cpp @@ -0,0 +1,66 @@ +#include +#include +#include +#include + +std::vector getFish(const std::string &file_name) { + std::vector fish; + + std::ifstream file(file_name); + std::string str; + std::getline(file, str); + int tmp{}; + char tmp_char{}; + std::stringstream ss(str); + while (ss >> tmp) { + fish.push_back(tmp); + ss >> tmp_char; + } + return fish; +} + +uint64_t computeFish(std::vector fish, int lifecycle, int young_addition, + int days) { + // index of bucket = days until reproduction + std::vector fish_buckets{}; + for (int i = 0; i < lifecycle + young_addition; i++) { + fish_buckets.push_back(0); + } + for (auto &f : fish) { + fish_buckets[f] += 1; + } + for (int i = 0; i < days; i++) { + auto new_fish = fish_buckets[0]; + for (int j = 1; j < fish_buckets.size(); j++) { + fish_buckets[j - 1] = fish_buckets[j]; + } + fish_buckets[lifecycle - 1] += new_fish; + fish_buckets.back() = new_fish; + } + uint64_t population = 0; + for (auto &f : fish_buckets) { + population += f; + } + return population; +} + +uint64_t part1(const std::vector &fish) { + return computeFish(fish, 7, 2, 80); +} + +uint64_t part2(const std::vector &fish) { + return computeFish(fish, 7, 2, 256); +} + +int main(int argc, char **argv) { + if (argc < 2) { + std::cerr << "You must provide input file!" << std::endl; + return 1; + } + auto fish = getFish(argv[1]); + std::cout << "The number of fish after 80 days is \033[91;1m" << part1(fish) + << "\033[0m." << std::endl; + std::cout << "The number of fish after 256 days is \033[91;1m" + << part2(fish) << "\033[0m." << std::endl; + return 0; +}