Created
December 8, 2022 14:54
-
-
Save skatenerd/4d8a616a8864f397a91a50be47da2eeb to your computer and use it in GitHub Desktop.
advent day eight
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module DayEight | |
( partOne | |
) where | |
import qualified Data.Set as S | |
import qualified Data.List as L | |
import qualified Data.Text as T | |
import qualified Data.Text.IO as TI | |
import Control.Applicative | |
cartesianProduct a b = (,) <$> a <*> b | |
parseLine :: T.Text -> [Integer] | |
parseLine line = (map readChar $ T.unpack line) | |
where readChar c = read [c] | |
itemsLeft list index = take index list | |
itemsRight list index = drop (index + 1) list | |
takeUntil p items = take count items | |
where count = 1 + (length $ takeWhile (inverse p) items) | |
inverse f = \x -> not (f x) | |
visibleRight list index = takeUntil (>= atIndex) $ itemsRight list index | |
where atIndex = list !! index | |
visibleLeft list index = takeUntil (>= atIndex) $ reverse $ itemsLeft list index | |
where atIndex = list !! index | |
isVisibleHorizontal list index = leftVisible || rightVisible | |
where leftVisible = all (< atIndex) $ itemsLeft list index | |
rightVisible = all (< atIndex) $ itemsRight list index | |
atIndex = list !! index | |
treehouseScore row column grid = (length left) * (length right) * (length up) * (length down) | |
where left = visibleLeft (grid !! row) column | |
right = visibleRight (grid !! row) column | |
up = visibleRight (transposed !! column) row | |
down = visibleLeft (transposed !! column) row | |
transposed = L.transpose grid | |
isVisible row column grid = isVisibleHorizontal (grid !! row) column || isVisibleHorizontal (transposed !! column) row | |
where transposed = L.transpose grid | |
partOne :: String -> IO Int | |
partOne path = do | |
input <- TI.readFile path | |
let inputLines = T.lines input | |
parsed = map parseLine inputLines | |
rowsCount = length parsed | |
columnsCount = length $ head parsed | |
indices = cartesianProduct [0..(rowsCount - 1)] [0..(columnsCount - 1)] | |
tupleVisible (row, column) = isVisible row column parsed | |
count = length $ filter tupleVisible indices | |
return count | |
partTwo :: String -> IO Int | |
partTwo path = do | |
input <- TI.readFile path | |
let inputLines = T.lines input | |
parsed = map parseLine inputLines | |
rowsCount = length parsed | |
columnsCount = length $ head parsed | |
indices = cartesianProduct [0..(rowsCount - 1)] [0..(columnsCount - 1)] | |
tupleScore (row, column) = treehouseScore row column parsed | |
answer = L.maximum $ map tupleScore indices | |
print $ answer | |
return answer |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment