feat(01): Solved part 1

This commit is contained in:
Jiří Štefka 2023-12-10 04:14:17 +01:00
parent baf227ce23
commit 118f3632a9
Signed by: jiriks74
GPG Key ID: 1D5E30D3DB2264DE
8 changed files with 3957 additions and 36 deletions

View File

@ -5,6 +5,8 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
# Turn on testing by default
option(BUILD_TESTING "Build tests" ON)
# Turn off documentation build by default
option(BUILD_DOC "Build documentation" OFF)
# Turn off coverage by default
option(ENABLE_COVERAGE "Enable test coverage" ON)
@ -24,12 +26,6 @@ FUNCTION(PREPEND var )
SET(${var} "${listVar}" PARENT_SCOPE)
ENDFUNCTION(PREPEND)
# After a normal build, we can specify the location of various outputs of the
# build. We put executables and static libraries outside the build directory in
# bin/ and lib/, respectively.
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/bin")
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/lib")
# Include source code and headers. This calls the CMakeLists.txt in each
# subdirectory. These can define their own libraries, executables, etc. as targets,
# but here we define all exportable targets in the root CMakeLists.txt.
@ -50,3 +46,6 @@ endif()
add_executable(${PROJECT_NAME} ${SRC} ${INC})
target_include_directories(${PROJECT_NAME} PRIVATE include)
if(BUILD_DOC)
add_subdirectory(docs)
endif()

View File

