solve: day 5
- solve: day 5, part 1 - solve: day 5, part 2
This commit is contained in:
parent
7cd2925927
commit
4cd13c0129
4 changed files with 60 additions and 0 deletions
|
@ -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.
|
28
2020/05/day5.hs
Normal file
28
2020/05/day5.hs
Normal file
|
@ -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
|
9
2020/05/part1.hs
Executable file
9
2020/05/part1.hs
Executable file
|
@ -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
|
14
2020/05/part2.hs
Executable file
14
2020/05/part2.hs
Executable file
|
@ -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
|
Loading…
Reference in a new issue