/* The Society's planning. * * The town is founded once, as it would be: every founder's habits, house and * clock decided by chance, and nothing tuned. The Society keeps the dead letter * office, so it has read everything the town has ever written to the World, and * it knows what must arrive on the census day. It lets each line live, looks at * what it would post, and plans the least it must do: * * it sees to the naming of a child, at the christening; * it finds the household a house, after the line's last move; * it has a word with them, a few minutes at a time, so they get up later or earlier; * it puts their clock wrong by a minute or two, after the bell last put it right. * * All but the naming must be done by the keeper of the office, on foot. When she * lives too far from a household, the Society finds her a house nearer first, * and makes sure that moving her has not put anyone it has already seen to out * of her reach. * * Lines do not affect one another, except through the keeper, so each is planned * with her alone. Then every first of the month from 1900 to 2200 is surveyed, to * make sure the town says hello on the census day and on no other. * * Writes records/founding.txt, records/institutions.txt, records/almanac.txt and * records/society.txt. */ #include #include #include #include #include "census.h" #include "fate.h" #include "history.h" #include "post.h" #include "records.h" #include "town.h" #include "weather.h" #define CHANCE 19000101u /* What decided the founding. */ #define ATTEMPTS 24 #define TRIES 24 #define MOVES 6 /* The most times the keeper will move house for one line. */ #define WORD 15 /* The most anyone will change their habits for one word. */ #define MONTHS (301 * 12) struct wanted { const char *line; enum kind kind; const char *name; const char *home; /* Institutions. */ const char *sends; /* Institutions. */ const char *since; /* Institutions. Residents' lines are as old as the town. */ const char *box; /* NULL for the keeper, who does not post. */ const char *posted; const char *body; }; static const struct wanted wanted[] = { {"G1", RESIDENT, "Gwen", 0, 0, 0, NULL, NULL, NULL}, {"H1", RESIDENT, "Hazel", 0, 0, 0, "Hill", "06:35", "H"}, {"O1", RESIDENT, "Oscar", 0, 0, 0, "Green", "06:55", "o"}, {"L1", RESIDENT, "Lottie", 0, 0, 0, "Green", "07:03", "l"}, {"L2", RESIDENT, "Lionel", 0, 0, 0, "Green", "07:13", "l"}, {"E1", RESIDENT, "Edith", 0, 0, 0, "Green", "07:20", "e"}, {"D1", RESIDENT, "Dora", 0, 0, 0, "Market", "07:42", "d"}, {"L3", RESIDENT, "Lena", 0, 0, 0, "Market", "07:50", "l"}, {"R1", RESIDENT, "Rosa", 0, 0, 0, "Market", "07:52", "r"}, {"W1", RESIDENT, "Walter", 0, 0, 0, "Market", "07:58", "W"}, {"O2", RESIDENT, "Olive", 0, 0, 0, "Market", "07:55", "o"}, {"School", INSTITUTION, "School", "School", " ", "1900-01-01", "Green", "07:45", " "}, {CHURCH, BELL, CHURCH, "Church", ",", "1900-01-01", "Green", "07:53", ","}, {"Fire Station", INSTITUTION, "Fire Station", "Fire Station", "!", "1912-03-01", "Green", "08:12", "!"}, {"G2", RESIDENT, "Gwen", 0, 0, 0, NULL, NULL, NULL}, }; #define WANTED (int)(sizeof wanted / sizeof wanted[0]) #define KEEPER 0 #define DEPUTY (WANTED - 1) static long asked; static unsigned roll(unsigned sides) { return fate(CHANCE, asked++, 0, sides); } static uint64_t roll_seed(void) { uint64_t high = roll(0x80000000u), low = roll(0x80000000u); return high << 31 ^ low; } static int later(int a, int b) { return a > b ? a : b; } static struct day founded, census; static struct town town; static void found_by_chance(const struct wanted *w, struct household *h) { memset(h, 0, sizeof *h); h->kind = w->kind; snprintf(h->line, sizeof h->line, "%s", w->line); h->since = h->set = w->since ? parse_day(w->since) : founded; h->head_since = month_of(h->since); h->member = w->line == wanted[KEEPER].line || w->line == wanted[DEPUTY].line; snprintf(h->name, FIELD, "%s", w->name); if (w->kind == BELL) { /* The church leaves at ten to eight, as it always has. */ snprintf(h->home, FIELD, "%s", w->home); snprintf(h->sends, sizeof h->sends, "%s", w->sends); h->weekdays = h->weekends = clock_time("07:50"); return; } h->seed = roll_seed(); h->gains = (int)roll(21) - 10; if (w->kind == RESIDENT) { const char *names[8], *places[MAX_PLACES]; h->initial = w->line[0]; snprintf(h->name, FIELD, "%s", names[roll((unsigned)names_for(h->initial, names))]); snprintf(h->home, FIELD, "%s", places[roll((unsigned)homes(places))]); h->weekdays = 5 * 60 + 30 + (int)roll(241); h->weekends = h->weekdays + (int)roll(91); h->writes = 10 + (int)roll(31); } else { snprintf(h->home, FIELD, "%s", w->home); snprintf(h->sends, sizeof h->sends, "%s", w->sends); h->weekdays = h->weekends = 7 * 60 + (int)roll(121); } } static struct act planned[MAX_ACTS]; static int acts; static void plan_act(int month, const char *line, enum deed deed, const char *what, int minutes) { if (acts == MAX_ACTS) { fprintf(stderr, "the Society has planned too much\n"); exit(1); } struct act *a = &planned[acts++]; memset(a, 0, sizeof *a); a->month = month; snprintf(a->line, sizeof a->line, "%s", line); a->deed = deed; snprintf(a->what, FIELD, "%s", what); a->minutes = minutes; } /* Forget a line's planned acts of one kind. */ static void forget(const char *line, enum deed deed) { int kept = 0; for (int i = 0; i < acts; i++) if (strcmp(planned[i].line, line) != 0 || planned[i].deed != deed) planned[kept++] = planned[i]; acts = kept; } /* What happened to a line before the census month, as it last lived. -1 if it never did. */ static struct { int succeeded, moved, rung; /* Its last child, its last move, its clock last set by the bell. */ int unreached; /* Acts the keeper could not get to. */ char home[MONTHS][FIELD]; /* Where it lived at the end of each month, before the Society acted. */ char keeper[MONTHS][FIELD]; /* Where the keeper lived then, once the Society had seen to her. */ } story; static void listen(struct day day, const struct household *h, const char *what) { (void)day; (void)h; if (strstr(what, "could not reach")) story.unreached++; } /* Live a line and the keeper together to the census day, under the planned acts. */ static void live_with_keeper(int line, struct household *then) { struct town pair = {.month = month_of(founded), .lines = line == KEEPER ? 1 : 2}; pair.line[0] = town.line[line]; pair.line[1] = town.line[KEEPER]; set_society(planned, acts); story.succeeded = story.moved = story.rung = -1; story.unreached = 0; chronicle = listen; for (; pair.month < month_of(census); pair.month++) { int month = pair.month; struct household before = pair.line[0]; for (int i = 0; i < pair.lines; i++) live(&pair.line[i], month); const struct household *lived = &pair.line[0]; if (lived->generation != before.generation) story.succeeded = month; if (moves(&before, month)) story.moved = month; /* Even into the house they had: a Society house would not survive it. */ if (!same_day(lived->set, before.set)) story.rung = month; snprintf(story.home[month], FIELD, "%s", lived->home); carry_out(&pair, month); snprintf(story.keeper[month], FIELD, "%s", pair.line[pair.lines - 1].home); } chronicle = NULL; *then = pair.line[0]; } static int reachable(int month) { return distance(month_day(month), story.keeper[month], story.home[month]) <= REACH; } /* The months from first to last when the keeper could get to the line, latest first. * Not the month it is housed: the house is seen to first, and the story knows only the old one. */ static int within_reach(int first, int last, int housed, int out[]) { int n = 0; for (int month = last; month >= first; month--) if (month != housed && reachable(month)) out[n++] = month; return n; } static const char *box_for(const char *home) { static struct visit today[MAX_DAILY]; const char *boxes[MAX_DAILY]; int n = visits(census, today), count = 0; for (int i = 0; i < n; i++) { int seen = 0; for (int j = 0; j < count; j++) seen |= strcmp(boxes[j], today[i].box) == 0; if (!seen) boxes[count++] = today[i].box; } /* As post() chooses: the nearest, ties going alphabetically. */ for (int i = 1; i < count; i++) for (int j = i; j > 0 && strcmp(boxes[j - 1], boxes[j]) > 0; j--) { const char *swap = boxes[j]; boxes[j] = boxes[j - 1]; boxes[j - 1] = swap; } return nearest(census, home, boxes, count); } /* When the letter would be posted on the census day, whether or not a van comes. */ static int posting(const struct household *h) { struct sender s = sender_of(h, census); int walk = conditions(census).walk; return s.ready + (distance(census, h->home, box_for(h->home)) + walk - 1) / walk; } static int suits(const struct wanted *w, const char *home) { return strcmp(box_for(home), w->box) == 0 && north(home) == (isupper((unsigned char)w->body[0]) != 0); } static int satisfied(const struct wanted *w, const struct household *then) { if (story.unreached || (w->kind == RESIDENT && strcmp(then->name, w->name) != 0)) return 0; if (!w->box) return 1; struct sender s = sender_of(then, census); struct letter l; return post(&s, census, &l) && strcmp(l.visit.box, w->box) == 0 && l.posted == clock_time(w->posted) && strcmp(s.body, w->body) == 0; } /* Whether every line before this one is still as the Society wants it. */ static int still_right(int line) { for (int i = 0; i < line; i++) { struct household then; live_with_keeper(i, &then); if (!satisfied(&wanted[i], &then)) return 0; } return 1; } /* Find the keeper a house within reach of a line, some month from first to last. The month, or -1. */ static int bring_keeper(int line, int first, int last) { if (first > last) return -1; for (int tries = 0; tries < TRIES; tries++) { struct household then; live_with_keeper(line, &then); int month = first + (int)roll((unsigned)(last - first + 1)); const char *places[MAX_PLACES], *near[MAX_PLACES]; int n = homes(places), count = 0; for (int i = 0; i < n; i++) if (distance(month_day(month), places[i], story.home[month]) <= REACH) near[count++] = places[i]; if (!count) continue; plan_act(month, wanted[KEEPER].line, HOUSES, near[roll((unsigned)count)], 0); if (still_right(line)) return month; acts--; } return -1; } static int plan(int line) { const struct wanted *w = &wanted[line]; struct household then; int first = month_of(town.line[line].since), last = month_of(census) - 1; live_with_keeper(line, &then); if (w->kind == RESIDENT && strcmp(then.name, w->name) != 0) { if (story.succeeded < 0) { printf(" %s: nobody has died since the founding, so there has been no christening\n", w->line); return 0; } plan_act(story.succeeded, w->line, NAMES, w->name, 0); live_with_keeper(line, &then); } if (w->kind == BELL || !w->box) return satisfied(w, &then); int housed = -1; if (w->kind == RESIDENT && !suits(w, then.home)) { int from = later(story.moved, first) + 1; int until = from <= last - 12 ? last - 12 : last; /* A year to see to the rest, if there is one. */ int months[MONTHS], n = within_reach(from, until, -1, months); housed = n ? months[roll((unsigned)n)] : bring_keeper(line, from, until); if (housed < 0) { printf(" %s: the keeper cannot get to them to find them a house\n", w->line); return 0; } const char *places[MAX_PLACES], *good[MAX_PLACES]; int count = 0; n = homes(places); for (int i = 0; i < n; i++) if (suits(w, places[i])) good[count++] = places[i]; plan_act(housed, w->line, HOUSES, good[roll((unsigned)count)], 0); } /* Then words, in fives and no more than WORD at a time, after the child last took the house, * and the clock set by what is left, after the bell last put it right. Habits go with * people when they move, so words may come before the house or after it. */ int sooner = 0, moves = 0; for (int attempt = 0;; attempt++) { forget(w->line, WORDS); forget(w->line, CLOCKS); live_with_keeper(line, &then); int fives = 5 * ((sooner >= 0 ? sooner + 2 : sooner - 2) / 5), clock = sooner - fives; int words = (abs(fives) + WORD - 1) / WORD; int talk[MONTHS], set[MONTHS]; int from = later(story.succeeded, first) + 1; int talks = within_reach(from, last, housed, talk); int sets = within_reach(later(later(story.rung, story.succeeded), first), last, housed, set); if (talks < 2 * words || (clock && !sets)) { if (++moves > MOVES || bring_keeper(line, from, last) < 0) { printf(" %s: the keeper cannot get to them often enough\n", w->line); return 0; } continue; } /* Words a month apart at least, the last of them latest. */ for (int i = 0, left = -fives; i < words; i++) { int word = abs(left) > WORD ? (left > 0 ? WORD : -WORD) : left; plan_act(talk[2 * i], w->line, WORDS, "", word); left -= word; } if (clock) plan_act(set[0], w->line, CLOCKS, "", clock); /* A clock that much faster gets them out that much sooner. */ live_with_keeper(line, &then); if (satisfied(w, &then)) return 1; if (attempt == ATTEMPTS) { printf(" %s: they will not come right\n", w->line); return 0; } sooner += posting(&then) - clock_time(w->posted); } } static void hhmm(FILE *out, int minutes) { fprintf(out, "%02d:%02d", minutes / 60, minutes % 60); } static int in_order(const void *a, const void *b) { const struct act *x = a, *y = b; if (x->month != y->month) return x->month - y->month; int line = strcmp(x->line, y->line); return line ? line : (int)x->deed - (int)y->deed; } static void write_records(uint64_t almanac) { FILE *out = fopen(RECORDS "/founding.txt", "w"); fprintf(out, "# The town as founded, on 1 January 1900. Chance decided all of it.\n"); fprintf(out, "# Line | first of the line | lives at | wakes on weekdays | wakes at weekends" " | minutes writing | seconds the clock gains a day | seed\n"); for (int i = 0; i < town.lines; i++) { const struct household *h = &town.line[i]; if (h->kind != RESIDENT) continue; fprintf(out, "%s | %s | %s | ", h->line, h->name, h->home); hhmm(out, h->weekdays); fputs(" | ", out); hhmm(out, h->weekends); fprintf(out, " | %d | %d | %llu\n", h->writes, h->gains, (unsigned long long)h->seed); } fclose(out); out = fopen(RECORDS "/institutions.txt", "w"); fprintf(out, "# Institutions, as founded. Name | at | founded | leaves to post, by its own clock | sends" " | seconds the clock gains a day | seed\n"); fprintf(out, "# The church clock is the bell. It is always right, and the church does not change.\n"); for (int i = 0; i < town.lines; i++) { const struct household *h = &town.line[i]; if (h->kind == RESIDENT) continue; fprintf(out, "%s | %s | %04d-%02d-%02d | ", h->name, h->home, h->since.y, h->since.m, h->since.d); hhmm(out, h->weekdays); fprintf(out, " | \"%s\" | %d | %llu\n", h->sends, h->gains, (unsigned long long)h->seed); } fclose(out); out = fopen(RECORDS "/almanac.txt", "w"); fprintf(out, "# The almanac. The climate is in climate.txt; this decides each day.\n"); fprintf(out, "Seed | %llu\n", (unsigned long long)almanac); fclose(out); qsort(planned, (size_t)acts, sizeof planned[0], in_order); static const char *const deeds[] = {"sees to the naming", "finds them a house", "sets the clock", "has a word"}; out = fopen(RECORDS "/society.txt", "w"); fprintf(out, "# The Society. What it has done, and when. Nobody in town knows of it.\n"); fprintf(out, "# Each act is done in the month given, after everything else that happens that month,\n"); fprintf(out, "# by the keeper of the dead letter office, who must walk to the household to do it.\n"); fprintf(out, "# Clocks are put a number of minutes fast, or slow if negative. The bell puts them right.\n"); fprintf(out, "# A word gets someone up a number of minutes later, or earlier if negative.\n"); fprintf(out, "# When | line | what | how\n"); for (int i = 0; i < acts; i++) { struct day day = month_day(planned[i].month); fprintf(out, "%04d-%02d-%02d | %s | %s | ", day.y, day.m, day.d, planned[i].line, deeds[planned[i].deed]); if (planned[i].deed == CLOCKS || planned[i].deed == WORDS) fprintf(out, "%+d\n", planned[i].minutes); else fprintf(out, "%s\n", planned[i].what); } fclose(out); } int main(void) { founded = calendar("Founded"); census = census_day(); uint64_t almanac = 1; for (set_almanac(almanac); strcmp(weather(census), "fog") != 0; set_almanac(++almanac)) ; printf("almanac seed %llu: fog on the census day\n", (unsigned long long)almanac); town = (struct town){.month = month_of(founded), .lines = WANTED}; for (int i = 0; i < WANTED; i++) found_by_chance(&wanted[i], &town.line[i]); for (int i = 0; i < WANTED; i++) { int before = acts; if (!plan(i)) { printf("the Society cannot see to %s\n", wanted[i].line); return 1; } printf("%-12s founded as %-12s %d act%s\n", wanted[i].line, town.line[i].name, acts - before, acts - before == 1 ? "" : "s"); } if (!still_right(WANTED)) { printf("the Society has undone its own work\n"); return 1; } set_society(planned, acts); int hellos = 0, on_census = 0; struct town survey = town; for (int month = month_of(founded); month < month_of((struct day){2201, 1, 1}); month++) { struct letter letters[MAX_SENDERS]; struct day day = month_day(month); int n = letters_from(&survey, day, letters); char text[64] = ""; for (int i = 0; i < n; i++) strcat(text, letters[i].sender.body); if (strcmp(text, "Hello, World!") == 0) { hellos++; on_census |= same_day(day, census); printf("Hello, World! on %04d-%02d-%02d\n", day.y, day.m, day.d); } advance(&survey); } if (hellos != 1 || !on_census) { printf("the town says hello %d times, %s on the census day\n", hellos, on_census ? "once" : "never"); return 1; } write_records(almanac); printf("%d acts written\n", acts); return 0; }