Full Stack Functional Programmer focused on Accessibility, Testing, and Documentation/Technical Writing. Trying to be less clever and more kind. He/Him
Nice solve. I did a similar method to your js version, but in F#. Haskell definitely has some more elegant options though. F# isn't lazy, so all the solutions I thought of where I made pairs or triples would have me going through the entire list multiple times. But I feel good that someone else looked at it and immediately saw the folds. Because folds are amazing. :D
// read the data in as a list of strings then convert to integersopenSystem.IOletdata=File.ReadAllLines(@"day1part1.txt")|>Array.mapint
Part one
letfolder(increases,last)next=ifnext>lastthenincreases+1,nextelseincreases,next// System.Int32.MaxValue is .NET's largest int32 so the first value will always decreaseletday1part1solution=fst<|Array.foldfolder(0,System.Int32.MaxValue)data
Part two
letfolder2(increases,last3)(index,next)=// we just fill up the triple for the first 3 iterations. ifindex<3thenincreases,last3@[next]elseletnext3=List.taillast3@[next]ifList.sumnext3>List.sumlast3thenincreases+1,next3elseincreases,next3// we can use the indexed function to convert our list of values to (index value) pairsletday1part2solution=fst<|Array.foldfolder2(0,[])(Array.indexeddata)
This looks great, thanks for sharing! F# is on my to do list of languages to dig into more. I was actually looking for the pipe operator from F# when trying to make the Haskell version, but sadly, it isn't in the prelude. It's cool that you used a fold with a tuple!
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.
Nice solve. I did a similar method to your js version, but in F#. Haskell definitely has some more elegant options though. F# isn't lazy, so all the solutions I thought of where I made pairs or triples would have me going through the entire list multiple times. But I feel good that someone else looked at it and immediately saw the folds. Because folds are amazing. :D
Part one
Part two
This looks great, thanks for sharing! F# is on my to do list of languages to dig into more. I was actually looking for the pipe operator from F# when trying to make the Haskell version, but sadly, it isn't in the prelude. It's cool that you used a fold with a tuple!