day 6: rewrite
use Data.Text Signed-off-by: Amneesh Singh <natto@weirdnatto.in>
This commit is contained in:
17
day6.hs
17
day6.hs
@@ -1,16 +1,21 @@
|
|||||||
|
{-# LANGUAGE OverloadedStrings #-}
|
||||||
|
|
||||||
import Data.List (nub)
|
import Data.List (nub)
|
||||||
|
import Lib (readFile')
|
||||||
|
import Data.Text (Text)
|
||||||
|
import qualified Data.Text as T
|
||||||
|
|
||||||
main :: IO ()
|
main :: IO ()
|
||||||
main = do
|
main = do
|
||||||
input <- readFile "day6.in"
|
input <- readFile' "day6.in"
|
||||||
putStr "Q1: "
|
putStr "Q1: "
|
||||||
print $ parse 4 input
|
print $ parse 4 input
|
||||||
putStr "Q2: "
|
putStr "Q2: "
|
||||||
print $ parse 14 input
|
print $ parse 14 input
|
||||||
|
|
||||||
group :: Int -> [Char] -> [[Char]]
|
group :: Int -> Text -> [Text]
|
||||||
group _ [] = []
|
group _ "" = []
|
||||||
group n xs = take n xs : group n (tail xs)
|
group n xs = T.take n xs : group n (T.tail xs)
|
||||||
|
|
||||||
parse :: Int -> String -> Int
|
parse :: Int -> Text -> Int
|
||||||
parse n = (+ n) . length . takeWhile ((< n) . length) . map nub . group n
|
parse n = (+ n) . length . takeWhile ((< n) . length) . map (nub . T.unpack) . group n
|
||||||
|
Reference in New Issue
Block a user