Ryan is an engineer in the Sacramento Area with a focus in Python, Ruby, and Rust. Bash/Python Exercism mentor. Coding, physics, calculus, music, woodworking. Looking for work!
Had some extra time tonight so I got ahead of the curve. The time complexity isn't great, with the loops being a nasty hairy mess, but it works fine :)
#include "Day9.h"
#include <limits.h>
#include <stdbool.h>
#include <stdio.h>
#include "parsing.h"
/// Day 9: Encoding Error/// /// Find the number that can't be made by a combination of the numbers/// preceding it.#define MAX_NUMBER_STRING_SIZE 16
/// Parse the input file into a list of integers, one per line.int*parse(constchar*filename,int*count){FILE*fp;fp=fopen(filename,"r");if(fp==NULL){printf("Couldn't open file.\n");exit(EXIT_FAILURE);}*count=count_lines(fp);int*numbers=(int*)malloc(sizeof(int)**count);for(inti=0;i<*count;i++){charbuf[MAX_NUMBER_STRING_SIZE]={0};fgets(buf,MAX_NUMBER_STRING_SIZE,fp);numbers[i]=atoi(buf);}fclose(fp);returnnumbers;}/// Decides if a number is a 'valid number,' i.e. there is some/// combination of two numbers in the preceding 'preamble_length' /// numbers that add up to it.boolvalid_number(int*numbers,inti,intpreamble_length){// Add each pair of numbers in the preceding 'preamble_length' #'s// If there's any pair that add up to the i'th number, it's valid.for(intj=0;j<preamble_length;j++){for(intk=j+1;k<preamble_length;k++){if(numbers[i-preamble_length+j]+numbers[i-preamble_length+k]==numbers[i]){returntrue;}}}returnfalse;}/// Find the one number that isn't 'valid' per the above definition.intpart1(constchar*filename,intpreamble_length){intcount=0;int*numbers=parse(filename,&count);for(inti=preamble_length;i<count;i++){if(!valid_number(numbers,i,preamble_length)){returnnumbers[i];}}return-1;}/// There is a set of contiguous numbers, two or greater in length,/// that, when added up, equal the invalid number from part 1./// Return the minimum number in that set + the max in that set.intpart2(constchar*filename,intpreamble_length){intcount=0;int*numbers=parse(filename,&count);intinvalid_target=part1(filename,preamble_length);// Check each size from 2 all the way up to all of the numbersfor(intsize=2;size<=count;size++){// At each size, shift the window from front to back, one by one.for(inti=0;i+size-1<count;i++){// Add up that window of numbers and see if it matchesinttotal=0;for(intj=0;j<size;j++){total+=numbers[i+j];}// If it matches, find the min and max values in that window// and return their sum.if(total==invalid_target){intmin=INT_MAX;intmax=0;// Simultaneously find the min and max in the range.for(intj=0;j<size;j++){if(numbers[i+j]>max)max=numbers[i+j];if(numbers[i+j]<min)min=numbers[i+j];}returnmin+max;}}}return-1;}/// Run both partsintday9(){printf("====== Day 9 ======\n");printf("Part 1: %d\n",part1("data/day9.txt",25));printf("Part 2: %d\n",part2("data/day9.txt",25));returnEXIT_SUCCESS;}
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.
Had some extra time tonight so I got ahead of the curve. The time complexity isn't great, with the loops being a nasty hairy mess, but it works fine :)