-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path5a.hs
49 lines (32 loc) · 1.3 KB
/
5a.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
{-# LANGUAGE OverloadedStrings #-}
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
maxAcc :: Int -> Int -> Int
maxAcc acc x = if x > acc then x else acc
maxAccList :: [Int] -> Int
maxAccList = foldl maxAcc 0
fn :: [String] -> Int
fn xs = maxAccList $ map (genPlaceId . splitAt magicRowPartitionLength) xs