master
Jonathan Hodgson 2 years ago
parent 653f3a51bb
commit 9f6ff1c52d
  1. 10
      day9/Makefile
  2. 5
      day9/example.txt
  3. 100
      day9/realinput.txt
  4. 91
      day9/solution-part1.c
  5. 157
      day9/solution-part2.c

@ -0,0 +1,10 @@
solution-part%: solution-part%.c
gcc $< -o $@ -lm
test-%: solution-% example.txt
$< example.txt
run-%: solution-% realinput.txt
$< realinput.txt
.PHONY: $(patsubst %,run-%, 1 2) $(patsubst %,test-%, 1 2)

@ -0,0 +1,5 @@
2199943210
3987894921
9856789892
8767896789
9899965678

@ -0,0 +1,100 @@
3567891987643457898879876567896543567999831045697654564567923989994345678987678789732349887675978921
2458910976432179987568965456789732679898753234598753213459205978989234899875467679549498765434569530
0199891987651099999699864346678901236799874447679832102398919867879345698685353568998999654323678991
9987689997543988942987954244599932345679975998799993219987998756567956796543212457896898765512356789
8876599898759867899876543123989893456893989899998789398996789645458967989954563568965989997434667990
6932465779998656789998756439876799578902398799987678987975696532367898979797678979654979876598798991
4321234569876549896569968998765698989653998698994569876564589643456799867689989989769865987679899989
5210156789965323597459879998874587898769896587989678995323578956567989754567891299879954598789998979
4321345899876434589567999987923456789899765445678999986414789967979876543488910198998767679899987867
6632397965987545678999999876212345693987654323567912398924567899899989764579329977899879795967986459
9543989894299866789987898754101656892198765214568903459435898979798999875789998756989999893456796598
8759878792101979897676987643212567894239899399979214567986789765687989989899889549878989901349987986
9898765689212591987565698654323459964349978987899935978999897654245678996947679698769979932598999995
9998654569423492989434599765434567895598867456987899899423987542134599875434568989654667894987987894
9789743458975989876325989976545678987697656349896798778912499651013987997645679876543458965976876792
8698652456896978965439879898876799398789543298775986567893599973129876798958893976402979976965345891
7597543467899767896998965679998895479997652109654323479994989984234984589767912993213567899879866920
6498794698998457989876534569999976569876543299865212569989876797545999679878929854323989924989977891
2379898789876345678921024678987898789987856987654323457976545987659898989999798769574567899798989999
4456999899765456889532125699896789898698997899765434567899432398798767897535699998765689987657492478
5578999921996567897656236989765678987569798999876645698957921239987656789649987659977799998743201367
7689689890989688998798759876524589765456659789987786789546890149876545699898999543989999876543212457
8994598799878999339899767998435678976342345678998987893435789349765434569976898932395699987654326568
9012987643456797123902979876549889887201956889769698942329895467976787678954967891234989999965437679
2179876732367896014594989988659997654319899998754569321018799578998998789123456789349879899896548989
4359865321298965123689892198768999766598678997653698932147678989859999896346569896498765789789679997
9498763210245954349799651099978989898976567896542987654234567898745899965487978976569654345678998986
8987654321239895498987543987989679999432459789693498765445679997656789876568999987698786467789987545
7899875434398789987898659876594568987991234678989999876656789998769897988979989998789987578999876434
6531989546497678976549898765423679876789545789767898987987898999878976599989879989999898999767997645
7432397659986568997932999976512398544897656899959987598998987999989765322398769879898769456989498867
6543498798775457889431098987624987632369767929898976439569976789999854310989654766789654349894329978
7654569987656345579943987698899764321248978909797895323457895678929875929876543245678921235789412399
8765679976542136467899876559987654320136799998676789214568954567919989899877610176899420126897543999
9876889987431012349913987432199766872345789987545795323459543456798998769754321398999321247897659897
2989999987542123567899877643019898953489899876436989435678954597897899659876432345678992348998798765
3599989997654344567998765432123989876578998765425678956999978989986899543989546797789989459359899954
4698979998865455878939998765234568987989987654314567997896989876775678932397656898899978978998999965
5997767899876666789323459654345689798995498765323456789985398765434567891239789999998768999887989876
9865456912987989895457998767858789649689349986465589899965269894323788910199898989987659998766578997
8754319899899198998667919898969891234589237697877679998764356983212669921989976878999846899542467898
9865456789765347899778929999989910346679104598999897659975569862101457899876795467897656987431298999
9976777895986456799899998999998434457895323679876998943498778954212345678965789346789879876540139898
8998998954987567897956987678976595978965455789765689899999899965323486789654567999897999987621998786
6659989543298998965344986567897989899876576897643476798789939876765797899865678987986567898749876545
5349867994039579999123975456789876789998697976532365698699421987978998949878989896575456899656987634
3298756889123459878939765345998765699998789765431234986578932398989459134989898765452345698768999523
2197545678939967967899874239877674578939899998420125987899543999797678999795698654321246789879998434
3986434567897899879997985645965432349649989987531349998935959898689789987654239765530124578999997645
4994323978976799989876976759878653468998767987642498989129898769456998998562123987641436789999898867
9875413899765689998765987868989965678987957899999987778998799954349887789421045699532647999789769978
9975326789984568949984398979099876789896548789878996567897659895459765678932156798743498987698754989
9875435689965989239876239989299987999765435699767423456998946789569874567893278897659999999549865998
5987749899899892049954345799989998998843323789654312599899834596978983479954399998998789899432977897
4597656998754679198765456789879789976532012698943201987689656975989994567895989879549697678953989945
3498767899843598939876567899765679865432124567895329876598967894699865678999876768998546568964590234
4569878978932467921987798999654578987543236689997435985467899943457988789998765449876323479987689345
7699999568901357890298899998843459899854457899876557897589965432356799899989854333985454567999789499
8789123479953456789349912997672699689765698912987668987699975431234898999976543212987565678999899978
9893234567894568997999439876531987578976899993498789098967896945949987678998765323498686789889999867
9965445679965678996788998987610398689987901989569892199656789899898796569999965434569797895678998756
8987896893496889985437987896321239789998999778999943987547898788789653467899879545679898934789349432
7898987942987999876545976895434549899999987656789994976432987656679732567893989698789999655891296551
6799898931998979998959875689545856999889976543499989984521298645578901246932198799898789867932987867
5689769649899758789879754678956767897679865431299878993210985432459913459951099912987678979999898978
4569998798799645678998763456968978987567987320987656789929976421567895598892989893976568999989769989
3498899899698934569898542347899999995439876459876545677898797533678999976789576789865459789878943294
2376789987566899689797655458987999986320998767966431256789698754789998764593445899879345698767892102
1235999987345678998659778969895498765421239978954310137996549965898889865692136901998976789456789264
2348969876459799997549889879789329876432357989876521245894321986987678976789029212987897891347894395
3467942987578899998634999989699567999943569795987432656789210197896546987899198933986789930456989989
4578931298689999899324799996588989987894698654599545867895423298965434699998987899865578921239879879
7679549499895398765435678965466898776895987542349657978999564569876986792397976798783467893398767956
8989698989943239986586889984345789654997899651298789989398979678998997891986545987632348965459854543
9998797678987458997697999875235799843689998910129891093297898999549049992398939876543567896598753652
6789986569896567989788987654356999754567897953256992129986767896432135689987824987654578987965432101
4579995445789879878999998976999898975678976894345789998765456798743546798776501298765989599876543213
3498986323589998968999989899878667896799875799556999899654345689656787987654312349989795467989754354
4987691012478987657898878789765456987899864678977898798765467899867899898766423756799654367899875565
5986543123567898946997654598654347998998743457898997679877568999998998769987534678978975256789988677
6987663248678999858987643569876767899597652368999876565989678989899987645697685989769976347999899898
7999854357789998767896542354989898976439763578998765424598789878678987656789876999659899869999789919
8998765469899949878989750123498969598929854689129873213659897654598998767895987898998789979898678901
9999879879999832999679971334597653469898765793234964302349953212387899878934699987987678997657567992
9987989989998721023457895499989432598789976789549875463598764301256789989123459876543589876545456789
8876899999987652134568999978678921997678998999799986654789975432345897993234567989542499987632345689
7654698989998543245979987856567939876567999102989899865678987543456976789345679898743459876721234678
8769987678987654346789996543459899987679789212976799976989398764689565678956789789954698765430123569
9998798582399765458898775432376789899889678929865689987993109865789454567899898697895899876521234567
3987654341349878579997654321235679765994569998654567898943212978894313456789976546896999989632376789
2498765210234989678998976492346789654323458999765679979764323989932102568999975435987898799843497997
4569886345695699899899989989959996543212356998986889768975434598754213479239894326798936598765567896
5699997556789789924689999877898799754901239897899997656899545679865674589998732107989323459877678965
6789898767899891013579888765789678969892398776798965345678959789996786679876545212378901266988789323
7998759978912942125698769434134567898789997654357893257789768999989899789987687354567892345699899212
9876532199909943286789654321023678987679876543234789357899879239878998990298798865679943466789978994
2987694367898765497898765432335789989467989862125678969987989498657897892989899976789955577895456789
3498986456789976567959876543566899865346995993013489878976597987545656789864945989898766698934349899
6569297678992988798943998678797897643219864987654899989497456986531248999943239899919897979210245678
9791098989321099899532109899899998856424965699766789994398577895432346898765356789323998964323345789

