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)
|
2020-12-07 11:11:12 +01:00
|
|
|
import Data.List (sort)
|
2020-12-07 10:57:34 +01:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
2020-12-07 11:11:12 +01:00
|
|
|
solvePart2 :: [String] -> Int
|
|
|
|
solvePart2 = fst . head . (filter (\(x,y) -> x /= y)) . (zip [13..]) . sort . (map seatID)
|
|
|
|
|
2020-12-07 10:57:34 +01:00
|
|
|
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"
|
2020-12-07 11:11:12 +01:00
|
|
|
putStrLn $ "Solution of part 1"
|
2020-12-07 10:57:34 +01:00
|
|
|
print $ solvePart1 input
|
2020-12-07 11:11:12 +01:00
|
|
|
putStrLn $ "Solution of part 2"
|
|
|
|
print $ solvePart2 input
|