diff --git a/2020/05/README.md b/2020/05/README.md index e69de29..e9468ba 100644 --- a/2020/05/README.md +++ b/2020/05/README.md @@ -0,0 +1,9 @@ +# Day 5 + +## Part 1 + +It's just binary where `F` and `L` are `0` and `B` and `R` are `1`. + +## Part 2 + +At first I just looked at the difference between lists to get the answer. Since updated to find the first difference in the two lists (of all possible seats and the actual seats) after skipping the gap at the start. diff --git a/2020/05/day5.hs b/2020/05/day5.hs new file mode 100644 index 0000000..bf15928 --- /dev/null +++ b/2020/05/day5.hs @@ -0,0 +1,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 diff --git a/2020/05/part1.hs b/2020/05/part1.hs new file mode 100755 index 0000000..d12c5bb --- /dev/null +++ b/2020/05/part1.hs @@ -0,0 +1,9 @@ +#!/usr/bin/env runghc + +import Day5 + +main = interact solve + +solve = show . maxSeatId . map (read :: String -> BoardingPass) . lines + +maxSeatId = maximum . map seatId diff --git a/2020/05/part2.hs b/2020/05/part2.hs new file mode 100755 index 0000000..8be8c42 --- /dev/null +++ b/2020/05/part2.hs @@ -0,0 +1,14 @@ +#!/usr/bin/env runghc + +import Day5 +import Data.List + +main = interact solve + +solve = show . mySeat . map (seatId . read) . lines + +mySeat input = mySeat' + where + boardingPasses = sort input + start = head boardingPasses + mySeat' = fst . head . dropWhile (uncurry (==)) $ zip [start..] boardingPasses