@ -0,0 +1,91 @@
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>
int countRows(FILE *fp){
unsigned int lines=0;
while (EOF != (fscanf(fp, "%*[^\n]"), fscanf(fp,"%*c")))
++lines;
return lines;
}
int countCols(FILE *fp){
unsigned int lineLength=0;
char ch = fgetc(fp);
while ( ch != '\n' ){
lineLength++;
ch = fgetc(fp);
}
return lineLength;
}
bool isMinimum(int rows, int cols, int grid[rows][cols], int row, int col){
// Check above
if ( row > 0 && grid[row-1][col] <= grid[row][col] ) return false;
//check below
if ( row < rows-1 && grid[row+1][col] <= grid[row][col] ) return false;
// Check left
if ( col > 0 && grid[row][col-1] <= grid[row][col] ) return false;
//check below
if ( col < cols-1 && grid[row][col+1] <= grid[row][col] ) return false;
return true;
}
int main( int argc, char *argv[] ){
if( argc == 2 ) {
FILE *fp=fopen(argv[1], "r");
if ( fp == NULL ) {
return 1;
}
unsigned int rows=countRows(fp);
rewind(fp);
unsigned int cols=countCols(fp);
rewind(fp);
int grid[rows][cols];
memset( &grid, 0, rows*cols*sizeof(int) );
unsigned int risk=0;
unsigned int row=0, col=0;
for ( char ch = fgetc(fp); EOF != ch; ch=fgetc(fp)){
if ( ch == '\n' ){
row++;
col=0;
} else {
grid[row][col] = atoi(&ch);
col++;
}
}
for ( unsigned int row = 0; row < rows; row++ ){
for ( unsigned int col = 0; col < cols; col++ ){
if ( isMinimum(rows, cols, grid, row, col) ){
risk+= grid[row][col]+1;
}
}
}
printf("Total risk at lowpoints: %i\n", risk);
fclose(fp);
return 0;
} else {
printf("You need to provide a file\n");
return 1;
}
}

