~ knotless Search/
↑↓ move↵ open⇧↵ all results

spaces.cameron.stream/hello

Hello.

0
1
main
179 lines · 4.1 KB Raw History
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "records.h"
#include "town.h"

/* Streets open and close. An epoch is a stretch of days with the same streets. */
#define MAX_EPOCHS 16

static char names[MAX_PLACES][FIELD];
static int places;
static int changes[MAX_EPOCHS]; /* Day numbers on which a street opened or closed, in order. */
static int epochs;
static int built;

static struct {
	int built;
	int metres[MAX_PLACES][MAX_PLACES]; /* 0 where no open street joins two places. */
	int known[MAX_PLACES][MAX_PLACES]; /* Walks already measured, plus one. */
} era[MAX_EPOCHS + 1];

static int find(const char *name)
{
	for (int i = 0; i < places; i++)
		if (strcmp(names[i], name) == 0)
			return i;
	return -1;
}

static int add(const char *name)
{
	int i = find(name);
	if (i >= 0)
		return i;
	if (places == MAX_PLACES) {
		fprintf(stderr, "the town has no room for %s\n", name);
		exit(1);
	}
	snprintf(names[places], FIELD, "%s", name);
	return places++;
}

static void change(const char *date)
{
	if (strcmp(date, "-") == 0)
		return;
	int when = day_number(parse_day(date));
	for (int i = 0; i < epochs; i++)
		if (changes[i] == when)
			return;
	if (epochs == MAX_EPOCHS) {
		fprintf(stderr, "the streets have changed more than %d times\n", MAX_EPOCHS);
		exit(1);
	}
	int i = epochs++;
	for (; i > 0 && changes[i - 1] > when; i--)
		changes[i] = changes[i - 1];
	changes[i] = when;
}

static void build(void)
{
	if (built)
		return;
	const struct record *streets = read_record("streets");
	for (int row = 0; row < streets->rows; row++) {
		add(streets->field[row][1]);
		add(streets->field[row][2]);
		change(streets->field[row][4]);
		change(streets->field[row][5]);
	}
	built = 1;
}

/* Which epoch a day is in, with its streets laid out. */
static int epoch(struct day day)
{
	build();
	int today = day_number(day), e = 0;
	while (e < epochs && changes[e] <= today)
		e++;
	if (!era[e].built) {
		const struct record *streets = read_record("streets");
		for (int row = 0; row < streets->rows; row++) {
			const char(*f)[FIELD] = streets->field[row];
			if (!in_force(f[4], f[5], day))
				continue;
			int a = find(f[1]), b = find(f[2]);
			era[e].metres[a][b] = era[e].metres[b][a] = atoi(f[3]);
		}
		era[e].built = 1;
	}
	return e;
}

static int place(const char *name)
{
	build();
	int i = find(name);
	if (i < 0) {
		fprintf(stderr, "there is no %s\n", name);
		exit(1);
	}
	return i;
}

static int measure(int e, int from, int to)
{
	int best[MAX_PLACES], done[MAX_PLACES] = {0};
	for (int i = 0; i < places; i++)
		best[i] = INT_MAX;
	best[from] = 0;

	for (;;) {
		int here = -1;
		for (int i = 0; i < places; i++)
			if (!done[i] && best[i] != INT_MAX && (here < 0 || best[i] < best[here]))
				here = i;
		if (here < 0)
			break;
		if (here == to)
			return best[here];
		done[here] = 1;
		for (int next = 0; next < places; next++) {
			int length = era[e].metres[here][next];
			if (length && best[here] + length < best[next])
				best[next] = best[here] + length;
		}
	}
	fprintf(stderr, "no way from %s to %s\n", names[from], names[to]);
	exit(1);
}

int distance(struct day day, const char *start, const char *end)
{
	int e = epoch(day), from = place(start), to = place(end);
	if (!era[e].known[from][to])
		era[e].known[from][to] = era[e].known[to][from] = measure(e, from, to) + 1;
	return era[e].known[from][to] - 1;
}

static int by_name(const void *a, const void *b)
{
	return strcmp(*(const char *const *)a, *(const char *const *)b);
}

int every_place(const char *out[])
{
	build();
	for (int i = 0; i < places; i++)
		out[i] = names[i];
	qsort(out, (size_t)places, sizeof out[0], by_name);
	return places;
}

const char *nearest(struct day day, const char *start, const char *const candidates[], int n)
{
	const char *best = NULL;
	int best_metres = INT_MAX;
	for (int i = 0; i < n; i++) {
		int m = distance(day, start, candidates[i]);
		if (m < best_metres) {
			best = candidates[i];
			best_metres = m;
		}
	}
	return best;
}

int north(const char *name)
{
	const struct record *across = read_record("north");
	for (int row = 0; row < across->rows; row++)
		if (strcmp(across->field[row][0], name) == 0)
			return 1;
	return 0;
}