Overview
| Comment: | uses dynamic memory |
|---|---|
| Downloads: | Tarball | ZIP archive |
| Timelines: | family | ancestors | descendants | both | trunk |
| Files: | files | file ages | folders |
| SHA3-256: |
c73a56324df86114625fc188a3738f23 |
| User & Date: | nnz on 2024-12-04 18:48:23.513 |
| Other Links: | manifest | tags |
Context
|
2024-12-04
| ||
| 18:53 | added free() check-in: 292a60d81c user: nnz tags: trunk | |
| 18:48 | uses dynamic memory check-in: c73a56324d user: nnz tags: trunk | |
| 18:37 | 201503 2nd star check-in: f5ff8c9552 user: nnz tags: trunk | |
Changes
Modified aoc2015.c
from [e6433b2bdc]
to [1552da6bee].
1 2 3 | #include <stdbool.h> #include <stdio.h> #include <stdlib.h> | < < < < < < | > | | > > > | | > > > | | | | < | > > > | | > > > > < | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include "aocdailies.h"
#include "aocutils.h"
struct House {
int row, col;
};
void aoc201503(char *data, size_t len) {
(void)len; // unused argument
char *data2 = data; // save it
struct House *house = malloc(512 * sizeof *house); // assume it worked
unsigned mhouse = 512, nhouse = 1;
house[0].row = house[0].col = 0; // house at [0, 0] visited
int x = 0, y = 0;
while (*data) {
switch (*data) {
default: break;
case '^': y++; break;
case 'v': y--; break;
case '>': x++; break;
case '<': x--; break;
}
data++;
bool visited = false;
for (size_t k = 0; k < nhouse; k++) {
if ((house[k].row == x) && (house[k].col == y)) {
visited = true;
break;
}
}
if (!visited) {
if (nhouse == mhouse) {
// grow the array (by golden ratio)
struct House *tmp = realloc(house, (13*mhouse) / 8 * sizeof *house);
if (!tmp) {
fprintf(stderr, "no memory. program aborted\n");
exit(EXIT_FAILURE);
}
house = tmp;
mhouse = (13*mhouse) / 8;
}
house[nhouse].row = x;
house[nhouse].col = y;
nhouse += 1;
}
}
printf("Santa delivered at least 1 present to %u houses.\n", nhouse);
// reuse house array for part 2; don't worry about its contents
nhouse = 1; // house at [0, 0] still visited :)
struct House p[2] = {0}; // coordinates of santa and bot
int who = 0; // 0: santa; 1: santa bot
// go around from the beginning
while (*data2) {
switch (*data2) {
default: break;
case '^': p[who].row++; break;
case 'v': p[who].row--; break;
case '>': p[who].col++; break;
case '<': p[who].col--; break;
}
data2++;
bool visited = false;
for (size_t k = 0; k < nhouse; k++) {
if ((house[k].row == p[who].row) && (house[k].col == p[who].col)) {
visited = true;
break;
}
}
if (!visited) {
if (nhouse == mhouse) {
// grow the array (by golden ratio)
struct House *tmp = realloc(house, (13*mhouse) / 8 * sizeof *house);
if (!tmp) {
fprintf(stderr, "no memory. program aborted\n");
exit(EXIT_FAILURE);
}
house = tmp;
mhouse = (13*mhouse) / 8;
}
house[nhouse].row = p[who].row;
house[nhouse].col = p[who].col;
nhouse += 1;
}
who = 1 - who; // santa; bot; santa; bot; ..., ..., ...
}
printf("Santa and santa bot delivered at least 1 present to %u houses.\n", nhouse);
}
void aoc201502(char *data, size_t len) {
(void)len; // unused argument
unsigned sqf = 0, f = 0;
for (;;) {
char *err;
unsigned l = strtoul(data, &err, 10);
|
| ︙ | ︙ |