Brute forced part 1 in Haskell for its amazing list manipulations. But that's not going to work for part 2. Half the pattern is zeros in each row so clearly there are ways to optimise. Will need to mull it over.
importData.Char(isSpace,ord)importData.List(dropWhileEnd,intercalate,transpose)importTest.HspecbasePattern=[0,1,0,-1]zeroChar=ord'0'strip::String->Stringstrip=dropWhileisSpace.dropWhileEndisSpaceparseInput::String->[Int]parseInput=map(flip(-)zeroChar.ord)showOutput::[Int]->StringshowOutput=intercalate"".mapshowpatternForIndex::Int->[Int]patternForIndexn=drop1$concat$repeat$concat$transpose$replicatenbasePatternpairProduct::(Int,Int)->IntpairProduct(x,y)=x*youtputRow::[Int]->Int->IntoutputRowinputn=(abs$sum$mappairProduct$zipinput(patternForIndexn))`mod`10-- extra parameter makes this useful in foldsfftPhase::[Int]->Int->[Int]fftPhaseinput_=map(outputRowinput)[1..lengthinput]runPhases::Int->String->StringrunPhasesninput=showOutput$foldlfftPhase(parseInputinput)[1..n]fftPhaseTests=doletphases=scanlfftPhase(parseInput"12345678")[1..4]mapshowOutputphases`shouldBe`["12345678","48226158","34040438","03415518","01029498"]largerTests=dorunPhases100"80871224585914546619083218645595"`shouldStartWith`"24176176"runPhases100"19617804207202209144916044189917"`shouldStartWith`"73745418"runPhases100"69317163492948606335995924319873"`shouldStartWith`"52432133"part1input=letresult=take8$runPhases100inputinputStrLn$"Part 1 .. "++resultmain=dofftPhaseTestslargerTestsinput<-fmapstrip$readFile"input.txt"part1input
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.
Brute forced part 1 in Haskell for its amazing list manipulations. But that's not going to work for part 2. Half the pattern is zeros in each row so clearly there are ways to optimise. Will need to mull it over.