Today was the first day we got different params for the sample solution, so I had to think about how to make that work with my puzzle runner. I don't love the solution, but it works. Thinking I might do a quick post on the runner, not sure if that would interest anyone...
Overall this was a fun little exercise, not super challenging, though I did have a harder time trying to condense the code for this solution. Not something I typically do day-to-day, but it's a code challenge, not production code 🥳
Anyway, here's my solution for the day
Part1
publicclassPart1:Puzzle<long[],long>{publicoverridelongSampleAnswer=>127;protectedintpreamble=25;publicoverridelong[]ParseInput(stringrawInput)=>rawInput.Split(Environment.NewLine).Where(line=>line.Length>0).Select(line=>long.Parse(line)).ToArray();publicoverridelongSolve(long[]input)=>FindInvalidNumber(input)??thrownewSystem.Exception("Result not found!");publicoverrideboolValidateSample(long[]input){// First time we hit a sample with different params as the solution.preamble=5;returnbase.ValidateSample(input);}protectedlong?FindInvalidNumber(long[]input){for(inti=preamble;i<input.Length;i++){varwindow=input[(i-preamble)..(i)];if(TargetSumExistsInWindow(input[i],window)==false){returninput[i];}}returnnull;// not found}privateboolTargetSumExistsInWindow(longtarget,long[]window){boolfound=false;for(varj=0;j<window.Length;j++){// Bail early. One half of the sum is already greater than the result.if(window[j]>target){continue;}// If already true, don't set back to false.found=window.Any(x=>x!=window[j]&&x+window[j]==target)||found;}returnfound;}}
Part 2
publicclassPart2:Part1{publicoverridelongSampleAnswer=>62;publicoverridelongSolve(long[]input){varinvalidNumber=FindInvalidNumber(input)??thrownewSystem.Exception("Result not found!");varrange=FindRangeSummingTo(invalidNumber,input)??thrownewSystem.Exception("Result not found!");returnrange!.Start+range!.End;}(longStart,longEnd)?FindRangeSummingTo(longtarget,long[]input){for(vari=0;i<input.Length;i++){longsum=0;for(varj=i;j<input.Length;j++){if((sum+=input[j])==target){return(input[i..j].Min(),input[i..j].Max());}if(sum>target){break;};}}returnnull;}}
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.
Today was the first day we got different params for the sample solution, so I had to think about how to make that work with my puzzle runner. I don't love the solution, but it works. Thinking I might do a quick post on the runner, not sure if that would interest anyone...
Overall this was a fun little exercise, not super challenging, though I did have a harder time trying to condense the code for this solution. Not something I typically do day-to-day, but it's a code challenge, not production code 🥳
Anyway, here's my solution for the day
Part1
Part 2