Skip to content
Snippets Groups Projects
Verified Commit 763b0f57 authored by mirabilos's avatar mirabilos Committed by mirabilos
Browse files

this half was easier than the other…

parent eb860e4f
No related branches found
No related tags found
No related merge requests found
../../i2c/lsp-rnul
\ No newline at end of file
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "inputs.h"
static unsigned char FF[H][W];
#define F(y,x) FF[y][x]
static unsigned long long total = 0;
static void getfield(int y, int x);
int
main(void)
{
int y, x;
memcpy(FF, I, sizeof(I));
for (y = 1; y < H - 1; ++y)
for (x = 1; x < W - 1; ++x)
if (!(F(y, x) & 0x80U))
getfield(y, x);
printf("%llu\n", total);
return (0);
}
static unsigned char oc;
static unsigned char xc;
static unsigned long long area;
static unsigned long long perimeter;
static unsigned int reclvl;
static void
recurse(int y, int x)
{
unsigned char c;
++reclvl;
F(y, x) |= 0x80U;
++area;
#define try(Y,X,dir) do { \
if ((c = F(Y, X)) != oc && c != xc) { \
printf("│%*s" dir " perimeter @ (%d, %d)\n", \
reclvl, "", Y, X); \
++perimeter; \
} else if (c == oc) { \
printf("│%*s" dir " recursing @ (%d, %d)\n", \
reclvl, "", Y, X); \
recurse(Y, X); \
} \
} while (/* CONSTCOND */ 0)
try(y - 1, x, "up");
try(y, x + 1, "right");
try(y + 1, x, "down");
try(y, x - 1, "left");
--reclvl;
}
static void
getfield(int y, int x)
{
oc = F(y, x);
xc = oc | 0x80U;
area = 0;
perimeter = 0;
printf("BEGIN '%c' @ (%d, %d)\n", oc, y, x);
reclvl = 0;
recurse(y, x);
printf("└─END '%c' @ (%d, %d) → area %llu, perimeter %llu\n",
oc, y, x, area, perimeter);
total += area * perimeter;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment