47 lines
1.5 KiB
Haskell
Executable file
47 lines
1.5 KiB
Haskell
Executable file
#! /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)
|
|
import Data.List (sort)
|
|
|
|
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)
|
|
|
|
solvePart2 :: [String] -> Int
|
|
solvePart2 = fst . head . (filter (\(x,y) -> x /= y)) . (zip [13..]) . sort . (map seatID)
|
|
|
|
main :: IO ()
|
|
main = do
|
|
putStrLn "Day 5 - Part 1"
|
|
print $ seatID "FBFBBFFRLR"
|
|
print $ seatID "BFFFBBFRRR"
|
|
print $ seatID "FFFBBBFRRR"
|
|
print $ seatID "BBFFBBFRLL"
|
|
input <- lines <$> readFile "day5/input"
|
|
putStrLn $ "Solution of part 1"
|
|
print $ solvePart1 input
|
|
putStrLn $ "Solution of part 2"
|
|
print $ solvePart2 input
|