From dc8e8a27febe3a461f9a3347aec95391f1441181 Mon Sep 17 00:00:00 2001 From: Evan Burkey Date: Thu, 2 Sep 2021 15:25:09 -0700 Subject: [PATCH] 2015-03. Add advent_utility and libflint --- .gitmodules | 3 ++ .idea/workspace.xml | 38 +++++++++++++++-------- CMakeLists.txt | 7 +++-- README.md | 4 +++ include/advent_utility.h | 14 +++++++++ lib/libflint | 1 + src/2015/03.c | 66 +++++++++++++++++++++++++++++++++++++++- src/advent_utility.c | 28 +++++++++++++++++ 8 files changed, 146 insertions(+), 15 deletions(-) create mode 100644 .gitmodules create mode 100644 include/advent_utility.h create mode 160000 lib/libflint create mode 100644 src/advent_utility.c diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..7281ad5 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "lib/libflint"] + path = lib/libflint + url = git@git.fputs.com:fputs/libflint diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 8cfc77d..26edda4 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -1,8 +1,12 @@ + + + @@ -13,16 +17,13 @@ - - - - - - - - - - + + + + + + + - + + + + + + + + + @@ -88,7 +102,7 @@ diff --git a/CMakeLists.txt b/CMakeLists.txt index c027341..b6e4b4d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,7 +1,10 @@ cmake_minimum_required(VERSION 3.10) +set(CMAKE_C_STANDARD 99) project(advent C) +add_subdirectory(lib/libflint) + file(GLOB SRC src/*.c) file(GLOB SRC2015 src/2015/*.c) file(GLOB SRC2016 src/2016/*.c) @@ -13,5 +16,5 @@ file(GLOB SRC2020 src/2020/*.c) file(COPY input DESTINATION ${CMAKE_BINARY_DIR}) add_executable(advent ${SRC} ${SRC2015} ${SRC2016} ${SRC2017} ${SRC2018} ${SRC2019} ${SRC2020}) -target_link_libraries(advent PRIVATE bsd) -target_include_directories(advent PRIVATE include) \ No newline at end of file +target_link_libraries(advent PRIVATE bsd flint) +target_include_directories(advent PRIVATE include lib/libflint/include) \ No newline at end of file diff --git a/README.md b/README.md index 6bbe8f8..e1f1eb6 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,10 @@ Advent Of Code solutions using C99. ## Building +Be sure to clone the project with its submodules: + + git clone --recurse-submodules -j8 https://git.fputs.com/fputs/advent + This project relies on several BSD extensions to the stdlib. BSDs should be able to build the project out of the box. Linux users will need `libbsd` installed. The package is called `libbsd-dev` on Debian-based systems. Build the project using Cmake: diff --git a/include/advent_utility.h b/include/advent_utility.h new file mode 100644 index 0000000..4f83c8b --- /dev/null +++ b/include/advent_utility.h @@ -0,0 +1,14 @@ +#ifndef ADVENT_H_UTILITY_ +#define ADVENT_H_UTILITY_ + +struct Point { + int x; + int y; +}; + +struct Point new_Point(int, int); +struct Point *new_Point_p(int, int); +int same_Point(const struct Point*, const struct Point*); +int same_Point_v(const void*, const void*); + +#endif diff --git a/lib/libflint b/lib/libflint new file mode 160000 index 0000000..d564364 --- /dev/null +++ b/lib/libflint @@ -0,0 +1 @@ +Subproject commit d5643646860cdc2e0401b63094408753893e3ae0 diff --git a/src/2015/03.c b/src/2015/03.c index 4660cdf..e33c5f1 100644 --- a/src/2015/03.c +++ b/src/2015/03.c @@ -1,10 +1,74 @@ #include #include +#include +#include "set.h" #include "input.h" +#include "advent_utility.h" + +static int part_two(char *input) { + int sx = 0, sy = 0, rx = 0, ry = 0, is_santa = 1; + int *x, *y; + char *c = input; + + Set* houses = malloc(sizeof(Set)); + set_init(houses, same_Point_v, free); + + while (*c != '\0') { + if (is_santa) { + x = &sx; + y = &sy; + } else { + x = ℞ + y = &ry; + } + is_santa = is_santa == 1 ? 0 : 1; + + switch (*c) { + case '^': ++(*y); break; + case 'v': --(*y); break; + case '>': ++(*x); break; + case '<': --(*x); break; + } + set_insert(houses, (void *) new_Point_p(*x, *y)); + ++c; + } + + int sz = (int)houses->size; + set_destroy(houses); + + return sz; +} + +static int part_one(char *input) { + int x = 0, y = 0; + char *c = input; + + Set* houses = malloc(sizeof(Set)); + set_init(houses, same_Point_v, free); + + while (*c != '\0') { + switch (*c) { + case '^': ++y; break; + case 'v': --y; break; + case '>': ++x; break; + case '<': --x; break; + } + set_insert(houses, (void *) new_Point_p(x, y)); + ++c; + } + + int sz = (int)houses->size; + set_destroy(houses); + + return sz; +} void advent2015day03(void) { char *input = get_input("input/2015/03"); - printf("Solution for Day 03 of 2015 is not completed yet\n"); + + printf("%d\n", part_one(input)); + printf("%d\n", part_two(input)); + free(input); } diff --git a/src/advent_utility.c b/src/advent_utility.c new file mode 100644 index 0000000..b78feb0 --- /dev/null +++ b/src/advent_utility.c @@ -0,0 +1,28 @@ +#include + +#include "advent_utility.h" + +struct Point new_Point(int x, int y) { + struct Point p; + p.x = x; + p.y = y; + return p; +} + +struct Point *new_Point_p(int x, int y) { + struct Point *p = malloc(sizeof(struct Point)); + p->x = x; + p->y = y; + return p; +} + +int same_Point(const struct Point* a, const struct Point* b) { + if (a->x == b->x && a->y == b->y) { + return 1; + } + return 0; +} + +int same_Point_v(const void *a, const void *b) { + return same_Point((const struct Point *)a, (const struct Point *)b); +}