2025-03-22 13:30:08 +00:00

144 lines
2.9 KiB
C

/*
* Copyright (c) Camden Dixie O'Brien
* SPDX-License-Identifier: AGPL-3.0-only
*/
#include "puzz.h"
#include <assert.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#define NRUNS 1000
enum { UNKNOWN = 0xfe, KILLER = 0xfd };
typedef enum {
FOUND_MINE,
FOUND_SAFE,
FOUND_NOTHING,
} update_res_t;
typedef struct {
int mines, unknown;
puzz_t field;
} state_t;
static void setadj(puzz_t field, int x, int y, uint8_t from, uint8_t to)
{
for (int yp = y - 1; yp < y + 2; ++yp) {
for (int xp = x - 1; xp < x + 2; ++xp) {
if (xp < 0 || xp >= WIDTH || yp < 0 || yp >= HEIGHT
|| (xp == x && yp == y))
continue;
field[xp][yp] = field[xp][yp] == from ? to : field[xp][yp];
}
}
}
static void getadj(puzz_t field, int *x, int *y, uint8_t val)
{
for (int yp = *y - 1; yp < *y + 2; ++yp) {
for (int xp = *x - 1; xp < *x + 2; ++xp) {
if (xp < 0 || xp >= WIDTH || yp < 0 || yp >= HEIGHT
|| (xp == *x && yp == *y))
continue;
if (field[xp][yp] == val) {
*x = xp;
*y = yp;
return;
}
}
}
assert(false);
}
static update_res_t update(state_t *state, int *x_out, int *y_out)
{
state->mines = state->unknown = 0;
for (int y = 0; y < HEIGHT; ++y) {
for (int x = 0; x < WIDTH; ++x) {
state->mines += state->field[x][y] == MINE ? 1 : 0;
state->unknown += state->field[x][y] == UNKNOWN ? 1 : 0;
}
}
for (int y = 0; y < HEIGHT; ++y) {
for (int x = 0; x < WIDTH; ++x) {
if (state->field[x][y] == UNKNOWN || state->field[x][y] == MINE)
continue;
const int mines = countadj(state->field, x, y, MINE);
const int unknowns = countadj(state->field, x, y, UNKNOWN);
if (unknowns == 0)
continue;
if (mines + unknowns == state->field[x][y]) {
setadj(state->field, x, y, UNKNOWN, MINE);
return FOUND_MINE;
}
if (mines == state->field[x][y]) {
getadj(state->field, &x, &y, UNKNOWN);
*x_out = x;
*y_out = y;
return FOUND_SAFE;
}
}
}
return FOUND_NOTHING;
}
static status_t solve(void)
{
state_t state = { .mines = 0, .unknown = WIDTH * HEIGHT };
memset(state.field, UNKNOWN, sizeof(puzz_t));
int x = rand() % WIDTH;
int y = rand() % HEIGHT;
status_t status;
do {
if (state.field[x][y] != MINE
&& (status = probe(x, y, state.field)) == DEAD)
break;
update_res_t update_res;
do
update_res = update(&state, &x, &y);
while (update_res == FOUND_MINE);
if (update_res == FOUND_NOTHING) {
x = rand() % WIDTH;
y = rand() % HEIGHT;
}
} while (state.mines < NMINES);
return status;
}
int main(void)
{
struct timeval tv;
if (gettimeofday(&tv, NULL) != 0) {
perror("Failed to get time");
exit(1);
}
srand(tv.tv_usec);
int nsolved = 0;
for (int i = 0; i < NRUNS; ++i) {
gen();
nsolved += solve() == OK ? 1 : 0;
}
const double prop = (double)nsolved / NRUNS;
printf("Solved %d/%d (%0.1f%%)\n", nsolved, NRUNS, 100 * prop);
return 0;
}