-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path5b.hs
54 lines (38 loc) · 1.53 KB
/
5b.hs
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
50
51
52
53
54
{-# LANGUAGE OverloadedStrings #-}
import Data.Function
import Data.List
import Prelude
magicRowPartitionLength = 7
type Row = Int
type Seat = Int
data Half = LeftH | RightH
type PartitionSteps = [Char]
type PartitionChars = (Char, Char)
type PartitionInts = (Int, Int)
main = do
input <- getContents
putStr $ show $ fn $ lines input
getNextHalf :: Half -> PartitionInts -> (Int, Int)
getNextHalf LeftH (left, right) = (left, right - (right - left + 1) `div` 2)
getNextHalf RightH (left, right) = (left + (right - left + 1) `div` 2, right)
walkPartition :: PartitionChars -> PartitionInts -> String -> Int
walkPartition pChars (left, right) [char] = if char == fst pChars then left else right
walkPartition pChars pInts (head : xs)
| head == fst pChars = walkPartition pChars (getNextHalf LeftH pInts) xs
| head == snd pChars = walkPartition pChars (getNextHalf RightH pInts) xs
genRow :: PartitionSteps -> Row
genRow = walkPartition ('F', 'B') (0, 127)
genSeat :: PartitionSteps -> Seat
genSeat = walkPartition ('L', 'R') (0, 7)
genPlaceId :: (String, String) -> Int
genPlaceId (row, seat) = (genRow row * 8) + genSeat seat
findAllMissing :: [Int] -> [Int] -> [Int]
findAllMissing acc [] = acc
findAllMissing acc [a, b]
| succ a == b = acc
| otherwise = [succ a .. pred b] ++ acc
findAllMissing acc (a : b : xs)
| succ a == b = findAllMissing acc (b : xs)
| otherwise = findAllMissing ([succ a .. pred b] ++ acc) (b : xs)
fn :: [String] -> [Int]
fn xs = findAllMissing [] $ sort $ map (genPlaceId . splitAt magicRowPartitionLength) xs