This commit is contained in:
zv0n 2021-12-06 09:07:32 +01:00
parent b097b1887f
commit 2341d77e03
3 changed files with 89 additions and 0 deletions

22
06/CMakeLists.txt Normal file
View File

@ -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
)

1
06/input Normal file
View File

@ -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

66
06/main.cpp Normal file
View File

@ -0,0 +1,66 @@
#include <fstream>
#include <iostream>
#include <sstream>
#include <vector>
std::vector<int> getFish(const std::string &file_name) {
std::vector<int> 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<int> fish, int lifecycle, int young_addition,
int days) {
// index of bucket = days until reproduction
std::vector<uint64_t> 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<int> &fish) {
return computeFish(fish, 7, 2, 80);
}
uint64_t part2(const std::vector<int> &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;
}