2015-04, add md5 helper function

This commit is contained in:
Evan Burkey 2021-09-08 12:12:11 -07:00
parent 2e89a903ec
commit 0a7aaec503
12 changed files with 70 additions and 155 deletions

3
.gitignore vendored
View File

@ -2,6 +2,5 @@ input/
advent
compile_commands.json
.cache
cmake-build-debug
cmake-build-release
build
.clangd

2
.idea/advent.iml generated
View File

@ -1,2 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<module classpath="CMake" type="CPP_MODULE" version="4" />

4
.idea/misc.xml generated
View File

@ -1,4 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CMakeWorkspace" PROJECT_DIR="$PROJECT_DIR$" />
</project>

8
.idea/modules.xml generated
View File

@ -1,8 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/advent.iml" filepath="$PROJECT_DIR$/.idea/advent.iml" />
</modules>
</component>
</project>

6
.idea/vcs.xml generated
View File

@ -1,6 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

126
.idea/workspace.xml generated
View File

@ -1,126 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="AutoImportSettings">
<option name="autoReloadType" value="SELECTIVE" />
</component>
<component name="CMakeRunConfigurationManager" shouldGenerate="true" shouldDeleteObsolete="true">
<generated>
<config projectName="advent" targetName="advent" />
<config projectName="advent" targetName="flint" />
</generated>
</component>
<component name="CMakeSettings" AUTO_RELOAD="true">
<configurations>
<configuration PROFILE_NAME="Debug" ENABLED="true" CONFIG_NAME="Debug" />
<configuration PROFILE_NAME="Release" ENABLED="true" CONFIG_NAME="Release" />
</configurations>
</component>
<component name="ChangeListManager">
<list default="true" id="b07dea61-9ce6-4bea-9c31-27e5f3826c53" name="Changes" comment="">
<change afterPath="$PROJECT_DIR$/.gitmodules" afterDir="false" />
<change afterPath="$PROJECT_DIR$/include/advent_utility.h" afterDir="false" />
<change afterPath="$PROJECT_DIR$/lib/libflint" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/advent_utility.c" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/CMakeLists.txt" beforeDir="false" afterPath="$PROJECT_DIR$/CMakeLists.txt" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/2015/03.c" beforeDir="false" afterPath="$PROJECT_DIR$/src/2015/03.c" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
<option name="LAST_RESOLUTION" value="IGNORE" />
</component>
<component name="ClangdSettings">
<option name="formatViaClangd" value="false" />
</component>
<component name="ExecutionTargetManager" SELECTED_TARGET="CMakeBuildProfile:Debug" />
<component name="Git.Settings">
<option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$" />
</component>
<component name="MacroExpansionManager">
<option name="directoryName" value="1938zyxz" />
</component>
<component name="ProjectId" id="1xbNX7kuANn0Me5819c09SEpHhP" />
<component name="ProjectLevelVcsManager">
<ConfirmationsSetting value="2" id="Add" />
</component>
<component name="ProjectViewState">
<option name="hideEmptyMiddlePackages" value="true" />
<option name="showLibraryContents" value="true" />
</component>
<component name="PropertiesComponent">
<property name="ASKED_ADD_EXTERNAL_FILES" value="true" />
<property name="RunOnceActivity.OpenProjectViewOnStart" value="true" />
<property name="RunOnceActivity.ShowReadmeOnStart" value="true" />
<property name="RunOnceActivity.cidr.known.project.marker" value="true" />
<property name="WebServerToolWindowFactoryState" value="false" />
<property name="cf.first.check.clang-format" value="false" />
<property name="cidr.known.project.marker" value="true" />
<property name="cmake.loaded.for.project" value="true" />
<property name="node.js.detected.package.eslint" value="true" />
<property name="node.js.detected.package.tslint" value="true" />
<property name="node.js.selected.package.eslint" value="(autodetect)" />
<property name="node.js.selected.package.tslint" value="(autodetect)" />
<property name="nodejs_package_manager_path" value="npm" />
</component>
<component name="RunManager" selected="CMake Application.advent">
<configuration default="true" type="CLionExternalRunConfiguration" factoryName="Application" REDIRECT_INPUT="false" ELEVATE="false" USE_EXTERNAL_CONSOLE="false" PASS_PARENT_ENVS_2="true">
<method v="2">
<option name="CLION.EXTERNAL.BUILD" enabled="true" />
</method>
</configuration>
<configuration default="true" type="CMakeRunConfiguration" factoryName="Application" REDIRECT_INPUT="false" ELEVATE="false" USE_EXTERNAL_CONSOLE="false" PASS_PARENT_ENVS_2="true">
<method v="2">
<option name="com.jetbrains.cidr.execution.CidrBuildBeforeRunTaskProvider$BuildBeforeRunTask" enabled="true" />
</method>
</configuration>
<configuration name="advent" type="CMakeRunConfiguration" factoryName="Application" REDIRECT_INPUT="false" ELEVATE="false" USE_EXTERNAL_CONSOLE="false" PASS_PARENT_ENVS_2="true" PROJECT_NAME="advent" TARGET_NAME="advent" CONFIG_NAME="Debug" RUN_TARGET_PROJECT_NAME="advent" RUN_TARGET_NAME="advent">
<method v="2">
<option name="com.jetbrains.cidr.execution.CidrBuildBeforeRunTaskProvider$BuildBeforeRunTask" enabled="true" />
</method>
</configuration>
<configuration name="flint" type="CMakeRunConfiguration" factoryName="Application" REDIRECT_INPUT="false" ELEVATE="false" USE_EXTERNAL_CONSOLE="false" PASS_PARENT_ENVS_2="true" PROJECT_NAME="advent" TARGET_NAME="flint" CONFIG_NAME="Debug">
<method v="2">
<option name="com.jetbrains.cidr.execution.CidrBuildBeforeRunTaskProvider$BuildBeforeRunTask" enabled="true" />
</method>
</configuration>
<configuration default="true" type="GradleAppRunConfiguration" factoryName="Application" REDIRECT_INPUT="false" ELEVATE="false" USE_EXTERNAL_CONSOLE="false" PASS_PARENT_ENVS_2="true">
<method v="2">
<option name="com.jetbrains.cidr.cpp.gradle.execution.GradleNativeBuildBeforeRunTaskProvider$BuildBeforeRunTask" enabled="true" />
</method>
</configuration>
<list>
<item itemvalue="CMake Application.advent" />
<item itemvalue="CMake Application.flint" />
</list>
</component>
<component name="SpellCheckerSettings" RuntimeDictionaries="0" Folders="0" CustomDictionaries="0" DefaultDictionary="application-level" UseSingleDictionary="true" transferred="true" />
<component name="TaskManager">
<task active="true" id="Default" summary="Default task">
<changelist id="b07dea61-9ce6-4bea-9c31-27e5f3826c53" name="Changes" comment="" />
<created>1630619869539</created>
<option name="number" value="Default" />
<option name="presentableId" value="Default" />
<updated>1630619869539</updated>
<workItem from="1630619872068" duration="1587000" />
</task>
<servers />
</component>
<component name="TypeScriptGeneratedFilesManager">
<option name="version" value="3" />
</component>
<component name="Vcs.Log.Tabs.Properties">
<option name="TAB_STATES">
<map>
<entry key="MAIN">
<value>
<State />
</value>
</entry>
</map>
</option>
</component>
<component name="VcsManagerConfiguration">
<option name="ADD_EXTERNAL_FILES_SILENTLY" value="true" />
</component>
</project>

