create vec.h
This commit is contained in:
parent
441471ae48
commit
db68cca73e
28
day2.c
28
day2.c
@ -2,36 +2,32 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
#include "aoc.h"
|
#include "aoc.h"
|
||||||
|
#include "vec.h"
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
char you, opponent;
|
char you, opponent;
|
||||||
} Round;
|
} Round;
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
Round *rounds;
|
|
||||||
size_t count;
|
|
||||||
} day2_Data;
|
|
||||||
|
|
||||||
static void *day2_parse(char **lines, int line_count)
|
static void *day2_parse(char **lines, int line_count)
|
||||||
{
|
{
|
||||||
day2_Data *data = calloc(1, sizeof(day2_Data));
|
Vec *vec = vec_malloc(line_count);
|
||||||
data->count = line_count;
|
|
||||||
data->rounds = calloc(line_count, sizeof(Round));
|
|
||||||
|
|
||||||
for (int i = 0; i < line_count; i++) {
|
for (int i = 0; i < line_count; i++) {
|
||||||
data->rounds[i].opponent = lines[i][0];
|
Round *round = malloc(sizeof(Round));
|
||||||
data->rounds[i].you = lines[i][2];
|
round->opponent = lines[i][0];
|
||||||
|
round->you = lines[i][2];
|
||||||
|
vec_push(vec, round);
|
||||||
}
|
}
|
||||||
|
|
||||||
return data;
|
return vec;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int day2_part1(void *p)
|
static int day2_part1(void *p)
|
||||||
{
|
{
|
||||||
day2_Data *data = (day2_Data*)p;
|
Vec *rounds = p;
|
||||||
int result = 0;
|
int result = 0;
|
||||||
for (int i = 0; i < data->count; i++) {
|
for (int i = 0; i < rounds->count; i++) {
|
||||||
Round *round = &data->rounds[i];
|
Round *round = rounds->data[i];
|
||||||
char you = round->you;
|
char you = round->you;
|
||||||
char opponent = round->opponent;
|
char opponent = round->opponent;
|
||||||
|
|
||||||
@ -58,10 +54,10 @@ static int day2_part1(void *p)
|
|||||||
|
|
||||||
static int day2_part2(void *p)
|
static int day2_part2(void *p)
|
||||||
{
|
{
|
||||||
day2_Data *data = (day2_Data*)p;
|
Vec *data = p;
|
||||||
int result = 0;
|
int result = 0;
|
||||||
for (int i = 0; i < data->count; i++) {
|
for (int i = 0; i < data->count; i++) {
|
||||||
Round *round = &data->rounds[i];
|
Round *round = data->data[i];
|
||||||
char you = round->you;
|
char you = round->you;
|
||||||
char opponent = round->opponent;
|
char opponent = round->opponent;
|
||||||
|
|
||||||
|
49
day3.c
49
day3.c
@ -4,28 +4,15 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#include "aoc.h"
|
#include "aoc.h"
|
||||||
|
#include "vec.h"
|
||||||
typedef struct {
|
|
||||||
char *contents;
|
|
||||||
size_t size;
|
|
||||||
} Backpack;
|
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
Backpack *backpacks;
|
|
||||||
size_t count;
|
|
||||||
} day3_Data;
|
|
||||||
|
|
||||||
static void *day3_parse(char **lines, int line_count)
|
static void *day3_parse(char **lines, int line_count)
|
||||||
{
|
{
|
||||||
day3_Data *data = calloc(1, sizeof(day3_Data));
|
Vec *vec = vec_malloc(line_count);
|
||||||
Backpack *backpacks = calloc(line_count, sizeof(Backpack));
|
|
||||||
data->backpacks = backpacks;
|
|
||||||
data->count = line_count;
|
|
||||||
for (size_t i = 0; i < line_count; i++) {
|
for (size_t i = 0; i < line_count; i++) {
|
||||||
backpacks[i].size = strlen(lines[i]);
|
vec_push(vec, lines[i]);
|
||||||
backpacks[i].contents = lines[i];
|
|
||||||
}
|
}
|
||||||
return data;
|
return vec;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool contains(char needle, char* haystack, size_t len)
|
static bool contains(char needle, char* haystack, size_t len)
|
||||||
@ -60,11 +47,12 @@ static int get_priority(char c)
|
|||||||
|
|
||||||
static int day3_part1(void *p)
|
static int day3_part1(void *p)
|
||||||
{
|
{
|
||||||
day3_Data *data = (day3_Data*)p;
|
Vec *vec = p;
|
||||||
int result = 0;
|
int result = 0;
|
||||||
for (size_t i = 0; i < data->count; i++) {
|
for (size_t i = 0; i < vec->count; i++) {
|
||||||
Backpack *b = &data->backpacks[i];
|
char *b = vec->data[i];
|
||||||
char common = find_common(b->contents, b->contents+b->size/2, b->size/2);
|
int size = strlen(b);
|
||||||
|
char common = find_common(b, b + size/2, size/2);
|
||||||
if (common) {
|
if (common) {
|
||||||
result += get_priority(common);
|
result += get_priority(common);
|
||||||
} else {
|
} else {
|
||||||
@ -76,17 +64,20 @@ static int day3_part1(void *p)
|
|||||||
|
|
||||||
static int day3_part2(void *p)
|
static int day3_part2(void *p)
|
||||||
{
|
{
|
||||||
day3_Data *data = (day3_Data*)p;
|
Vec *vec = p;
|
||||||
int result = 0;
|
int result = 0;
|
||||||
for (size_t i = 0; i < data->count; i+=3) {
|
for (size_t i = 0; i < vec->count; i+=3) {
|
||||||
Backpack *b1 = &data->backpacks[i+0];
|
char *b1 = vec->data[i+0];
|
||||||
Backpack *b2 = &data->backpacks[i+1];
|
char *b2 = vec->data[i+1];
|
||||||
Backpack *b3 = &data->backpacks[i+2];
|
char *b3 = vec->data[i+2];
|
||||||
|
int size1 = strlen(b1);
|
||||||
|
int size2 = strlen(b2);
|
||||||
|
int size3 = strlen(b3);
|
||||||
|
|
||||||
bool found = false;
|
bool found = false;
|
||||||
for (size_t j = 0; j < b1->size; j++) {
|
for (size_t j = 0; j < size1; j++) {
|
||||||
char c = b1->contents[j];
|
char c = b1[j];
|
||||||
if (contains(c, b2->contents, b2->size) && contains(c, b3->contents, b3->size)) {
|
if (contains(c, b2, size2) && contains(c, b3, size3)) {
|
||||||
result += get_priority(c);
|
result += get_priority(c);
|
||||||
found = true;
|
found = true;
|
||||||
break;
|
break;
|
||||||
|
35
day4.c
35
day4.c
@ -4,6 +4,7 @@
|
|||||||
#include <sys/param.h>
|
#include <sys/param.h>
|
||||||
|
|
||||||
#include "aoc.h"
|
#include "aoc.h"
|
||||||
|
#include "vec.h"
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
int from, to;
|
int from, to;
|
||||||
@ -14,11 +15,6 @@ typedef struct {
|
|||||||
Range second;
|
Range second;
|
||||||
} DoubleRange;
|
} DoubleRange;
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
DoubleRange *ranges;
|
|
||||||
size_t count;
|
|
||||||
} day4_Data;
|
|
||||||
|
|
||||||
static inline void day4_parse_range(Range *range, char *s)
|
static inline void day4_parse_range(Range *range, char *s)
|
||||||
{
|
{
|
||||||
range->from = atoi(strsep(&s, "-"));
|
range->from = atoi(strsep(&s, "-"));
|
||||||
@ -36,23 +32,23 @@ static void day4_parse_line(DoubleRange *double_range, char *line)
|
|||||||
|
|
||||||
static void *day4_parse(char **lines, int line_count)
|
static void *day4_parse(char **lines, int line_count)
|
||||||
{
|
{
|
||||||
day4_Data *data = calloc(1, sizeof(day4_Data));
|
Vec *vec = vec_malloc(line_count);
|
||||||
DoubleRange *ranges = calloc(line_count, sizeof(DoubleRange));
|
|
||||||
data->ranges = ranges;
|
|
||||||
data->count = line_count;
|
|
||||||
for (size_t i = 0; i < line_count; i++) {
|
for (size_t i = 0; i < line_count; i++) {
|
||||||
day4_parse_line(&data->ranges[i], lines[i]);
|
DoubleRange *double_range = malloc(sizeof(DoubleRange));
|
||||||
|
vec_push(vec, double_range);
|
||||||
|
day4_parse_line(double_range, lines[i]);
|
||||||
}
|
}
|
||||||
return data;
|
return vec;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int day4_part1(void *p)
|
static int day4_part1(void *p)
|
||||||
{
|
{
|
||||||
day4_Data *data = (day4_Data*)p;
|
Vec *vec = p;
|
||||||
int result = 0;
|
int result = 0;
|
||||||
for (int i = 0; i < data->count; i++) {
|
for (int i = 0; i < vec->count; i++) {
|
||||||
Range *range1 = &data->ranges[i].first;
|
DoubleRange *double_range = vec->data[i];
|
||||||
Range *range2 = &data->ranges[i].second;
|
Range *range1 = &double_range->first;
|
||||||
|
Range *range2 = &double_range->second;
|
||||||
if ((range1->from <= range2->from && range1->to >= range2->to) ||
|
if ((range1->from <= range2->from && range1->to >= range2->to) ||
|
||||||
(range2->from <= range1->from && range2->to >= range1->to)) {
|
(range2->from <= range1->from && range2->to >= range1->to)) {
|
||||||
result++;
|
result++;
|
||||||
@ -63,11 +59,12 @@ static int day4_part1(void *p)
|
|||||||
|
|
||||||
static int day4_part2(void *p)
|
static int day4_part2(void *p)
|
||||||
{
|
{
|
||||||
day4_Data *data = (day4_Data*)p;
|
Vec *vec = p;
|
||||||
int result = 0;
|
int result = 0;
|
||||||
for (int i = 0; i < data->count; i++) {
|
for (int i = 0; i < vec->count; i++) {
|
||||||
Range *range1 = &data->ranges[i].first;
|
DoubleRange *double_range = vec->data[i];
|
||||||
Range *range2 = &data->ranges[i].second;
|
Range *range1 = &double_range->first;
|
||||||
|
Range *range2 = &double_range->second;
|
||||||
if ((range1->from <= range2->from && range1->to >= range2->to) ||
|
if ((range1->from <= range2->from && range1->to >= range2->to) ||
|
||||||
(range2->from <= range1->from && range2->to >= range1->to) ||
|
(range2->from <= range1->from && range2->to >= range1->to) ||
|
||||||
MIN(range1->to, range2->to) >= MAX(range1->from, range2->from)) {
|
MIN(range1->to, range2->to) >= MAX(range1->from, range2->from)) {
|
||||||
|
43
vec.h
Normal file
43
vec.h
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
#ifndef VEC_H_
|
||||||
|
#define VEC_H_
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <assert.h>
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
int count;
|
||||||
|
int capacity;
|
||||||
|
void **data;
|
||||||
|
} Vec;
|
||||||
|
|
||||||
|
static inline Vec *vec_malloc(size_t capacity)
|
||||||
|
{
|
||||||
|
Vec *vec = (Vec*)malloc(sizeof(Vec));
|
||||||
|
vec->count = 0;
|
||||||
|
vec->capacity = capacity;
|
||||||
|
vec->data = (void**)malloc(capacity * sizeof(void*));
|
||||||
|
return vec;
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void vec_push(Vec *vec, void* value)
|
||||||
|
{
|
||||||
|
if (vec->count >= vec->capacity) {
|
||||||
|
vec->capacity = (vec->capacity + 1) * 2;
|
||||||
|
vec->data = (void**)realloc(vec->data, vec->capacity);
|
||||||
|
}
|
||||||
|
vec->data[vec->count++] = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void vec_pop(Vec* vec)
|
||||||
|
{
|
||||||
|
vec->count--;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void vec_free(Vec *v)
|
||||||
|
{
|
||||||
|
free(v->data);
|
||||||
|
free(v);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#endif //VEC_H_
|
Loading…
Reference in New Issue
Block a user