@ -0,0 +1,157 @@
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>
#define ANSI_COLOR_RED "\x1b[31m"
#define ANSI_COLOR_YELLOW "\x1b[33m"
#define ANSI_COLOR_RESET "\x1b[0m"
typedef struct Position {
int value;
bool marked;
bool lowpoint;
} Position;
int countRows(FILE *fp){
unsigned int lines=0;
while (EOF != (fscanf(fp, "%*[^\n]"), fscanf(fp,"%*c")))
++lines;
return lines;
}
int countCols(FILE *fp){
unsigned int lineLength=0;
char ch = fgetc(fp);
while ( ch != '\n' ){
lineLength++;
ch = fgetc(fp);
}
return lineLength;
}
bool isMinimum(int rows, int cols, Position grid[rows][cols], int row, int col){
// Check above
if ( row > 0 && grid[row-1][col].value <= grid[row][col].value ) return false;
//check below
if ( row < rows-1 && grid[row+1][col].value <= grid[row][col].value ) return false;
// Check left
if ( col > 0 && grid[row][col-1].value <= grid[row][col].value ) return false;
//check below
if ( col < cols-1 && grid[row][col+1].value <= grid[row][col].value ) return false;
return true;
}
int cmp_asc_int(const void *a, const void *b) {
return *(int*)a > *(int*)b;
}
unsigned int basinSize(int rows, int cols, Position grid[rows][cols], int row, int col){
unsigned int ret = 0;
if ( grid[row][col].value == 9 ){
return 0;
}
ret++;
grid[row][col].marked = true;
//Check above
if ( row > 0 && ! grid[row-1][col].marked )
ret+=basinSize(rows, cols, grid, row-1, col);
//Check below
if ( row < rows-1 && ! grid[row+1][col].marked )
ret+=basinSize(rows, cols, grid, row+1, col);
// Check left
if ( col > 0 && ! grid[row][col-1].marked )
ret+=basinSize(rows, cols, grid, row, col-1);
//check right
if ( col < cols-1 && ! grid[row][col+1].marked )
ret+=basinSize(rows, cols, grid, row, col+1);
return ret;
}
void printGrid(int rows, int cols, Position grid[rows][cols]){
for ( unsigned int row = 0; row < rows; row++ ){
for ( unsigned int col = 0; col < cols; col++ ){
if ( grid[row][col].lowpoint ) printf(ANSI_COLOR_RED);
else if ( grid[row][col].marked ) printf(ANSI_COLOR_YELLOW);
printf("%i", grid[row][col].value );
if ( grid[row][col].marked ) printf(ANSI_COLOR_RESET);
}
printf("\n");
}
}
int main( int argc, char *argv[] ){
if( argc == 2 ) {
FILE *fp=fopen(argv[1], "r");
if ( fp == NULL ) {
return 1;
}
unsigned int rows=countRows(fp);
rewind(fp);
unsigned int cols=countCols(fp);
rewind(fp);
Position grid[rows][cols];
memset( &grid, 0, rows*cols*sizeof(Position) );
unsigned int risk=0, row=0, col=0;
unsigned int max[4] = {0};
for ( char ch = fgetc(fp); EOF != ch; ch=fgetc(fp)){
if ( ch == '\n' ){
row++;
col=0;
} else {
grid[row][col].value = atoi(&ch);
col++;
}
}
for ( unsigned int row = 0; row < rows; row++ ){
for ( unsigned int col = 0; col < cols; col++ ){
if ( isMinimum(rows, cols, grid, row, col) ){
risk+= grid[row][col].value+1;
grid[row][col].lowpoint = true;
max[0] = basinSize(rows, cols, grid, row, col);
qsort(max, 4, sizeof(unsigned int), cmp_asc_int);
}
}
}
unsigned int prouduct=1;
printf("The largest 3 basins are:\n");
for (unsigned int i = 1; i < 4; i++){
printf("* %i\n", max[i]);
prouduct *= max[i];
}
printf("\nProduct is %i\n", prouduct);
fclose(fp);
return 0;
} else {
printf("You need to provide a file\n");
return 1;
}
}
Loading…
Cancel
Save