diff --git a/include/advent_utility.h b/include/advent_utility.h index d528f60..93c84e7 100644 --- a/include/advent_utility.h +++ b/include/advent_utility.h @@ -1,8 +1,14 @@ #ifndef ADVENT_H_UTILITY_ #define ADVENT_H_UTILITY_ +#include "lfvector.h" + +#define DEFAULT_DELIM " \t\n\r\f\v" + char *md5_str(const char *); char *capture_system(const char *); +Vector *string_to_int_vector(const char *input_string, const char *delim); + #endif diff --git a/lib/libflint b/lib/libflint index 0b5e8b9..0d8d695 160000 --- a/lib/libflint +++ b/lib/libflint @@ -1 +1 @@ -Subproject commit 0b5e8b999a7ae03ac015ce433af13dd4226f5ef4 +Subproject commit 0d8d695be7611181757e10c9d5a5aac59365a056 diff --git a/src/2017/02.c b/src/2017/02.c index e91c9bd..5eaea7a 100644 --- a/src/2017/02.c +++ b/src/2017/02.c @@ -1,3 +1,4 @@ +#include #include #include @@ -6,10 +7,30 @@ void advent2017day02(void) { size_t sz = 0; char **input = get_lines("input/2017/02", &sz); + int p1 = 0, p2 = 0; for (size_t i = 0; i < sz; ++i) { + Vector *v = string_to_int_vector(input[i], DEFAULT_DELIM); + const int *max = vec_max(v, vec_cmp_int); + const int *min = vec_min(v, vec_cmp_int); + p1 += *max - *min; + + for (size_t j = 0; j < vec_len(v); ++j) { + for (size_t k = 0; k < vec_len(v); ++k) { + if (j != k && *(int*)vec_at(v, j) % *(int*)vec_at(v, k) == 0) { + p2 += *(int*)vec_at(v, j) / *(int*)vec_at(v, k); + goto NEXT_LINE; + } + } + } + +NEXT_LINE: + vec_destroy(v); + free(v); } + printf("%d\n%d\n", p1, p2); + free(input); } diff --git a/src/2017/03.c b/src/2017/03.c index df93fdc..7cdd3ec 100644 --- a/src/2017/03.c +++ b/src/2017/03.c @@ -3,8 +3,85 @@ #include "lfinput.h" -void advent2017day03(void) { - char *input = get_input("input/2017/03"); - printf("Solution for Day 03 of 2017 is not completed yet\n"); - free(input); +static int part1(int goal) +{ + int x, y, dx, dy, step, dxtmp; + x = y = dx = step = 0; + dy = -1; + + for (;;) { + step += 1; + if (goal == step) + return abs(x) + abs(y); + + if ((x == y) || ((x < 0) && (x == -y)) || ((x > 0) && (x == 1-y))) { + dxtmp = dx; + dx = -dy; + dy = dxtmp; + } + x += dx; + y += dy; + } +} + +static int part2(int goal) +{ + int x, y, dx, dy, dxtmp, step, total, i, j, tx, ty; + x = y = dx = 0; + dy = -1; + + int array[1000][3]; + int coords[8][2] = {{1, 0}, {1, -1}, {0, -1}, {-1, -1}, {-1, 0}, {-1, 1}, {0, 1}, {1, 1}}; + + for (i = 0; i < 1000; i++) { + array[i][0] = 0; + array[i][1] = 0; + array[i][2] = 0; + } + + for (;;) { + total = 0; + for (i = 0; i < 1000; i++) { + tx = array[i][0]; + ty = array[i][1]; + for (j = 0; j < 8; j++) { + if ((x+coords[j][0] == tx) && (y+coords[j][1] == ty)) + total += array[i][2]; + } + } + if ((x == 0) && (y == 0)) { + array[step][0] = 0; + array[step][1] = 0; + array[step][2] = 1; + } + else { + array[step][0] = x; + array[step][1] = y; + array[step][2] = total; + } + + if (total > goal) { + return total; + } + + step += 1; + + if ((x == y) || ((x < 0) && (x == -y)) || ((x > 0) && (x == 1-y))) { + dxtmp = dx; + dx = -dy; + dy = dxtmp; + } + x += dx; + y += dy; + } +} + +void advent2017day03(void) { + char *input_s = get_input("input/2017/03"); + const int input = atoi(input_s); + + printf("%d\n", part1(input)); + printf("%d\n", part2(input)); + + free(input_s); } diff --git a/src/advent_utility.c b/src/advent_utility.c index 430f3f3..c4b39bb 100644 --- a/src/advent_utility.c +++ b/src/advent_utility.c @@ -44,3 +44,20 @@ char *capture_system(const char *cmd) { fgets(buf, 256, tmp); return buf; } + +Vector *string_to_int_vector(const char *input_string, const char* delim) { + char *copy = strdup(input_string); + char* token = strtok(copy, delim); + Vector *v = malloc(sizeof(Vector)); + vec_init(v, free); + + while (token != NULL) { + int *i = malloc(sizeof(int)); + *i = atoi(token); + vec_push(v, i); + token = strtok(NULL, delim); + } + + free(copy); + return v; +} \ No newline at end of file