From b0b6773395691f626ba3bad5d08f8847e2e959fe Mon Sep 17 00:00:00 2001 From: Evan Burkey Date: Fri, 9 Sep 2022 11:13:53 -0700 Subject: [PATCH] update for macOS, 2015-08 --- CMakeLists.txt | 12 +++++++++-- get_input.sh | 8 +++---- src/2015/08.c | 51 +++++++++++++++++++++++++++++++++++++++----- src/advent_utility.c | 4 ++-- 4 files changed, 62 insertions(+), 13 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 2524e84..f707083 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -20,12 +20,20 @@ file(COPY input DESTINATION ${CMAKE_BINARY_DIR}) add_executable(advent ${SRC} ${SRC2015} ${SRC2016} ${SRC2017} ${SRC2018} ${SRC2019} ${SRC2020} ${SRC2021}) +if ((${CMAKE_SYSTEM_NAME} STREQUAL "Darwin")) + set(OPENSSL_ROOT_DIR /opt/homebrew/opt/openssl@3) +endif() + if ((${CMAKE_SYSTEM_NAME} STREQUAL "OpenBSD")) target_link_libraries(advent PRIVATE flint) target_include_directories(advent PRIVATE include lib/libflint/include lib/uthash/src) -else() +elseif ((${CMAKE_SYSTEM_NAME} STREQUAL "Linux")) find_package(OpenSSL REQUIRED) target_link_libraries(advent PRIVATE bsd flint ${OPENSSL_LIBRARIES}) target_include_directories(advent PRIVATE include lib/libflint/include lib/uthash/src ${OpenSSL_INCLUDE_DIR}) +else() # Basically MacOS/Darwin + set(OpenSSL_INCLUDE_DIR /opt/homebrew/opt/openssl@3/include) + find_package(OpenSSL REQUIRED) + target_link_libraries(advent PRIVATE flint ${OPENSSL_LIBRARIES}) + target_include_directories(advent PRIVATE include lib/libflint/include lib/uthash/src ${OpenSSL_INCLUDE_DIR}) endif() - diff --git a/get_input.sh b/get_input.sh index 5dc3ecd..59bfebf 100755 --- a/get_input.sh +++ b/get_input.sh @@ -1,8 +1,8 @@ -#!/usr/bin/env bash +#!/usr/bin/env zsh rm -rf input for year in {2015..2021}; do - mkdir -p input/$year + mkdir -p input/"$year" for day in {1..25}; do if [[ day -lt 10 ]]; then d="0$day" @@ -10,7 +10,7 @@ for year in {2015..2021}; do d="$day" fi url="https://adventofcode.com/$year/day/$day/input" - curl --cookie "session=$1" $url | perl -pe 'chomp if eof' > input/$year/$d + curl --cookie "session=$1" $url | perl -pe 'chomp if eof' > "input/$year/$d" done - touch input/$year/test + touch "input/$year/test" done diff --git a/src/2015/08.c b/src/2015/08.c index 7734647..dcd7dea 100644 --- a/src/2015/08.c +++ b/src/2015/08.c @@ -1,10 +1,51 @@ #include -#include +#include #include "lfinput.h" -void advent2015day08(void) { - char *input = get_input("input/2015/08"); - printf("Solution for Day 08 of 2015 is not completed yet\n"); - free(input); +static size_t part_one(char** input, size_t sz) { + size_t code = 0, mem = 0; + + for (size_t i = 0; i < sz; ++i) { + code += strlen(input[i]); + for (size_t j = 1; j < strlen(input[i]) - 1; ++j) { + if (input[i][j] == '\\') { + if (input[i][j + 1] == '\"' || input[i][j + 1] == '\\') { + ++j; + } else { + j += 3; + } + } + ++mem; + } + } + + return code - mem; +} + +static size_t part_two(char** input, size_t sz) { + size_t code = 0, encoded = 0; + + for (size_t i = 0; i < sz; ++i) { + code += strlen(input[i]); + encoded += 6; + for (size_t j = 1; j < strlen(input[i]) - 1; ++j) { + if (input[i][j] == '\\' || input[i][j] == '\"') { + ++encoded; + } + ++encoded; + } + } + + return encoded - code; +} + +void advent2015day08(void) { + size_t sz = 0; + char **input = get_lines("input/2015/08", &sz); + + printf("%zu\n", part_one(input, sz)); + printf("%zu\n", part_two(input, sz)); + + del_lines(input); } diff --git a/src/advent_utility.c b/src/advent_utility.c index 82f1d3c..34fd95f 100644 --- a/src/advent_utility.c +++ b/src/advent_utility.c @@ -2,7 +2,7 @@ #include #include -#ifdef __linux__ +#if defined __linux__ || defined __APPLE__ #include @@ -40,7 +40,7 @@ int same_Point_v(const void *a, const void *b) { char *md5_str(const char *input) { unsigned char digest[16]; -#ifdef __linux__ +#if defined __linux__ || defined __APPLE__ MD5_CTX context; MD5_Init(&context); MD5_Update(&context, input, strlen(input));