Overview
| Comment: | day 4 |
|---|---|
| Downloads: | Tarball | ZIP archive |
| Timelines: | family | ancestors | descendants | both | trunk |
| Files: | files | file ages | folders |
| SHA3-256: |
53ddcc2ade653fdcc85196398d14eb45 |
| User & Date: | nnz on 2025-12-04 21:34:13.627 |
| Other Links: | manifest | tags |
Context
|
2025-12-04
| ||
| 21:50 | reworked a few comments check-in: 12cb9a19a0 user: nnz tags: trunk | |
| 21:34 | day 4 check-in: 53ddcc2ade user: nnz tags: trunk | |
|
2025-12-03
| ||
| 15:37 | file for 2025 check-in: e75425b15b user: nnz tags: trunk | |
Changes
Modified aoc2025.c
from [6036121579]
to [69ff078f3b].
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | 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 | + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + |
#include <ctype.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "aocdailies.h"
#include "aocutils.h"
#if 0
/* === aocYYYYDD =======================================================
===================================================================== */
void aocYYYYDD(char *data, size_t len) {
(void)len; // unused argument
}
#endif
/* === aoc202504 =======================================================
===================================================================== */
static unsigned evolve(struct RectangularMap *rm, unsigned src) {
unsigned changes = 0;
unsigned dst = 1 - src;
for (int row = 0; row < rm->rows; row++) {
for (int col = 0; col < rm->cols; col++) {
char cc = RMcharat(rm + src, col, row);
char *destin = RMcharptr(rm + dst, col, row);
if (cc == '@') {
unsigned nearby = 0;
for (int deltarow = -1; deltarow <= 1; deltarow++) {
for (int deltacol = -1; deltacol <= 1; deltacol++) {
nearby += (RMcharat(rm + src, col+deltacol,
row+deltarow) == '@');
}
}
nearby -= 1; // remove self
if (nearby < 4) {
*destin = '.';
changes++;
} else {
*destin = '@';
}
} else if (cc != 0) {
*destin = '.';
} else {
*destin = 0; // shouldn't happen
}
}
}
return changes;
}
void aoc202504(char *data, size_t len) {
(void)len; // unused argument
struct RectangularMap rm[2] = {0};
while (*data) {
char *data2 = data;
while (*data != '\n') data++;
*data = 0; // erase newline
RMaddline(rm + 0, data2);
RMaddline(rm + 1, data2); // set both rm's to the same thing
*data++ = '\n'; // unerase newline and skip it
}
unsigned part1 = evolve(rm, 0); // evolve from rm[0] to rm[1]
unsigned part2 = part1, src = 1;
for (;;) {
unsigned tmp = evolve(rm, src); // keep evolving
if (tmp == 0) break; // STOP
src = 1 - src; // back and forth
part2 += tmp; // add changes in this round
}
printf("P1: %u; P2: %u\n", part1, part2);
RMfree(rm);
RMfree(rm + 1);
}
/* === aoc202503 =======================================================
===================================================================== */
static long long unsigned maxj(const unsigned char *b, unsigned blen, unsigned nlen) {
if (nlen == 0) return 0;
const unsigned char *newb = b + 1;
unsigned newlen = blen - 1;
|
| ︙ |
Modified aocdailies.c
from [405856050f]
to [d8f37a577d].
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | + |
#include <stddef.h>
#include "aocdailies.h"
aocfunc *aocselect(unsigned y, unsigned d) {
aocfunc *p;
switch (y * 100 + d) {
default: p = NULL; break;
case 202504: p = aoc202504; break;
case 202503: p = aoc202503; break;
case 202502: p = aoc202502; break;
case 202501: p = aoc202501; break;
// YYYYdd ==> aocYYYYdd
case 202422: p = aoc202422; break;
case 202417: p = aoc202417; break;
|
| ︙ |
Modified aocdailies.h
from [bba21221a3]
to [39dda2d12a].
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | + | #ifndef AOCDAILIES_H_INCLUDED #define AOCDAILIES_H_INCLUDED #include <stddef.h> typedef void aocfunc(char *, size_t); aocfunc *aocselect(unsigned, unsigned); aocfunc aoc202504; aocfunc aoc202503; aocfunc aoc202502; aocfunc aoc202501; aocfunc aoc202422; aocfunc aoc202417; aocfunc aoc202409; |
| ︙ |
Modified aocutils.c
from [2fd9fa9d99]
to [9332948c9c].
1 2 3 4 5 6 7 8 9 10 11 12 13 | 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 | + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + |
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "aocutils.h"
char RMcharat(struct RectangularMap *sm, int col, int row) {
if (col < 0) return 0;
if (row < 0) return 0;
if (col >= sm->cols) return 0;
if (row >= sm->rows) return 0;
return sm->data[row*sm->cols + col];
}
char *RMcharptr(struct RectangularMap *sm, int col, int row) {
if (col < 0) return NULL;
if (row < 0) return NULL;
if (col >= sm->cols) return NULL;
if (row >= sm->rows) return NULL;
return sm->data + row*sm->cols + col;
}
void RMcopy(struct RectangularMap *restrict dst,
const struct RectangularMap *restrict src) {
size_t size = (size_t)src->cols * (size_t)src->rows;
char *tmp = realloc(dst->data, size);
if (tmp == NULL) {
fprintf(stderr, "not enough memory\n");
exit(EXIT_FAILURE);
}
dst->data = tmp;
memcpy(dst->data, src->data, size);
}
void RMaddline(struct RectangularMap *sm, const char *lin) {
int linlen = strlen(lin);
if (sm->cols && (sm->cols != linlen)) {
// error
fprintf(stderr, "wrong line length!\n");
exit(EXIT_FAILURE);
}
if (sm->cols == 0) sm->cols = linlen;
char *tmp = realloc(sm->data, (size_t)(sm->rows+1) * (size_t)sm->cols);
if (tmp == NULL) {
fprintf(stderr, "not enough memory\n");
exit(EXIT_FAILURE);
}
sm->data = tmp;
memcpy(sm->data + sm->rows*sm->cols, lin, (size_t)sm->cols);
sm->rows += 1;
}
void RMfree(struct RectangularMap *sm) {
free(sm->data);
sm->data = NULL;
sm->cols = sm->rows = 0;
}
static uint32_t f(uint32_t x, uint32_t y, uint32_t z) {
return (x & y) | (~x & z);
}
static uint32_t g(uint32_t x, uint32_t y, uint32_t z) {
return (x & z) | (y & ~z);
|
| ︙ |
Modified aocutils.h
from [a0c4f0a95c]
to [1f637ca7e2].
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | 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 | + + + + + + + + + + + |
#ifndef AOCUTILS_H_INCLUDED
#define AOCUTILS_H_INCLUDED
#include <stdbool.h>
struct TextGrid {
unsigned cols, rows;
char *data; // may have '\n' at end of rows
};
struct RectangularMap {
int cols, rows;
char *data;
};
char RMcharat(struct RectangularMap *sm, int col, int row);
char *RMcharptr(struct RectangularMap *sm, int col, int row);
void RMcopy(struct RectangularMap *restrict dst,
const struct RectangularMap *restrict src);
void RMaddline(struct RectangularMap *sm, const char *lin);
void RMfree(struct RectangularMap *sm);
long long unsigned upow(unsigned base, unsigned exponent);
void md5mini(unsigned char *dstarr, const char *src);
bool TGvalid(struct TextGrid *tg, unsigned col, unsigned row);
char *TGcharptr(struct TextGrid *tg, unsigned col, unsigned row);
unsigned TGcol(struct TextGrid *tg, char *p);
|
| ︙ |