From 044b125b29b31d14211bc72d3c264b0cc9c22fd1 Mon Sep 17 00:00:00 2001 From: kirreen Date: Tue, 10 Dec 2024 20:59:48 +0100 Subject: [PATCH] solved 10a and b --- 10/a.out | Bin 0 -> 19680 bytes 10/input | 53 +++++++++++++++++++++ 10/inputDemo | 8 ++++ 10/main.c | 127 +++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 188 insertions(+) create mode 100755 10/a.out create mode 100644 10/input create mode 100644 10/inputDemo create mode 100644 10/main.c diff --git a/10/a.out b/10/a.out new file mode 100755 index 0000000000000000000000000000000000000000..896d67e95109b6591d7af7845fdac293759898a0 GIT binary patch literal 19680 zcmeHP4Rl-8eZTL?SC*~VvSTOM2{Z~xfKnnmpX9@7gfx+gKY0?HCJ$wSoW%ZBA#-D9biZ=WU^LPUuQo+PUo++V6kg zy~?w&SxTb4 ztHO3Zo5!5s%LHc0{T6}LD(Ccx)Fp8TDA_eoWfFS11v5oe3yG55RHZ=%P?X7jkR2x@ z$rsbR$}NwIDtRHE*OId$dLJpEC~CK(+>9&~Vm7j7B0VJYG9;<8BbMw&rQN8sD^MA4 zR8o~s$_f2e$b1%4KV($=V#`>uUB9#|rW>Tbq7;M5t?!zV-zDBQY4@C0xM9WgItyls zs(3fUj`H%`7B$Sb$aqtYL$l1kqB5>v5zyFdT(+ndMdeQ^CdPpnNo zcnk>r0y{W*8|m z6y9xwyLTI%p;!;=Oh%)uGtn1~v))ioPa+HzOtUcQB~7C@6pI51_lJzmSUl7dyBFHw zoh6X-nI>^~Pe~(n4*?rRrkeGG?@G-)cmn$!J$BWk!?Rx32F= z#G~6o9X(OR=;}?xWmLmbom3Is(lJ9dY<0{!+F~9Tw$yD6@+hd<-|AzrDw?4Qi9I*` z7WabzD@(qi1U^{G2@QDSVg2xA`r(!WExeYvFW^kh`=1&e(8y zh!gmT4X?CJ#rB*HM@I@})P~cXFBHF3qj^emQWclqsu6DAUu$f*I3HPRtiguck8@rd zE>BUy;u;&SazTP-8;(R5$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R1h>}t&ge*`q&soY6l;|y>KRx`Yy@uHV0_%V;fafwCpw@HN^rlD+SiLy z3#gg2Xt%22EEG-lF<(4kMtxoJw68CTCDTcBpAG6r$FMR#7Licvy0(R8sEY~p?og_m z`6Bz`2w_oNK?unnocQoj$^^!MEE(+yk%6rB^_a{jn&~s?;=w1z(w9t#!S+SFapDQ} zMvd+WY!zo&7@=e`gocuZs>3n9d%^jmD*<2F_(TB984}KX;Y2UKmw_-6?MQdQE)?&= zNkp=-cxOV@LmeGB$tw>3Ob}IdWKUHI#waEy?mxdHN6&w;OETOQh|(bMP!*+T1@=O^ z_A(C)?#T-FE2O=me!DX2bl$R;*#36l zzr-_K)&HEdS5)=iDea5f9|DfMyF&j*rM;qJ>r{%MhYP6`w~Txgi(LfI*YPZ!y%>l;PY`x~WS-2T6i_A39(D>WtkreqaS z`B(f&RH*;T{yDEDQIziX?WH*W8xY#b=*PwBJ>*v?|I@Q60v1dv!e;Y_{}OJCz${SKwhx zm}Fx85FT>?G*O4E3BCPYR8gID7N1$?Th=FS!Ef-r?6LkZkxjXKUysN zhh +#include +#include +#include + +struct list { + int xy[2]; + struct list *next; +}; + +int score_trail(int x, int y, int width, int height, char **map, struct list *peaks) +{ + int dir[4][2] = {{0, -1}, {1, 0}, {0, 1}, {-1, 0}}; + int score = 0; + int first = map[x][y]; + + // init list + if (peaks == NULL) { + peaks = malloc(sizeof(struct list)); + peaks->xy[0] = -1; + peaks->xy[1] = -1; + peaks->next = NULL; + } + + // if find 9, check if its already in list, else append it + // could have been its own func + if (map[x][y] == '9') { + printf("found %dx%d\n", x, y); + if (peaks->xy[0] == -1 ) { + printf("was first, init first trail\n"); + peaks->xy[0] = x; + peaks->xy[1] = y; + return 1; + } + // while (!(peaks->xy[0] == x && peaks->xy[1] == y) && peaks->next) { + while (peaks->next) { + printf("checking next peaklist...\n"); + peaks = peaks->next; + } + /* + if (peaks->xy[0] == x && peaks->xy[1] == y) { + printf("found same, exit\n"); + return 0; + } + */ + // printf("allocate new peak, jump to it, and set it\n"); + peaks->next = malloc(sizeof(struct list)); + peaks = peaks->next; + peaks->xy[0] = x; + peaks->xy[1] = y; + peaks->next = NULL; + return 1; + } + + // look in every direction for grid[j][i] + 1, recurse it + for (int i = 0; i < 4; i++) { + if (x + dir[i][0] >= 0 && + x + dir[i][0] < width && + y + dir[i][1] >= 0 && + y + dir[i][1] < height) { + if (map[x + dir[i][0]][y + dir[i][1]] == map[x][y] + 1) + score_trail(x + dir[i][0], y + dir[i][1], + width, height, map, peaks); + } + } + + // please count and cleanup the list + if (first == '0') { + struct list *tmp; + while (peaks) { + score++; + tmp = peaks; + peaks = peaks->next; + free(tmp); + } + return score; + } + return 0; +} + +int main () +{ + + // figure out data dimensions + FILE *input = fopen("./input", "r"); + int width = 0; + int height = 1; + while (fgetc(input) != '\n') width++; + for (char c = fgetc(input); c != EOF; c = fgetc(input)) { + if (c == '\n') + height++; + } + rewind(input); + printf("Dimensions: %d x %d\n", width, height); + + // make 2d array + char **map = malloc(width * sizeof(char*)); + map[0] = malloc(width * height); + for (int i = 1; i < width; i++) { + map[i] = map[0] + i * height; + } + + // put the data in the array + for (int i = 0; i < height; i++) { + for (int j = 0; j < width; j++) { + map[j][i] = fgetc(input); + } + fgetc(input); + } + fclose(input); + + // sum it + int scoreSum = 0; + // find trailheads, check their score, sumit (hehehe) + for (int i = 0; i < height; i++) { + for (int j = 0; j < width; j++) { + if (map[j][i] == '0') { + printf("\tExamining %dx%d:\n", j, i); + int score = score_trail(j, i, width, height, map, NULL); + scoreSum += score; + if (score) + printf("score sum trail head %dx%d: %d\n", j, i, score); + } + } + } + printf("total score: %d\n", scoreSum); +}