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!
Did a bit of tricky business after looking over the test cases and my input. Linear time with no recursion.
Because the numbers in my input were either 3 or 1 apart, never 2, and there were never more than 4 1's in a row before a 3, I pre-calculated the # of possibilities each of the possible patterns generated and then multiplied together to find all possible combinations.
3 - 3 (e.g. 4, 7, 10) => only 1 possible configuration
3 - 1 - 3 (e.g. 4, 7, 8, 11) => only 1 possible configuration
3 - 1 - 1 - 3 (e.g. 4, 7, 8, 9, 12) => 2 possible configurations (drop the middle number or don't)
3 - 1 - 1 - 1 - 3 (e.g. 4, 7, 8, 9, 10, 13) => 4 possible configurations (2 each for 8 and 9)
3 - 1 - 1 - 1 - 1 - 3 (e.g. 4, 7, 8, 9, 10, 11, 14) => 7 possible configurations (at least one of 8, 9, 10 must be present, so 2^3 - 1 since dropping all 3 not an option)
Then I rolled through the list producting up all the values for each "cell" of ones. The only thing I had an issue with is that I didn't think about the fact that the result was going to be a long long int, so I chased my tail printing out regular ints for a while even though my algorithm was right.
Also, TIL that C has a build-in sorting function called qsort!
#include "Day10.h"
#include <stdio.h>
#include "parsing.h"
/// Day 10: Adapter Array////// Use power adapters to calculate "joltage" jumps from connection/// to connection./// In a sorted array of adapters, return the number of jumps of size 1/// multiplied by the jumps of size 3./// Parse the input file, one number per line into a list of integers.staticint*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);// It's going to be easier if we manually attach our outlet and device*count+=2;int*numbers=(int*)malloc(sizeof(int)**count);numbers[0]=0;numbers[*count-1]=4**count;// Something so huge goes to the end.for(inti=1;i<*count-1;i++){charbuf[5]={0};fgets(buf,5,fp);numbers[i]=atoi(buf);}fclose(fp);returnnumbers;}/// Integer comparison function for qsort.intcmp(constvoid*a,constvoid*b){return(*(int*)a-*(int*)b);}intpart1(constchar*filename){intcount=0;int*numbers=parse(filename,&count);qsort(numbers,count,sizeof(int),cmp);numbers[count-1]=numbers[count-2]+3;intones=0;intthrees=0;for(inti=1;i<count;i++){intjump=numbers[i]-numbers[i-1];if(jump==1)ones++;elseif(jump==3)threes++;}free(numbers);returnones*threes;}/// How many possibilities does a group of continuous ones yield?staticintpattern_to_variants(intones){switch(ones){case0:return1;case1:return1;case2:return2;case3:return4;case4:return7;default:printf("Crap, didn't account for > 4 ones.\n");exit(EXIT_FAILURE);}}/// Count the number of distinct arrangements of adapters that/// would still work.longlongintpart2(constchar*filename){intcount=0;int*numbers=parse(filename,&count);qsort(numbers,count,sizeof(int),cmp);numbers[count-1]=numbers[count-2]+3;intones=0;longlongintproduct=1;for(inti=1;i<count;i++){intjump=numbers[i]-numbers[i-1];if(jump==1){ones++;}elseif(jump==3){product*=pattern_to_variants(ones);ones=0;}else{printf("Crap, this doesn't work for jumps of 2.\n");exit(EXIT_FAILURE);}}returnproduct;}/// Run both parts.intday10(){printf("====== Day 10 ======\n");printf("Part 1: %d\n",part1("data/day10.txt"));printf("Part 2: %lli\n",part2("data/day10.txt"));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.
Did a bit of tricky business after looking over the test cases and my input. Linear time with no recursion.
Because the numbers in my input were either 3 or 1 apart, never 2, and there were never more than 4 1's in a row before a 3, I pre-calculated the # of possibilities each of the possible patterns generated and then multiplied together to find all possible combinations.
Then I rolled through the list producting up all the values for each "cell" of ones. The only thing I had an issue with is that I didn't think about the fact that the result was going to be a
long long int
, so I chased my tail printing out regular ints for a while even though my algorithm was right.Also, TIL that C has a build-in sorting function called
qsort
!