-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.hs
49 lines (37 loc) · 1 KB
/
run.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
import Data.Bits (xor)
import Data.Ord (comparing)
import Data.Maybe
import Data.List
parseAll = lines
binary :: Int -> String -> Int
binary n = fst . foldl go (0, n)
where go (l, h) v
| v == 'F' || v == 'L' = (l, (l + h) `div` 2)
| otherwise = ((l + h + 1) `div` 2, h)
findSeat :: String -> (Int, Int)
findSeat spec =
let (rspec, cspec) = splitAt 7 spec
in (binary 127 rspec, binary 7 cspec)
seatId :: (Int, Int) -> Int
seatId (r, c) = r * 8 + c
posFromId :: Int -> (Int, Int)
posFromId = (`divMod` 8)
part1 :: [String] -> Int
part1 = maximum
. map seatId
. map findSeat
allIds :: [Int]
allIds = [0..127*8]
missing :: [Int] -> [Int]
missing = (allIds \\)
identifySeat :: [Int] -> Int
identifySeat xs =
head $ [m | (b:m:a:_) <- tails xs
, b + 1 /= m
, m + 1 /= a]
part2 = identifySeat . missing . map (seatId . findSeat)
example = "FBFBBFFRLR"
main = do
input <- parseAll <$> readFile "input.txt"
print (part1 input)
print (part2 input)