fix Point usage

This commit is contained in:
Evan Burkey 2023-11-29 14:03:03 -08:00
parent aa4946f17f
commit 8b61f739bc
6 changed files with 20 additions and 53 deletions

View File

@ -1,19 +1,6 @@
#ifndef ADVENT_H_UTILITY_ #ifndef ADVENT_H_UTILITY_
#define 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 *);
char *md5_str(const char *); char *md5_str(const char *);
char *capture_system(const char *); char *capture_system(const char *);

@ -1 +1 @@
Subproject commit 85536a351dd16df24835283c139aacf91961ab8a Subproject commit 0b5e8b999a7ae03ac015ce433af13dd4226f5ef4

View File

@ -3,7 +3,7 @@
#include "lfset.h" #include "lfset.h"
#include "lfinput.h" #include "lfinput.h"
#include "advent_utility.h" #include "lfutility.h"
static int part_two(char *input) { static int part_two(char *input) {
int sx = 0, sy = 0, rx = 0, ry = 0, is_santa = 1; int sx = 0, sy = 0, rx = 0, ry = 0, is_santa = 1;
@ -11,7 +11,7 @@ static int part_two(char *input) {
char *c = input; char *c = input;
Set *houses = malloc(sizeof(Set)); Set *houses = malloc(sizeof(Set));
set_init(houses, same_Point_v, free); set_init(houses, Point_cmp_v, free);
while (*c != '\0') { while (*c != '\0') {
if (is_santa) { if (is_santa) {
@ -37,11 +37,11 @@ static int part_two(char *input) {
--(*x); --(*x);
break; break;
} }
set_insert(houses, (void *) new_Point_p(*x, *y)); set_insert(houses, Point_new_p(*x, *y));
++c; ++c;
} }
int sz = (int) houses->size; int sz = houses->size;
set_destroy(houses); set_destroy(houses);
return sz; return sz;
@ -52,7 +52,7 @@ static int part_one(char *input) {
char *c = input; char *c = input;
Set *houses = malloc(sizeof(Set)); Set *houses = malloc(sizeof(Set));
set_init(houses, same_Point_v, free); set_init(houses, Point_cmp_v, free);
while (*c != '\0') { while (*c != '\0') {
switch (*c) { switch (*c) {
@ -69,11 +69,11 @@ static int part_one(char *input) {
--x; --x;
break; break;
} }
set_insert(houses, (void *) new_Point_p(x, y)); set_insert(houses, Point_new_p(x, y));
++c; ++c;
} }
int sz = (int) houses->size; int sz = houses->size;
set_destroy(houses); set_destroy(houses);
return sz; return sz;

View File

@ -4,7 +4,12 @@
#include "lfinput.h" #include "lfinput.h"
void advent2017day02(void) { void advent2017day02(void) {
char *input = get_input("input/2017/02"); size_t sz = 0;
printf("Solution for Day 02 of 2017 is not completed yet\n"); char **input = get_lines("input/2017/02", &sz);
for (size_t i = 0; i < sz; ++i) {
}
free(input); free(input);
} }

View File

@ -3,13 +3,13 @@
#include <limits.h> #include <limits.h>
#include "lfinput.h" #include "lfinput.h"
#include "advent_utility.h" #include "lfutility.h"
struct Point parse_floors(char *input) { Point parse_floors(char *input) {
size_t sz = 0; size_t sz = 0;
char **sp = split(input, &sz, "-"); char **sp = split(input, &sz, "-");
const char *errstr; const char *errstr;
struct Point p; Point p;
p.x = strtonum(sp[0], INT_MIN, INT_MAX, &errstr); p.x = strtonum(sp[0], INT_MIN, INT_MAX, &errstr);
p.y = strtonum(sp[1], INT_MIN, INT_MAX, &errstr); p.y = strtonum(sp[1], INT_MIN, INT_MAX, &errstr);
free(sp); free(sp);
@ -24,8 +24,8 @@ void advent2022day04(void) {
for (size_t i = 0; i < sz; ++i) { for (size_t i = 0; i < sz; ++i) {
size_t sp_sz = 0; size_t sp_sz = 0;
char **sp = split(lines[i], &sp_sz, ","); char **sp = split(lines[i], &sp_sz, ",");
struct Point e1 = parse_floors(sp[0]); Point e1 = parse_floors(sp[0]);
struct Point e2 = parse_floors(sp[1]); Point e2 = parse_floors(sp[1]);
if (e1.x <= e2.x && e1.y >= e2.y) { if (e1.x <= e2.x && e1.y >= e2.y) {
++p1; ++p1;

View File

@ -12,31 +12,6 @@
#include "advent_utility.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);
}
char *md5_str(const char *input) { char *md5_str(const char *input) {
unsigned char digest[16]; unsigned char digest[16];