easters

easters.dev solutions (C)
git clone git://bsandro.tech/easters
Log | Files | Refs | LICENSE

commit 6353c6bbed0989361488569323c0389955836705
parent 8a8d6cb14c77f026a879431ef99595dc3d201b96
Author: bsandro <email@bsandro.tech>
Date:   Mon, 12 Jan 2026 02:51:58 +0200

sunday: basic data structures and couple of essential functions

Diffstat:
MMakefile | 30+++++-------------------------
Msunday.c | 93+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------------
2 files changed, 79 insertions(+), 44 deletions(-)

diff --git a/Makefile b/Makefile @@ -4,6 +4,10 @@ MAKEFLAGS+=-rR UNAME:=$(shell uname -m) OS:=$(shell uname -s) +ifeq ($(origin CC),default) +CC=cc +endif + CFLAGS=-std=c99 -Wall -Wextra -I. ifeq ($(DEBUG),1) CFLAGS+=-O0 -g @@ -16,15 +20,7 @@ ifeq ($(OS),NetBSD) CFLAGS+=-I/usr/pkg/include endif -ifeq ($(OS),Darwin) -CFLAGS+=-I/opt/homebrew/include -endif - -ifeq ($(UNAME),riscv64) -CFLAGS+=-mtune=sifive-u74 -else ifeq ($(UNAME),Power Macintosh) -CFLAGS+=-mtune=native $(LTO) -else ifeq ($(UNAME),i386) +ifeq ($(UNAME),i386) CFLAGS+=-mtune=native else CFLAGS+=-march=native $(LTO) @@ -36,22 +32,6 @@ ifeq ($(OS),NetBSD) LDFLAGS+=-L/usr/pkg/lib -Wl,-rpath,/usr/pkg/lib endif -ifeq ($(OS),Darwin) -LDFLAGS+=-L/opt/local/lib -endif - -ifeq ($(GLPK),1) -LDFLAGS+=-lglpk -endif - -ifneq ($(shell which clang),) -CC=clang -else ifneq ($(shell which gcc-mp-14),) -CC=gcc-mp-14 -else -CC=cc -endif - .PHONY: clean %+run: %.bin diff --git a/sunday.c b/sunday.c @@ -1,45 +1,100 @@ #include <stdio.h> +#include <stdbool.h> -// 5x5 test -// 12x12 real +#define LEN(x) (sizeof(x)/sizeof(*x)) + +typedef struct { + char grid[12][12]; + int w; + int h; +} Map; + +typedef struct { + int x; + int y; +} Vec2; + +typedef struct { + Vec2 data[4]; + int len; +} PossibleMines; + +static Vec2 mine_area[] = { + {.x= 0,.y=-1}, + {.x=-1,.y= 0}, + {.x= 1,.y= 0}, + {.x= 0,.y= 1}, +}; + +static Vec2 heat_area[] = { + {.x=-1,.y=-1},{.x=0,.y=-1},{.x=1,.y=-1}, + {.x=-1,.y= 0}, {.x=1,.y= 0}, + {.x=-1,.y= 1},{.x=0,.y= 1},{.x=1,.y= 1} +}; + +PossibleMines findPossibleMines(Map map, Vec2 nodule) { + PossibleMines mines = {0}; + for (int i=0;i<LEN(mine_area);++i) { + Vec2 p = { .x=nodule.x+mine_area[i].x, .y=nodule.y+mine_area[i].y }; + if (p.x>=0&&p.x<map.w&&p.y>=0&&p.y<map.h&&map.grid[p.y][p.x]=='.') { + bool ok = true; + for (int j=0;j<LEN(heat_area);++j) { + Vec2 h = { .x=p.x+heat_area[j].x, .y=p.y+heat_area[j].y }; + if (h.x>=0&&h.x<map.w&&h.y>=0&&h.y<map.h&&map.grid[h.y][h.x]=='M') { + ok = false; + break; + } + } + if (ok) mines.data[mines.len++] = p; + } + } + return mines; +} int main(void) { - char map[14][14] = {0}; + Map map = {0}; int xmines[12] = {0}; int ymines[12] = {0}; - int w = 0; - int h = 0; + Vec2 nodules[50] = {0}; + int nodules_s = 0; for (char c=getchar();c!=EOF;c=getchar()) { if (c>='0'&&c<='9') { - if (h==0) { - xmines[w++]=c-'0'; + if (map.h==0) { + xmines[map.w++]=c-'0'; } else { - ymines[h-1]=c-'0'; - w=0; + ymines[map.h-1]=c-'0'; + map.w=0; } } else if (c=='.'||c=='O'||c=='G') { - map[h-1][w++]=c; - } else if (c=='\n'&&w>0) { - h++; + map.grid[map.h-1][map.w++]=c; + } else if (c=='\n'&&map.w>0) { + map.h++; } } - h-=1; - printf("w:%d,h:%d\n", w, h); - for (int y=0;y<h;++y) { - for (int x=0;x<w;++x) { - printf("%c", map[y][x]); + map.h-=1; + printf("map w:%d,h:%d\n", map.w, map.h); + for (int y=0;y<map.h;++y) { + for (int x=0;x<map.w;++x) { + printf("%c", map.grid[y][x]); + if (map.grid[y][x]=='O') { + nodules[nodules_s++] = (Vec2){ .x=x,.y=y }; + } } printf("\n"); } puts("ymines:"); - for (int y=0;y<h;++y) { + for (int y=0;y<map.h;++y) { printf("%d,", ymines[y]); } printf("\n"); puts("xmines:"); - for (int x=0;x<w;++x) { + for (int x=0;x<map.w;++x) { printf("%d,", xmines[x]); } printf("\n"); + printf("%d nodules:\n", nodules_s); + for (int i=0;i<nodules_s;++i) { + printf("x:%d,y:%d\n", nodules[i].x, nodules[i].y); + } return 0; }