0
0

PB4 part1 done

This commit is contained in:
Pcornat 2024-08-01 00:11:26 +02:00
parent 273384c8e7
commit 927b7ee529
Signed by: Pcornat
GPG Key ID: E0326CC678A00BDD
5 changed files with 117 additions and 2 deletions

View File

@ -50,7 +50,9 @@ add_subdirectory(pb_1)
add_subdirectory(pb_2) add_subdirectory(pb_2)
set_target_properties(AdventOfCode2023 pb_1 pb_2 PROPERTIES add_subdirectory(pb_4)
set_target_properties(AdventOfCode2023 pb_1 pb_2 pb_4 PROPERTIES
CXX_STANDARD 20 CXX_STANDARD 20
CXX_STANDARD_REQUIRED ON CXX_STANDARD_REQUIRED ON
CXX_EXTENSIONS OFF CXX_EXTENSIONS OFF
@ -64,4 +66,4 @@ set_target_properties(AdventOfCode2023 pb_1 pb_2 PROPERTIES
#target_link_options(AdventOfCode2023 PUBLIC ${LINKER_OPTIONS}) #target_link_options(AdventOfCode2023 PUBLIC ${LINKER_OPTIONS})
target_link_libraries(AdventOfCode2023 PUBLIC pb_1 pb_2 ${LINKER_FLAGS}) target_link_libraries(AdventOfCode2023 PUBLIC pb_1 pb_2 pb_4 ${LINKER_FLAGS})

View File

@ -1,6 +1,7 @@
#include <iostream> #include <iostream>
#include "pb_1/problem_1.hpp" #include "pb_1/problem_1.hpp"
#include "pb_2/problem_2.hpp" #include "pb_2/problem_2.hpp"
#include "pb_4/problem_4.hpp"
int main() { int main() {
{ {
@ -21,5 +22,12 @@ int main() {
std::cout << "Problem 2 part 1: " << result << '\n'; std::cout << "Problem 2 part 1: " << result << '\n';
} }
} }
{
std::cout << "Begin Problem 4 part 1\n";
if (const auto result = pb4::problem_part1("inputs/pb4.txt"); result != 0) {
std::cout << "Problem 4 part 1: " << result << '\n';
}
}
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }

11
pb_4/CMakeLists.txt Normal file
View File

@ -0,0 +1,11 @@
project(pb_4 CXX)
add_library(pb_4 STATIC problem_4.cpp problem_4.hpp)
target_compile_definitions(pb_4 PUBLIC $<$<AND:$<CONFIG:Debug>,$<STREQUAL:$<CXX_COMPILER_ID>,GNU>>:_GLIBCXX_DEBUG>)
target_compile_options(pb_4 PUBLIC ${COMPILE_FLAGS})
target_link_options(pb_4 PUBLIC ${LINKER_OPTIONS})
target_link_libraries(pb_4 ${LINKER_FLAGS} common)

74
pb_4/problem_4.cpp Normal file
View File

@ -0,0 +1,74 @@
//
// Created by postaron on 31/07/24.
//
#include "problem_4.hpp"
#include "../common/common_functions.hpp"
#include <vector>
#include <set>
#include <string>
#include <cmath>
#include <algorithm>
#include <numeric>
std::vector<int> extract_unique_cards(const std::string &input) {
std::set<int> unique_part;
bool begin_num = false;
std::string tmp_num;
for (const auto &item: input) {
if (!begin_num && item != ' ') {
begin_num = true;
tmp_num.push_back(item);
} else if (begin_num && item == ' ') {
begin_num = false;
unique_part.insert(std::stoi(tmp_num));
tmp_num.clear();
} else if (begin_num) {
tmp_num.push_back(item);
}
}
if (!tmp_num.empty()) {
unique_part.insert(std::stoi(tmp_num));
}
return { unique_part.cbegin(), unique_part.cend() };
}
std::size_t pb4::problem_part1(const std::filesystem::path &problemFile) noexcept {
auto file_opt = common::read_file(problemFile);
if (!file_opt) {
return 0;
}
std::ifstream &file = *file_opt;
std::string line;
std::vector<int> compute_points;
while (std::getline(file, line)) {
if (!line.empty()) {
line = line.substr(line.find_first_of(':') + 1);
const auto split_char = line.find_first_of('|');
const auto left = line.substr(0, split_char);
const auto right = line.substr(split_char + 1);
const auto left_cards = extract_unique_cards(left);
const auto right_cards = extract_unique_cards(right);
std::vector<int> intersection;
intersection.reserve(left.size() + right_cards.size());
std::set_intersection(left_cards.cbegin(),
left_cards.cend(),
right_cards.cbegin(),
right_cards.cend(),
std::back_inserter(intersection));
if (!intersection.empty()) {
compute_points.emplace_back(
static_cast<int>(std::floor(std::pow(2.0f, static_cast<float>(intersection.size() - 1))))
);
} else {
compute_points.emplace_back(0);
}
}
}
return std::accumulate(compute_points.cbegin(), compute_points.cend(), 0);
}

20
pb_4/problem_4.hpp Normal file
View File

@ -0,0 +1,20 @@
//
// Created by postaron on 31/07/24.
//
#ifndef ADVENTOFCODE2023_PROBLEM_4_HPP
#define ADVENTOFCODE2023_PROBLEM_4_HPP
#include <filesystem>
#include <vector>
#include <set>
namespace pb4 {
namespace fs = std::filesystem;
std::size_t problem_part1(const fs::path &problemFile) noexcept;
std::size_t problem_part2(const fs::path &problemFile) noexcept;
}
#endif //ADVENTOFCODE2023_PROBLEM_4_HPP