I'm a Sr. Software Engineer at Flashpoint. I specialize in Python and Go, building functional, practical, and maintainable web systems leveraging Kubernetes and the cloud. Blog opinions are my own.
I'm going to try to do my solutions in C. C is something I'm learning, so if anybody has any pointers (LOL), please don't hesitate to critique my code.
I didn't do anything crazy today. I just threw loops at it to see if it was fast enough. It solved before I could blink, so I'm not going to work any harder at improving speed.
/// Day 1: Report Repair/// /// My expense report for my trip is messed up and entries need/// adjusting.#include <stdio.h>
#include <stdlib.h>
/// Parse the input file, which contains one positive integer per line.int*parse(constchar*filename,size_t*count){FILE*fp;fp=fopen(filename,"r");if(fp==NULL){printf("Could not open input file.\n");exit(EXIT_FAILURE);}// Count how many we needsize_tlines=0;while(!feof(fp)){if(fgetc(fp)=='\n')lines++;}lines++;// The last line has no newline, but we should count it.rewind(fp);int*entries=malloc(sizeof(int)*lines);charbuff[5];// No numbers greater than 4 digitsfor(size_ti=0;i<lines;i++){fgets(buff,6,fp);entries[i]=atoi(buff);}fclose(fp);*count=lines;returnentries;}/// Part 1 finds the two entries that add up to 2020 exactly and returns/// their product, or -1 if none are found.intpart1(int*entries,size_tcount){for(size_ti=0;i<count;i++){for(size_tj=i+1;j<count;j++){if(entries[i]+entries[j]==2020){returnentries[i]*entries[j];}}}return-1;}/// Part 2 is the same as Part 1, but we're looking for a trio of numbers/// that sum up to 2020. Returns their product or -1 if no trio is found.intpart2(int*entries,size_tcount){for(size_ti=0;i<count;i++){for(size_tj=i+1;j<count;j++){for(size_tk=j+1;k<count;k++){if(entries[i]+entries[j]+entries[k]==2020){returnentries[i]*entries[j]*entries[k];}}}}return-1;}intmain(){size_tcount;int*entries=parse("day1/day1.txt",&count);printf("The 2020 pair product is: %d\n",part1(entries,count));printf("The 2020 triplet product is: %d\n",part2(entries,count));returnEXIT_SUCCESS;}
I'm also doing it in C this year! I'm also fairly new to the language and pretty much the only thing I've done differently is use fscanf instead of fgets in conjunction with atoi
(+ I've optimised a little bit to do both parts in the same loop)
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
I'm going to try to do my solutions in C. C is something I'm learning, so if anybody has any pointers (LOL), please don't hesitate to critique my code.
I didn't do anything crazy today. I just threw loops at it to see if it was fast enough. It solved before I could blink, so I'm not going to work any harder at improving speed.
I'm also doing it in C this year! I'm also fairly new to the language and pretty much the only thing I've done differently is use
fscanfinstead offgetsin conjunction withatoi(+ I've optimised a little bit to do both parts in the same loop)