2015-03. Add advent_utility and libflint

This commit is contained in:
2021-09-02 15:25:09 -07:00
parent e134e22224
commit dc8e8a27fe
8 changed files with 146 additions and 15 deletions

View File

@ -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 = &rx;
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
View 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);
}