You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

41 lines
1.0KB

  1. main :: IO ()
  2. main = do
  3. raw <- readFile "day3.txt"
  4. let ls = map parse $ lines raw
  5. ansA = solveSlope 3 1 ls
  6. ansB = foldr (*) 1 $
  7. [ (solveSlope 1 1 ls)
  8. , (solveSlope 3 1 ls)
  9. , (solveSlope 5 1 ls)
  10. , (solveSlope 7 1 ls)
  11. , (solveSlope 1 2 ls)
  12. ]
  13. in do
  14. putStrLn $ "day3a: " ++ (show ansA)
  15. putStrLn $ "day3b: " ++ (show ansB)
  16. parse :: String -> [Bool]
  17. parse = map (== '#')
  18. solveLine :: [Bool] -> Int -> Bool
  19. solveLine [] _ = error "empty list"
  20. solveLine bs i = bs !! index
  21. where
  22. index = i `mod` (length bs)
  23. solveSlope :: Int -> Int -> [[Bool]] -> Int
  24. solveSlope r d bs =
  25. length $ filter (== True) $ solveSlope' r d bs 0 []
  26. solveSlope' :: Int -> Int -> [[Bool]] -> Int -> [Bool] -> [Bool]
  27. solveSlope' _ _ [] _ as = as
  28. solveSlope' r d bs@(b:cs) i as =
  29. if ns == []
  30. then as
  31. else solveSlope' r d ns ni $ (solveLine (head ns) ni) : as
  32. where
  33. ni = (i + r) `mod` (length b)
  34. ns = if (length bs) <= d
  35. then []
  36. else (drop d bs)