Advent of Code solutions https://adventofcode.com
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.

34 lines
1.1KB

  1. main :: IO()
  2. main = do
  3. input <- readFile "input"
  4. print . foldr max 0 . map seatId . map seat . lines $ input
  5. seatId :: (Int, Int) -> Int
  6. seatId (row, col) = (row * 8) + col
  7. seat :: String -> (Int, Int)
  8. seat line = (row fst, col snd) where (fst, snd) = splitAt 7 line
  9. row :: String -> Int
  10. row = partition 'F' 'B' 0 127
  11. col :: String -> Int
  12. col = partition 'L' 'R' 0 7
  13. -- Perform binary space partition. The first two arguments are the characters
  14. -- which indicate that the lower or upper part of the space will be used
  15. -- respectively. The second two are the starting upper and lower bounds of the
  16. -- search space. The last is the instructions string.
  17. partition :: Char -> Char -> Int -> Int -> String -> Int
  18. partition _ _ min _ [] = min
  19. partition lowerC upperC min max str@(c:cs)
  20. | c == upperC = partition' (mid min max) max cs
  21. | c == lowerC = partition' min (mid min max) cs
  22. | otherwise = partition' min max cs
  23. where partition' = partition lowerC upperC
  24. -- Find the midpoint of two integers, e.g. the midpoint of 0 and 7 is 4
  25. mid :: Int -> Int -> Int
  26. mid a b = div (a + b + 1) 2