Browse Source

2020 day 5

master
Terrana Ninetailed 10 months ago
parent
commit
e9dfbe80a6
3 changed files with 923 additions and 0 deletions
  1. +846
    -0
      2020/day05/input
  2. +33
    -0
      2020/day05/part1.hs
  3. +44
    -0
      2020/day05/part2.hs

+ 846
- 0
2020/day05/input View File

@@ -0,0 +1,846 @@
FBBFFBBLLL
FFBFFFBRLL
FFBBBBFRRL
FBFBBBBRLL
BFBBBBFLLR
FFFBBBBLRR
BFFFFFBLLL
BBFFFBFRRL
FFBFFFFLLR
BFFFBBBRRL
FBFBFFFLRL
FFFBBFBLRR
FBFBFBFLRR
FBBBBFBRRL
BFFBFFBRRR
FBBBFBBRLL
FBFFBFBRLR
BBFBFFFLRL
FFBFFFFRLR
FFBBFBFRRR
BFBBBFBLRR
FFBBFFFLRL
FBBBBFFRLR
FBBBBBBRLR
FFBFBFBLLL
BBFBFBBLLL
FFFFFBBRRL
FBFFBFBRRR
FFFBFFFRLL
BFBFBFFLLL
BFBFFBFLLL
FFFFBFFRRL
FBFFFFFRLR
FBBFFBBLLR
BFFFFFBLRL
BFBFFFBLLR
FBBBBBBLLL
BBFBFBFLLL
FFBFFBFLRR
BBFFFBBRLL
FFBFFFFLLL
FBBFFBFLLL
FFFBBBFRRR
BFBBFBFRLL
FBBFBFFRRL
FBFBBFBLRR
FFBBBBBRLR
FFBFBFFLLL
FBFFFBFRRL
BFFBBFFRLL
BFFBBFFLLR
BBFFBFFRRL
FBFBFBFLLR
BBFFBFBLRL
BFBFBFBLRR
FFBBFBBRRL
BFBBBFBRLR
FBFFFFBLLR
BFBFFBBLRR
BBFBFBFRRR
FFBFBBBRRL
BFBFBBFRLR
FBFBBBBRLR
BFFFFBBRRR
BBFFFBFLLR
FBFFFFBRRL
BBFFBFBLLL
BFFFBBBLLL
FFBFFFBLRR
BFBFBBBRRR
FBFFBFFRRR
BFFBFFBLLL
BFBBFFFLRL
BBFBFFBRLR
FBBFFFFLLL
FBFFFFBLLL
BBFFFFBRLR
BBFBFFBRRL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LRR
FFBFBBBLLL
BFBFFBBRRL
FFFFBBFLRL
BFBBBFBRLL
FFBFFFBLRL
FBBFFFBRLL
BFFBBFBLRR
FFFBBFFLLR
FBBFFBBRRL
FFBFBBBRRR
FBFBFFBLRL
BFBBFBBRRR
BBFFFFBLRL
FBFBFBBLLR
BFFFBBBLRR
BFBFFFBRLR
FFFBBBBLLL
BFFBFFFLRR
BFBBFFFRRL
FBFFBBBLRL
FBFBBFFLRR
FBFFBBFRRL
BBFBFFBRLL

+ 33
- 0
2020/day05/part1.hs View File

@@ -0,0 +1,33 @@
main :: IO()
main = do
input <- readFile "input"
print . foldr max 0 . map seatId . map seat . lines $ input


seatId :: (Int, Int) -> Int
seatId (row, col) = (row * 8) + col

seat :: String -> (Int, Int)
seat line = (row fst, col snd) where (fst, snd) = splitAt 7 line

row :: String -> Int
row = partition 'F' 'B' 0 127

col :: String -> Int
col = partition 'L' 'R' 0 7

-- Perform binary space partition. The first two arguments are the characters
-- which indicate that the lower or upper part of the space will be used
-- respectively. The second two are the starting upper and lower bounds of the
-- search space. The last is the instructions string.
partition :: Char -> Char -> Int -> Int -> String -> Int
partition _ _ min _ [] = min
partition lowerC upperC min max str@(c:cs)
| c == upperC = partition' (mid min max) max cs
| c == lowerC = partition' min (mid min max) cs
| otherwise = partition' min max cs
where partition' = partition lowerC upperC

-- Find the midpoint of two integers, e.g. the midpoint of 0 and 7 is 4
mid :: Int -> Int -> Int
mid a b = div (a + b + 1) 2

+ 44
- 0
2020/day05/part2.hs View File

@@ -0,0 +1,44 @@
import Data.List (sort)


main :: IO()
main = do
input <- readFile "input"
print . mySeat . map seatId . map seat . lines $ input


mySeat :: [Int] -> Int
mySeat = findGap . sort

findGap :: [Int] -> Int
findGap (a:b:rest)
| (b - a) > 1 = b - 1
| otherwise = findGap $ b:rest

seatId :: (Int, Int) -> Int
seatId (row, col) = (row * 8) + col

seat :: String -> (Int, Int)
seat line = (row fst, col snd) where (fst, snd) = splitAt 7 line

row :: String -> Int
row = partition 'F' 'B' 0 127

col :: String -> Int
col = partition 'L' 'R' 0 7

-- Perform binary space partition. The first two arguments are the characters
-- which indicate that the lower or upper part of the space will be used
-- respectively. The second two are the starting upper and lower bounds of the
-- search space. The last is the instructions string.
partition :: Char -> Char -> Int -> Int -> String -> Int
partition _ _ min _ [] = min
partition lowerC upperC min max str@(c:cs)
| c == upperC = partition' (mid min max) max cs
| c == lowerC = partition' min (mid min max) cs
| otherwise = partition' min max cs
where partition' = partition lowerC upperC

-- Find the midpoint of two integers, e.g. the midpoint of 0 and 7 is 4
mid :: Int -> Int -> Int
mid a b = div (a + b + 1) 2

Loading…
Cancel
Save