aboutsummaryrefslogtreecommitdiff
path: root/2020/15a.hs
diff options
context:
space:
mode:
Diffstat (limited to '2020/15a.hs')
-rw-r--r--2020/15a.hs49
1 files changed, 49 insertions, 0 deletions
diff --git a/2020/15a.hs b/2020/15a.hs
new file mode 100644
index 0000000..9e574ef
--- /dev/null
+++ b/2020/15a.hs
@@ -0,0 +1,49 @@
+module Day15A where
+
+import System.Environment (getArgs)
+import Text.Printf
+
+-- Parse a string, with one number per line
+parseInput :: String -> [Int]
+parseInput = map read . lines
+
+-- Parse a file given the path
+-- Returns list of instructions
+parseFromFile :: String -> IO [Int]
+parseFromFile s = do
+ contents <- readFile s;
+ return $ parseInput contents;
+
+-- Index of an element in a list
+indexOf :: Eq a => [a] -> a -> Maybe Int
+indexOf [] t = Nothing
+indexOf (x:xs) t | x == t = Just 0
+ | otherwise = case indexOf xs t of
+ Nothing -> Nothing
+ Just x -> Just (x + 1)
+
+-- Index of an element in a list, starting from the right / end
+indexOfRight :: Eq a => [a] -> a -> Maybe Int
+indexOfRight xs t = case indexOf (reverse xs) t of
+ Nothing -> Nothing
+ Just x -> Just (length xs - x - 1)
+
+-- Run until the given turn
+runTillTurn :: [Int] -> Int -> [Int]
+runTillTurn ns l | length ns == l = ns
+ | otherwise = runTillTurn ns' l
+ where x = last ns
+ ns' = case indexOfRight (take (length ns - 1) ns) x of
+ Just x -> ns ++ [(length ns - 1) - x]
+ Nothing -> ns ++ [0]
+
+-- runghc 15a.hs inputs/day15
+main :: IO ()
+main = do
+ args <- getArgs;
+ ns <- parseFromFile (head args);
+ let xs = runTillTurn ns 30000000;
+
+ printf "Answer = %d\n" (last xs) :: IO ();
+
+ return ();