Ah, this one lead to a nice relaxing day :-) Nothing fancy in my Haskell implementation, but fine all the same. Relies heavely on lazyness to avoid over production, but if the feature exists why not use it...
-- helper function to generate tuple perms of a list and pass to a functionperms::Eqt=>(t->t->a)->[t]->[a]permsfxs=[fxy|(x:ys)<-tails(nubxs),y<-ys]-- asserts if a number is XMAS encrypted correctlyvalid::[Int]->Int->Int->(Bool,Int)validisli=letps=map(\(_,_,z)->z)$perms(\xy->(x,y,x+y))(takelis)n=is!!iin(n`elem`ps,n)-- checks, for a given header length, that each input is valid check::Int->[Int]->[(Bool,Int)]checklis|lengthis<=l+1=[]|otherwise=validisll:checkl(tailis)-- find the first input that is not valid with respec to XMAS encryption task1::Int->[Int]->Inttask1l=snd.head.filter(not.fst).checkl-- find the a contiguous sequence whose sum is equal to the input (calulated by task1) and-- sum the smallest and largest number in that sequencetask2::Int->[Int]->Inttask2n=(\xs->headxs+lastxs).snd.head.filter((n==).fst).map(\xs->(sumxs,sortxs)).filter((>1).length).(:)[].filter(not.null).concatMaptails.initsmain=doxs<-readFile"day9_input"<&>lines<&>fmap(read::String->Int)letx=task125xsprintxprint(task2xxs)
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.
Ah, this one lead to a nice relaxing day :-) Nothing fancy in my Haskell implementation, but fine all the same. Relies heavely on lazyness to avoid over production, but if the feature exists why not use it...