2015-03. Add advent_utility and libflint
This commit is contained in:
parent
e134e22224
commit
dc8e8a27fe
3
.gitmodules
vendored
Normal file
3
.gitmodules
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
[submodule "lib/libflint"]
|
||||
path = lib/libflint
|
||||
url = git@git.fputs.com:fputs/libflint
|
38
.idea/workspace.xml
generated
38
.idea/workspace.xml
generated
@ -1,8 +1,12 @@
|
||||
<?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">
|
||||
@ -13,16 +17,13 @@
|
||||
</component>
|
||||
<component name="ChangeListManager">
|
||||
<list default="true" id="b07dea61-9ce6-4bea-9c31-27e5f3826c53" name="Changes" comment="">
|
||||
<change afterPath="$PROJECT_DIR$/.idea/advent.iml" afterDir="false" />
|
||||
<change afterPath="$PROJECT_DIR$/.idea/misc.xml" afterDir="false" />
|
||||
<change afterPath="$PROJECT_DIR$/.idea/modules.xml" afterDir="false" />
|
||||
<change afterPath="$PROJECT_DIR$/.idea/vcs.xml" afterDir="false" />
|
||||
<change afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
|
||||
<change afterPath="$PROJECT_DIR$/CMakeLists.txt" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/.gitignore" beforeDir="false" afterPath="$PROJECT_DIR$/.gitignore" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/GNUmakefile" beforeDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/Makefile" beforeDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/README.md" beforeDir="false" afterPath="$PROJECT_DIR$/README.md" afterDir="false" />
|
||||
<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" />
|
||||
@ -56,9 +57,13 @@
|
||||
<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">
|
||||
<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" />
|
||||
@ -74,11 +79,20 @@
|
||||
<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">
|
||||
@ -88,7 +102,7 @@
|
||||
<option name="number" value="Default" />
|
||||
<option name="presentableId" value="Default" />
|
||||
<updated>1630619869539</updated>
|
||||
<workItem from="1630619872068" duration="346000" />
|
||||
<workItem from="1630619872068" duration="1587000" />
|
||||
</task>
|
||||
<servers />
|
||||
</component>
|
||||
|
@ -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)
|
||||
target_link_libraries(advent PRIVATE bsd flint)
|
||||
target_include_directories(advent PRIVATE include lib/libflint/include)
|
@ -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:
|
||||
|
14
include/advent_utility.h
Normal file
14
include/advent_utility.h
Normal file
@ -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
|
1
lib/libflint
Submodule
1
lib/libflint
Submodule
@ -0,0 +1 @@
|
||||
Subproject commit d5643646860cdc2e0401b63094408753893e3ae0
|
@ -1,10 +1,74 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#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);
|
||||
}
|
||||
|
28
src/advent_utility.c
Normal file
28
src/advent_utility.c
Normal file
@ -0,0 +1,28 @@
|
||||
#include <stdlib.h>
|
||||
|
||||
#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);
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user