Browse Source

day3p2

master
mid-kid 6 years ago
parent
commit
9a688e7716
  1. 12
      day3p2/Makefile
  2. 1331
      day3p2/input
  3. BIN
      day3p2/main
  4. 97
      day3p2/main.c

12
day3p2/Makefile

@ -0,0 +1,12 @@
CFLAGS := -Wall -Wextra -std=c17 -D_GNU_SOURCE
LIBS := glib-2.0
CFLAGS += $(shell pkg-config --cflags $(LIBS))
LDLIBS := $(shell pkg-config --libs $(LIBS))
.PHONY: all
all: main
.PHONY: clean
clean:
rm -f main

1331
day3p2/input

File diff suppressed because it is too large

BIN
day3p2/main

Binary file not shown.

97
day3p2/main.c

@ -0,0 +1,97 @@
#include <stdio.h>
#include <glib.h>
struct claim {
unsigned id;
unsigned x;
unsigned y;
unsigned w;
unsigned h;
};
struct claim *parseclaims(unsigned *len, const char *fname)
{
FILE *f = fopen(fname, "r");
if (!f) {
perror(fname);
return NULL;
}
GArray *array = g_array_new(FALSE, FALSE, sizeof(struct claim));
*len = 0;
char *line = NULL;
size_t line_len = 0;
while (getline(&line, &line_len, f) != -1) {
struct claim claim = {0};
// Haha what is proper parsing
sscanf(line, "#%u @ %u,%u: %ux%u",
&claim.id,
&claim.x,
&claim.y,
&claim.w,
&claim.h
);
// Basic sanity check
if (claim.y > 1000 ||
claim.y + claim.h > 1000 ||
claim.x > 1000 ||
claim.x + claim.w > 1000) {
fprintf(stderr, "Warning: Ignoring out-of-bounds claim: "
"#%u @ %u,%u: %ux%u\n",
claim.id,
claim.x,
claim.y,
claim.w,
claim.h
);
continue;
}
g_array_append_val(array, claim);
(*len)++;
}
free(line);
fclose(f);
struct claim *res = (struct claim *)g_array_free(array, FALSE);
return res;
}
int main()
{
unsigned claims_len;
struct claim *claims = parseclaims(&claims_len, "input");
if (!claims) return 1;
char (*tilemap)[1000] = g_malloc0(sizeof(char[1000][1000]));
for (struct claim *claim = claims;
claim < claims + claims_len; claim++) {
for (unsigned y = claim->y; y < claim->y + claim->h; y++) {
for (unsigned x = claim->x; x < claim->x + claim->w; x++) {
if (tilemap[y][x] >= 2) continue;
tilemap[y][x]++;
}
}
}
for (struct claim *claim = claims;
claim < claims + claims_len; claim++) {
gboolean overlap = FALSE;
for (unsigned y = claim->y; y < claim->y + claim->h && !overlap; y++) {
for (unsigned x = claim->x; x < claim->x + claim->w && !overlap; x++) {
overlap = (tilemap[y][x] >= 2);
}
}
if (!overlap) printf("%u\n", claim->id);
}
g_free(claims);
g_free(tilemap);
return 0;
}
Loading…
Cancel
Save