Implement safe cracking logic and setup development environment
- Added a new `main.cpp` file with logic to parse dial rotations from a file and execute safe cracking based on the parsed data. - Introduced a `DialRotation` struct to represent the direction and distance of dial rotations. - Implemented `parseRotations` function to read and parse input from a specified file, returning a vector of `DialRotation` or an error message. - Created `executeSafeCrack` function to process the dial rotations and print the current state of the dial. - Added `.clang-format` and `.clang-tidy` configuration files for code formatting and linting. - Set up a Dockerfile for a development container with necessary tools and dependencies. - Configured VS Code settings and tasks for building, formatting, and running clang-tidy. - Added a script to run clang-tidy on the source files, ensuring code quality.
This commit is contained in:
parent
c093e8a4bf
commit
38eca3b747
11 changed files with 4654 additions and 3 deletions
|
|
@ -1,2 +1,11 @@
|
|||
add_executable(day1 main.cpp)
|
||||
set_target_properties(day1 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON)
|
||||
set_target_properties(day1 PROPERTIES CXX_STANDARD 23 CXX_STANDARD_REQUIRED ON)
|
||||
# Use GCC 15's libstdc++ with Clang 21
|
||||
target_compile_options(day1 PRIVATE --gcc-toolchain=/usr)
|
||||
|
||||
add_custom_command(TARGET day1 POST_BUILD
|
||||
COMMAND ${CMAKE_COMMAND} -E copy
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/day1_input
|
||||
$<TARGET_FILE_DIR:day1>/day1_input
|
||||
COMMENT "Copying day1_input to build directory"
|
||||
)
|
||||
|
|
|
|||
4446
day1/day1_input
4446
day1/day1_input
File diff suppressed because it is too large
Load diff
|
|
@ -1,5 +1,76 @@
|
|||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <vector>
|
||||
#include <expected>
|
||||
#include <print>
|
||||
|
||||
int main(int, char**){
|
||||
std::cout << "Hello, from aoc25 day1!\n";
|
||||
struct DialRotation {
|
||||
char direction;
|
||||
int distance;
|
||||
};
|
||||
|
||||
auto parseRotations(const std::string& filename) -> std::expected<std::vector<DialRotation>, std::string> {
|
||||
std::ifstream inputF {filename};
|
||||
|
||||
if (!inputF){
|
||||
|
||||
return std::unexpected{"Some file open error.\n"};
|
||||
}
|
||||
std::vector<DialRotation> dialRotations{};
|
||||
DialRotation curDial{};
|
||||
while ( inputF >> curDial.direction >> curDial.distance)
|
||||
{
|
||||
//std::println("{} : {}", curDial.direction, curDial.distance);
|
||||
dialRotations.push_back(curDial);
|
||||
}
|
||||
|
||||
return std::move(dialRotations);
|
||||
}
|
||||
constexpr int dialStart = 50;
|
||||
constexpr int maxDial = 100 ;
|
||||
|
||||
auto executeSafeCrack(const std::vector<DialRotation>& dialRotations){
|
||||
int dial {dialStart};
|
||||
int countZero {};
|
||||
for (const auto& curDial : dialRotations){
|
||||
if(curDial.direction == 'L'){
|
||||
dial -= curDial.distance % maxDial;
|
||||
if (dial < 0){
|
||||
dial += maxDial;
|
||||
}
|
||||
}
|
||||
else{
|
||||
dial += curDial.distance % maxDial;
|
||||
if (dial > maxDial - 1){
|
||||
dial -= maxDial;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
std::println("{} : {:3}, Dial now at: {:5}", curDial.direction, curDial.distance, dial);
|
||||
if (dial == 0){
|
||||
countZero++;
|
||||
}
|
||||
if (dial < 0){
|
||||
break;
|
||||
}
|
||||
if (dial > maxDial){
|
||||
break;
|
||||
}
|
||||
}
|
||||
std::print("Zeros: {}", countZero);
|
||||
}
|
||||
|
||||
auto main(int, char**) -> int {
|
||||
std::cout << "Hello, from aoc25 day1!!\n";
|
||||
auto retval = parseRotations("day1_input");
|
||||
|
||||
if(retval)
|
||||
{
|
||||
executeSafeCrack(*retval);
|
||||
}
|
||||
else{
|
||||
std::print("{}\n", retval.error());
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue