adventofcode-2020/day5/main.hs

42 lines
1.3 KiB
Haskell
Raw Normal View History

2020-12-07 10:57:34 +01:00
#! /usr/bin/env -S"ANSWER=42" nix-shell
#! nix-shell -p ghcid
#! nix-shell -p "haskellPackages.ghcWithPackages (p: with p; [])"
#! nix-shell -i "ghcid -c 'ghci -Wall' -T main"
{-# LANGUAGE OverloadedStrings #-}
-- import Debug.Trace (trace)
binaryPartitionWalker :: String -> Int -> Int -> (String, Int, Int)
binaryPartitionWalker ('F':s) begin end =
binaryPartitionWalker s begin (((end - begin) `div` 2) + begin)
binaryPartitionWalker ('B':s) begin end =
binaryPartitionWalker s (((end - begin) `div` 2) + begin + 1) end
binaryPartitionWalker ('L':s) begin end =
binaryPartitionWalker ('F':s) begin end
binaryPartitionWalker ('R':s) begin end =
binaryPartitionWalker ('B':s) begin end
binaryPartitionWalker "" b e = ("",b,e)
binaryPartitionWalker _ _ _ = undefined
seatID :: String -> Int
seatID str = row * 8 + seat
where
(rowStr, seatStr) = splitAt 7 str
(_,row,_) = binaryPartitionWalker rowStr 0 127
(_,seat,_) = binaryPartitionWalker seatStr 0 7
solvePart1 :: [String] -> Int
solvePart1 = maximum . (map seatID)
main :: IO ()
main = do
putStrLn "Day 5 - Part 1"
print $ seatID "FBFBBFFRLR"
print $ seatID "BFFFBBFRRR"
print $ seatID "FFFBBBFRRR"
print $ seatID "BBFFBBFRLL"
putStrLn $ "Solution of part 1"
input <- lines <$> readFile "day5/input"
print $ solvePart1 input