17-02,03
This commit is contained in:
parent
8b61f739bc
commit
525a33963b
@ -1,8 +1,14 @@
|
|||||||
#ifndef ADVENT_H_UTILITY_
|
#ifndef ADVENT_H_UTILITY_
|
||||||
#define ADVENT_H_UTILITY_
|
#define ADVENT_H_UTILITY_
|
||||||
|
|
||||||
|
#include "lfvector.h"
|
||||||
|
|
||||||
|
#define DEFAULT_DELIM " \t\n\r\f\v"
|
||||||
|
|
||||||
char *md5_str(const char *);
|
char *md5_str(const char *);
|
||||||
|
|
||||||
char *capture_system(const char *);
|
char *capture_system(const char *);
|
||||||
|
|
||||||
|
Vector *string_to_int_vector(const char *input_string, const char *delim);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -1 +1 @@
|
|||||||
Subproject commit 0b5e8b999a7ae03ac015ce433af13dd4226f5ef4
|
Subproject commit 0d8d695be7611181757e10c9d5a5aac59365a056
|
@ -1,3 +1,4 @@
|
|||||||
|
#include <advent_utility.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
@ -6,10 +7,30 @@
|
|||||||
void advent2017day02(void) {
|
void advent2017day02(void) {
|
||||||
size_t sz = 0;
|
size_t sz = 0;
|
||||||
char **input = get_lines("input/2017/02", &sz);
|
char **input = get_lines("input/2017/02", &sz);
|
||||||
|
int p1 = 0, p2 = 0;
|
||||||
|
|
||||||
for (size_t i = 0; i < sz; ++i) {
|
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);
|
free(input);
|
||||||
}
|
}
|
||||||
|
@ -3,8 +3,85 @@
|
|||||||
|
|
||||||
#include "lfinput.h"
|
#include "lfinput.h"
|
||||||
|
|
||||||
void advent2017day03(void) {
|
static int part1(int goal)
|
||||||
char *input = get_input("input/2017/03");
|
{
|
||||||
printf("Solution for Day 03 of 2017 is not completed yet\n");
|
int x, y, dx, dy, step, dxtmp;
|
||||||
free(input);
|
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);
|
||||||
}
|
}
|
||||||
|
@ -44,3 +44,20 @@ char *capture_system(const char *cmd) {
|
|||||||
fgets(buf, 256, tmp);
|
fgets(buf, 256, tmp);
|
||||||
return buf;
|
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;
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user