This commit is contained in:
zv0n 2021-12-01 07:33:08 +01:00
parent df7e5882a8
commit 4336220d0e
3 changed files with 2077 additions and 0 deletions

22
01/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(AoC01)
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
)

2000
01/input Normal file

File diff suppressed because it is too large Load Diff

55
01/main.cpp Normal file
View File

@ -0,0 +1,55 @@
#include <fstream>
#include <iostream>
#include <sstream>
#include <vector>
std::vector<int> getDepths(const std::string &file_name) {
std::ifstream file(file_name);
std::vector<int> result{};
int tmp = 0;
std::string str;
while (std::getline(file, str)) {
std::stringstream ss(str);
ss >> tmp;
result.push_back(tmp);
}
return result;
}
int part1(const std::vector<int> &input) {
int result = 0;
int tmp = input[0];
for (auto &depth : input) {
if (depth > tmp) {
result++;
}
tmp = depth;
}
return result;
}
int part2(const std::vector<int> &input) {
int result = 0;
int tmp = input[0] + input[1] + input[2];
for (size_t i = 3; i < input.size(); i++) {
auto window = input[i] + input[i - 1] + input[i - 2];
if (window > tmp) {
result++;
}
tmp = window;
}
return result;
}
int main(int argc, char **argv) {
if (argc < 2) {
std::cerr << "You must provide input file!" << std::endl;
return 1;
}
auto depths = getDepths(argv[1]);
std::cout << "There are \033[91;1m" << part1(depths)
<< "\033[0m depth increases." << std::endl;
std::cout << "There are \033[91;1m" << part2(depths)
<< "\033[0m window depth increases." << std::endl;
return 0;
}