View File

@ -1,5 +1,7 @@
cmake_minimum_required(VERSION 3.10)
set(CMAKE_C_STANDARD 99)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(CMAKE_BUILD_TYPE Debug)
project(advent C)
@ -17,12 +19,12 @@ file(COPY input DESTINATION ${CMAKE_BINARY_DIR})
add_executable(advent ${SRC} ${SRC2015} ${SRC2016} ${SRC2017} ${SRC2018} ${SRC2019} ${SRC2020})
if ((${CMAKE_SYSTEM_NAME} STREQUAL "OpenBSD") OR (${CMAKE_SYSTEM_NAME} STREQUAL "FreeBSD"))
message(WARNING "${CMAKE_SYSTEM_NAME}: Not linking libbsd")
if ((${CMAKE_SYSTEM_NAME} STREQUAL "OpenBSD"))
target_link_libraries(advent PRIVATE flint)
target_include_directories(advent PRIVATE include lib/libflint/include)
else()
message(WARNING "${CMAKE_SYSTEM_NAME}: Linking libbsd")
target_link_libraries(advent PRIVATE bsd flint)
find_package(OpenSSL REQUIRED)
target_link_libraries(advent PRIVATE bsd flint ${OPENSSL_LIBRARIES})
target_include_directories(advent PRIVATE include lib/libflint/include ${OpenSSL_INCLUDE_DIR})
endif()
target_include_directories(advent PRIVATE include lib/libflint/include)

View File

@ -17,7 +17,7 @@ Build the project using Cmake:
cmake ..
make
The supplied `build.sh` script uses `scan-build` to build a compilation database. This is not neccesary for users.
The supplied `build.sh` script does the above for you.
## Inputs

View File

@ -5,4 +5,5 @@ cd build
cmake ..
make
cp advent ..
cp compile_commands.json ..
cd ..

View File

@ -11,4 +11,6 @@ struct Point *new_Point_p(int, int);
int same_Point(const struct Point*, const struct Point*);
int same_Point_v(const void*, const void*);
char *md5_str(const char*);
#endif

View File

@ -1,10 +1,51 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "input.h"
#include "advent_utility.h"
static int is_five(char *test) {
char *t = test;
for (int i = 0; i < 5; ++i, ++t) {
if (*t != '0') {
return 0;
}
}
return 1;
}
static int is_six(char *test) {
char *t = test;
for (int i = 0; i < 6; ++i, ++t) {
if (*t != '0') {
return 0;
}
}
return 1;
}
void advent2015day04(void) {
char *input = get_input("input/2015/04");
printf("Solution for Day 04 of 2015 is not completed yet\n");
int c = 0, five = 0, six = 0;
while (!five || !six) {
char test[strlen(input) + 8];
sprintf(test, "%s%d", input, c);
char *md5 = md5_str(test);
if (!five && is_five(md5)) {
five = c;
printf("%d\n", five);
}
if (is_six(md5)) {
six = c;
}
free(md5);
++c;
}
printf("%d\n", six);
free(input);
}

View File

@ -1,4 +1,7 @@
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <md5.h>
#include "advent_utility.h"
@ -26,3 +29,16 @@ int same_Point(const struct Point* a, const struct Point* b) {
int same_Point_v(const void *a, const void *b) {
return same_Point((const struct Point *)a, (const struct Point *)b);
}
char *md5_str(const char* input) {
unsigned char digest[16];
struct MD5Context context;
MD5Init(&context);
MD5Update(&context, input, strlen(input));
MD5Final(digest, &context);
char *md5string = malloc(33);
for(int i = 0; i < 16; ++i) {
sprintf(&md5string[i*2], "%02x", (unsigned int)digest[i]);
}
return md5string;
}