blob: 9e574efd156b7a5284dc8a2a49f9629b9ffff89d (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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 ();
|