nichobi

joined 2 years ago
[โ€“] [email protected] 3 points 1 year ago

Part 1 felt fairly pretty simple in Haskell:

import Data.Char (isDigit)

main = interact solve

solve :: String -> String
solve = show . sum . map (read . (\x -> [head x, last x]) . filter isDigit) . lines

Part 2 was more of a struggle, though I'm pretty happy with how it turned out. I ended up using concatMap inits . tails to generate all substrings, in order of appearance so one3m becomes ["","o","on","one","one3","one3m","","n","ne","ne3","ne3m","","e","e3","e3m","","3","3m","","m",""]. I then wrote a function stringToDigit :: String -> Maybe Char which simultaneously filtered out the digits and standardised them as Chars.

import Data.List (inits, tails)
import Data.Char (isDigit, digitToInt)
import Data.Maybe (mapMaybe)

main = interact solve

solve :: String -> String
solve = show . sum . map (read . (\x -> [head x, last x]) . mapMaybe stringToDigit . concatMap inits . tails) . lines
--                             |string of first&last digit| |find all the digits |   |all substrings of line|

stringToDigit "one"   = Just '1'
stringToDigit "two"   = Just '2'
stringToDigit "three" = Just '3'
stringToDigit "four"  = Just '4'
stringToDigit "five"  = Just '5'
stringToDigit "six"   = Just '6'
stringToDigit "seven" = Just '7'
stringToDigit "eight" = Just '8'
stringToDigit "nine"  = Just '9'
stringToDigit [x]
  | isDigit x         = Just x
  | otherwise         = Nothing
stringToDigit _       = Nothing

I went a bit excessively Haskell with it, but I had my fun!

[โ€“] [email protected] 14 points 1 year ago (1 children)

Hello, sponsorblockcast creator here! Happy to finally see a Go implementation, great work! I ended up writing a short blog post summarising the history of the SponsorBlock clients for Chromecasts.

Best of luck in your continued development!