-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday5.hs
28 lines (21 loc) · 867 Bytes
/
day5.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
module Day5 (BoardingPass(BoardingPass), seatId) where
data BoardingPass = BoardingPass { row :: Int, column :: Int }
deriving (Show)
binaryStringToInt b = binaryStringToInt' (reverse b) 0 0
where
binaryStringToInt' [] _ acc = acc
binaryStringToInt' ('1':bs) m acc = binaryStringToInt' bs (m+1) (acc + 2^m)
binaryStringToInt' ('0':bs) m acc = binaryStringToInt' bs (m+1) acc
charToBinaryDigit 'F' = '0'
charToBinaryDigit 'L' = '0'
charToBinaryDigit 'B' = '1'
charToBinaryDigit 'R' = '1'
stringToInt = binaryStringToInt . map charToBinaryDigit
readBoardingPass str =
let (str', rest) = splitAt 10 str
(rowS, colS) = splitAt 7 str'
in
(BoardingPass (stringToInt rowS) (stringToInt colS), rest)
instance Read BoardingPass where
readsPrec _ str = [readBoardingPass str]
seatId (BoardingPass row column) = row * 8 + column