@ -6,8 +6,9 @@
Something is wrong with global snow production,
and you've been selected to take a look.
The Elves have even given you a map; on it,
they've used stars to mark the top fifty locations that are likely to be having problems.
The Elves have even given you a map;
on it, they've used stars to mark the top fifty locations
that are likely to be having problems.
You've been doing this long enough to know that to restore snow operations,
you need to check all fifty stars by December 25th.
@ -30,18 +31,24 @@ has been amended by a very young Elf who was apparently just excited to show off
her art skills.
Consequently, the Elves are having trouble reading the values on the document.
The newly-improved calibration document consists of lines of text; each line originally contained a specific calibration value that the Elves now need to recover. On each line, the calibration value can be found by combining the first digit and the last digit (in that order) to form a single two-digit number.
The newly-improved calibration document consists of lines of text;
each line originally contained a specific calibration value
that the Elves now need to recover.
**On each line, the calibration value can be found by combining the first digit
and the last digit (in that order) to form a single two-digit number.**
For example:
```
```example
1abc2
pqr3stu8vwx
a1b2c3d4e5f
treb7uchet
```
In this example, the calibration values of these four lines are 12, 38, 15, and 77. Adding these together produces 142.
In this example, the calibration values of these four lines are 12, 38, 15, and 77.
Adding these together produces 142.
Consider your entire calibration document. What is the sum of all of the calibration values?
Consider your entire calibration document.
What is the sum of all of the calibration values?

1000
01/assignment_input.txt Normal file

File diff suppressed because it is too large Load Diff

20
01/docs/CMakeLists.txt Normal file
View File

@ -0,0 +1,20 @@
# check if Doxygen is installed
find_package(Doxygen)
if (DOXYGEN_FOUND)
# set input and output files
set(DOXYGEN_IN ${CMAKE_CURRENT_SOURCE_DIR}/Doxyfile)
set(DOXYGEN_OUT ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile)
# request to configure the file
configure_file(${DOXYGEN_IN} ${DOXYGEN_OUT} @ONLY)
message("Doxygen build started")
# note the option ALL which allows to build the docs together with the application
add_custom_target(doc ALL
COMMAND ${DOXYGEN_EXECUTABLE} ${DOXYGEN_OUT}
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
COMMENT "Generating documentation with Doxygen"
VERBATIM )
else (DOXYGEN_FOUND)
message("Doxygen needs to be installed to generate the documentation")
endif (DOXYGEN_FOUND)

2826
01/docs/Doxyfile Normal file

File diff suppressed because it is too large Load Diff

BIN
01/docs/logo.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

View File

@ -5,21 +5,71 @@
* @brief Main entry point
* @author jiriks74
*/
#define _GNU_SOURCE
// #define _LINE_LEN (size_t *)100
#define _LINE_LEN 100
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/**
* @brief Gets a calibration value from a line
* @param char* line containing the calibration value
* @return int the calibration value
*/
int getVal(char *str) {
int num = 0;
int lastNum = -1;
for (int i = 0; i < strlen(str); i++) {
if (str[i] >= *"0" && str[i] <= *"9") {
if (num == 0) {
num = (str[i] - 48) * 10;
} else {
lastNum = str[i] - 48;
}
}
}
if (lastNum == -1) {
num += num / 10;
} else {
num += lastNum;
}
return num;
}
/**
* @brief Main entry point
* @param argc Number of command-line arguments.
* @param argv Array of command-line arguments.
*/
#ifndef TESTING
#ifdef TESTING
int mainTest(FILE *stdin, int argc, char *argv[])
#else
int main(int argc, char *argv[])
#endif
#ifdef TESTING
int main_test(int argc, char *argv[])
#endif
{
printf("Hello world!\n");
return 0;
FILE *file;
if (argc == 1) {
file = stdin;
} else {
file = fopen(argv[1], "r");
}
if (file == NULL) {
if (argc > 1)
fprintf(stderr, "Could not open file '%s'\n", argv[1]);
else
fprintf(stderr, "Couldn't open file\n");
exit(EXIT_FAILURE);
}
char *line;
size_t len = 0;
ssize_t read;
int result = 0;
while ((read = getline(&line, &len, file) != -1)) {
result += getVal(line);
}
printf("The sum of the calibration values is %d\n", result);
return EXIT_SUCCESS;
}

View File

@ -1,31 +1,50 @@
#include "gtest/gtest.h"
#include <gtest/gtest.h>
#include <stdio.h>
#include <string>
#define TESTING
// Include the source file(s) to be tested.
#include "main.c"
// Create a test fixture class template - this will be like a "conlection" of
// tests. the : public ::testing::Test part is important! Add it to your fixture
// class.
class HelloTest : public ::testing::Test {
HelloTest() {}
void stream_add(FILE *stream, std::string string) {
fprintf(stream, "%s", string.c_str());
fseek(stream, -string.length(), SEEK_CUR);
}
~HelloTest() {}
class Trebuchet : public ::testing::Test {
public:
FILE *input;
void SetUp() {}
Trebuchet() {}
~Trebuchet() {}
void SetUp() {
input = tmpfile();
if (input == NULL) {
fprintf(stderr, "Error creating tmpfile\n");
fprintf(stderr, "Error: %s\n", strerror(errno));
exit(1);
}
}
void TearDown() {}
};
// Add tests to the test fixture class.
// @param fixture_class_name The name of the test fixture class.
// @param test_name The name of the test.
TEST(HelloTest, BasicAssertions) {
// Execute the code to be tested.
// Expect two strings not to be equal.
EXPECT_STRNE("hello", "world");
// Expect equality.
EXPECT_EQ(7 * 6, 42);
TEST_F(Trebuchet, AssignmentInput) {
testing::internal::CaptureStdout();
stream_add(input, "1abc2\npqr3stu8vwx\na1b2c3d4e5f\ntreb7uchet");
char* argv[2];
mainTest(input, 1, argv);
std::string output = testing::internal::GetCapturedStdout();
// Check the output of the program.
EXPECT_EQ("The sum of the calibration values is 142\n", output);
}
TEST_F(Trebuchet, getVal) {
EXPECT_EQ(12, getVal((char *)"1abc2"));
EXPECT_EQ(38, getVal((char *)"pqr3stu8vwx"));
EXPECT_EQ(15, getVal((char *)"a1b2c3d4e5f"));
EXPECT_EQ(77, getVal((char *)"treb7uchet"));
}