Browse Source

2015-05

master
Evan Burkey 1 month ago
parent
commit
ba0b0df515
  1. 11
      include/advent_utility.h
  2. 14
      src/2015/05.c
  3. 53
      src/advent_utility.c

11
include/advent_utility.h

@ -7,10 +7,15 @@ struct Point {
};
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*);
char *md5_str(const char*);
int same_Point(const struct Point *, const struct Point *);
int same_Point_v(const void *, const void *);
char *md5_str(const char *);
char *capture_system(const char *);
#endif

14
src/2015/05.c

@ -1,10 +1,16 @@
#include <stdio.h>
#include <stdlib.h>
#include "input.h"
#include "advent_utility.h"
void advent2015day05(void) {
char *input = get_input("input/2015/05");
printf("Solution for Day 05 of 2015 is not completed yet\n");
free(input);
/* Sometimes, grep really is the best tool */
char *p1 = capture_system(
"grep -E \"(.*[aeiou]){3}\" ./input/2015/05 | grep \"\\(.\\)\\1\" | egrep -v \"(ab|cd|pq|xy)\" | wc -l");
char *p2 = capture_system("grep \"\\(..\\).*\\1\" ./input/2015/05 | grep \"\\(.\\).\\1\" | wc -l");
printf("%s%s", p1, p2);
free(p1);
free(p2);
}

53
src/advent_utility.c

@ -1,7 +1,14 @@
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#ifdef __linux__
#include <openssl/md5.h>
#else
#include <md5.h>
#endif
#include "advent_utility.h"
@ -19,7 +26,7 @@ struct Point *new_Point_p(int x, int y) {
return p;
}
int same_Point(const struct Point* a, const struct Point* b) {
int same_Point(const struct Point *a, const struct Point *b) {
if (a->x == b->x && a->y == b->y) {
return 1;
}
@ -27,18 +34,38 @@ int same_Point(const struct Point* a, const struct Point* b) {
}
int same_Point_v(const void *a, const void *b) {
return same_Point((const struct Point *)a, (const struct Point *)b);
return same_Point((const struct Point *) a, (const struct Point *) b);
}
char *md5_str(const char* input) {
unsigned char digest[16];
struct MD5Context context;
MD5Init(&context);
MD5Update(&context, input, strlen(input));
MD5Final(digest, &context);
char *md5string = malloc(33);
for(int i = 0; i < 16; ++i) {
sprintf(&md5string[i*2], "%02x", (unsigned int)digest[i]);
}
return md5string;
char *md5_str(const char *input) {
unsigned char digest[16];
#ifdef __linux__
MD5_CTX context;
MD5_Init(&context);
MD5_Update(&context, input, strlen(input));
MD5_Final(digest, &context);
#else
struct MD5Context context;
MD5Init(&context);
MD5Update(&context, input, strlen(input));
MD5Final(digest, &context);
#endif
char *md5string = malloc(33);
for (int i = 0; i < 16; ++i) {
sprintf(&md5string[i * 2], "%02x", (unsigned int) digest[i]);
}
return md5string;
}
char *capture_system(const char *cmd) {
char *buf = malloc(256);
FILE *tmp = popen(cmd, "r");
if (tmp == NULL) {
printf("failed to open FILE *tmp\n");
exit(1);
}
fgets(buf, 256, tmp);
return buf;
}
Loading…
Cancel
Save