From faba37ef4bbc371de99eb0a5e209d0eb7d732fd9 Mon Sep 17 00:00:00 2001 From: sgoudham Date: Tue, 6 Dec 2022 07:02:49 +0000 Subject: [PATCH] chore: complete day 6 --- solutions/Days/Day06.hs | 33 ++++++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/solutions/Days/Day06.hs b/solutions/Days/Day06.hs index 60418bc..9c91af3 100644 --- a/solutions/Days/Day06.hs +++ b/solutions/Days/Day06.hs @@ -1,16 +1,35 @@ module Days.Day06 (day06) where import AOC (Solution (..)) -import qualified Data.Text as T +import Data.List +import Data.Maybe +import Data.Text qualified as T day06 :: Solution day06 = Solution parseInput part1 part2 -parseInput :: T.Text -> a -parseInput = error "parseInput not defined for day 06" +isUnique :: String -> Bool +isUnique str = length (nub str) == length str -part1 :: a -> Int -part1 = error "part1 not defined for day 06" +detectUnique :: Int -> String -> Char -> String +detectUnique n acc x = + if isUnique (take n $ reverse acc) + then acc ++ "0" + else acc ++ [x] -part2 :: a -> Int -part2 = error "part2 not defined for day 06" +parseInput :: T.Text -> String +parseInput = T.unpack + +part1 :: String -> Int +part1 input = fromJust $ elemIndex '0' (foldl (detectUnique chars) accumulator iterable) + where + chars = 4 + accumulator = take chars input + iterable = drop chars input + +part2 :: String -> Int +part2 input = fromJust $ elemIndex '0' (foldl (detectUnique chars) accumulator iterable) + where + chars = 14 + accumulator = take chars input + iterable = drop chars input