From 6d831177d2c874ca9888a70188f99ed92c900481 Mon Sep 17 00:00:00 2001 From: Yuchen Pei Date: Thu, 7 Dec 2017 15:34:27 +0100 Subject: finished puzzle5, 6, 7part1 --- Puzzle6.hs | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 Puzzle6.hs (limited to 'Puzzle6.hs') diff --git a/Puzzle6.hs b/Puzzle6.hs new file mode 100644 index 0000000..721aa9b --- /dev/null +++ b/Puzzle6.hs @@ -0,0 +1,41 @@ +import Data.List (elemIndex) +import Data.List.Split (splitOn) +import Data.Maybe (fromJust) +--import Control.Monad (liftM2) + +incList :: Int -> Int -> Int -> [Int] +incList n q r = replicate r (q + 1) ++ replicate (n - r - 1) q + +f :: [Int] -> [Int] +f xs = (drop (n - m) zs) ++ (take (n - m) zs) + where p = maximum xs + n = length xs + q = p `div` n + r = p `mod` n + m = (fromJust $ elemIndex p xs) + 1 + ys = drop m (cycle xs) + zs = (take (n - 1) $ zipWith (+) ys (incList n q r)) ++ [q] + +g :: [[Int]] -> [[Int]] +g xs = (f $ head xs):xs + +looped :: [Int] -> [[Int]] +looped xs = until (\ys -> (head ys) `elem` (tail ys)) g [xs] + +solve1' :: [Int] -> Int +solve1' xs = (length $ looped xs) - 1 + +solve2' :: [Int] -> Int +solve2' xs = let (y:ys) = looped xs in (fromJust $ elemIndex y ys) + 1 + +parseInput :: [Char] -> [Int] +parseInput xs = map read $ splitOn " " xs + +solve1 :: [Char] -> Int +solve1 = solve1' . parseInput + +solve2 :: [Char] -> Int +solve2 = solve2' . parseInput + +input :: [Char] +input = "4 10 4 1 8 4 9 14 5 1 14 15 0 15 3 5" -- cgit v1.2.3