solved 10a and b
This commit is contained in:
parent
721b69ae20
commit
044b125b29
4 changed files with 188 additions and 0 deletions
BIN
10/a.out
Executable file
BIN
10/a.out
Executable file
Binary file not shown.
53
10/input
Normal file
53
10/input
Normal file
|
@ -0,0 +1,53 @@
|
||||||
|
45678701021256787218432154301232100012334301023456789
|
||||||
|
32569892430543298909845067210145621965421012310545869
|
||||||
|
01210743549612187610756778143296534874310123458930978
|
||||||
|
12323651258703066525643889050387546789210234567821567
|
||||||
|
01434210367012178434512918761236695694391349650131054
|
||||||
|
12544303438124569232101109678945784321487658743232343
|
||||||
|
43695496549433450143001234532034653210156961234589787
|
||||||
|
94786987834342100154519873541128763165432870234671096
|
||||||
|
85677889929854343267610565690639454076501210165692345
|
||||||
|
76012870010767256998701234788748348987432101156787654
|
||||||
|
01043961171258107887898345659654239858901101089810123
|
||||||
|
32154552987349016576987454564980108765432232123209874
|
||||||
|
43960143476987657607876523875676501678921349854112365
|
||||||
|
54871232564890548510965014934189432578900458963012453
|
||||||
|
69890121465431239423454876821054329657811467874501412
|
||||||
|
78781230656322102347623945498765018746324320189432303
|
||||||
|
45610945567212121098510130340121201235435410234534564
|
||||||
|
44327876438901010101498321233290345110346761809621875
|
||||||
|
34387654323432129812367321044789876011289898918760976
|
||||||
|
45297890012343456703455433445602345895670767823451987
|
||||||
|
56187781201278914567526932563211056734321296744589854
|
||||||
|
67096654302107803498017801074787654321234585430076763
|
||||||
|
78945109213456012332101301985698543210987676121125892
|
||||||
|
21032238376788768945432452394987650121789678032434981
|
||||||
|
32561247487699854876983345401276345430678549140123470
|
||||||
|
23470056794521003123476236982345036781565432101210565
|
||||||
|
14980129873430412001569107810034129092634307870389874
|
||||||
|
05691234562541343432018098941125678104321216921010123
|
||||||
|
06788765101632234589127657832103543219450325432167012
|
||||||
|
12109453210762103678934566543012354308765014703458983
|
||||||
|
43898344789899872100129875414983989412894327812565410
|
||||||
|
56701235692198561091223014305894876543781016945678320
|
||||||
|
12345106541085432782014323216765321789692345238769801
|
||||||
|
01416787632176306654105450125601450694547654199654432
|
||||||
|
12109898501201217653296961234702364543498703080123569
|
||||||
|
01234549654323898741787870149810676032107012678034078
|
||||||
|
67899678760015677230765487654321980121978710569985127
|
||||||
|
58908707871234982101896398923450890120879623450276434
|
||||||
|
43211216910123878982363210110961051234566542141105589
|
||||||
|
52890345034987965985476545607872340345651033032234676
|
||||||
|
01761212125675654876983432787401456978762122345897655
|
||||||
|
10354305430234503210012301294301967869887831056798587
|
||||||
|
23487416521105012342121345385210876778896990987123496
|
||||||
|
96596547012276109653010256106321236569045781234012345
|
||||||
|
87432108983489298764560187287810345652134650965421004
|
||||||
|
76545017698548345675078894396923456743221045874540218
|
||||||
|
89632123087632210982189123405410567892100038973234389
|
||||||
|
56749834128901043983458016512321098754321122980198476
|
||||||
|
43898765439456712276567087695632347665430101076567567
|
||||||
|
32109650169329803125690198787541056578920122345445898
|
||||||
|
78980543278019874034787239645670567810110233410336765
|
||||||
|
65211230165216565129876543532789436921898398561221234
|
||||||
|
34302321254305653210210123401890125432765487652310123
|
8
10/inputDemo
Normal file
8
10/inputDemo
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
89010123
|
||||||
|
78121874
|
||||||
|
87430965
|
||||||
|
96549874
|
||||||
|
45678903
|
||||||
|
32019012
|
||||||
|
01329801
|
||||||
|
10456732
|
127
10/main.c
Normal file
127
10/main.c
Normal file
|
@ -0,0 +1,127 @@
|
||||||
|
#include<stdlib.h>
|
||||||
|
#include<stdio.h>
|
||||||
|
#include<string.h>
|
||||||
|
#include<stdbool.h>
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
Loading…
Reference in a new issue