-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ex5.hs
33 lines (25 loc) · 808 Bytes
/
Ex5.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
module Ex5 where
import Data.List
solve :: String -> Int
solve seatlines = solve2 (lines seatlines)
solve2 :: [String] -> Int
solve2 seats = findSeat (sort (map seatId seats)) (-1)
findSeat :: [Int] -> Int -> Int
findSeat [] x = x
findSeat (s:ss) prev
| prev + 2 == s = prev + 1
| otherwise = findSeat ss s
solve1 :: [String] -> Int
solve1 seats = foldr max 0 (map seatId seats)
seatId :: String -> Int
seatId seat = 8*row + column
where row = index (take 7 seat)
column = index (drop 7 seat)
index :: String -> Int
index chars = sum (zipWith (\x y -> (topHalf x)*2^y) chars (reverse [0..length chars-1]))
topHalf :: Char -> Int
topHalf 'B' = 1
topHalf 'R' = 1
topHalf 'F' = 0
topHalf 'L' = 0
topHalf c = error ("the char:"++[c]++": caused the problem.")