mirror of
https://github.com/mx42/adventofcode.git
synced 2026-01-14 22:09:50 +01:00
Add 2020 day 5
This commit is contained in:
39
haskellAoC/src/Y2020/Day05.hs
Normal file
39
haskellAoC/src/Y2020/Day05.hs
Normal file
@@ -0,0 +1,39 @@
|
||||
module Y2020.Day05 (y20day05) where
|
||||
|
||||
import Data.List
|
||||
|
||||
parseRow :: [Char] -> Int
|
||||
parseRow ('B':[]) = 1
|
||||
parseRow ('F':[]) = 0
|
||||
parseRow ('B':xs) = 1 + 2 * parseRow xs
|
||||
parseRow ('F':xs) = 2 * parseRow xs
|
||||
|
||||
parseCol :: [Char] -> Int
|
||||
parseCol "RRR" = 7
|
||||
parseCol "RRL" = 6
|
||||
parseCol "RLR" = 5
|
||||
parseCol "RLL" = 4
|
||||
parseCol "LRR" = 3
|
||||
parseCol "LRL" = 2
|
||||
parseCol "LLR" = 1
|
||||
parseCol "LLL" = 0
|
||||
|
||||
parseInput :: [Char] -> (Int, Int)
|
||||
parseInput input = (row, col)
|
||||
where row = parseRow $ reverse $ (take 7 input)
|
||||
col = parseCol $ drop 7 input
|
||||
|
||||
computeSeatId :: (Int, Int) -> Int
|
||||
computeSeatId (row, col) = row * 8 + col
|
||||
|
||||
findMissingId :: [Int] -> Int
|
||||
findMissingId (x:x2:xs)
|
||||
| x2 == x + 1 = findMissingId (x2:xs)
|
||||
| otherwise = x + 1
|
||||
findMissingId _ = -1
|
||||
|
||||
y20day05 :: [String] -> (String, String)
|
||||
y20day05 input = (part1, part2)
|
||||
where part1 = show $ maximum $ map computeSeatId parsedInput
|
||||
part2 = show $ findMissingId $ sort $ map computeSeatId parsedInput
|
||||
parsedInput = map parseInput input
|
||||
@@ -4,6 +4,7 @@ import Y2020.Day01
|
||||
import Y2020.Day02
|
||||
import Y2020.Day03
|
||||
import Y2020.Day04
|
||||
import Y2020.Day05
|
||||
|
||||
|
||||
year2020 :: String -> [String] -> (String, String)
|
||||
@@ -11,3 +12,4 @@ year2020 "01" = y20day01
|
||||
year2020 "02" = y20day02
|
||||
year2020 "03" = y20day03
|
||||
year2020 "04" = y20day04
|
||||
year2020 "05" = y20day05
|
||||
|
||||
Reference in New Issue
Block a user