diff --git a/8-a.hs b/8-a.hs new file mode 100644 index 0000000..594cb3f --- /dev/null +++ b/8-a.hs @@ -0,0 +1,32 @@ +{-# LANGUAGE TupleSections #-} + +import Data.List + +-- this is literally only a function because read is stupid +parse :: String -> [[Int]] +parse = map (map (read . singleton)) . lines + +getGridSize :: [[a]] -> (Int, Int) +getGridSize grid = (length $ head grid, length grid) + +isVisible :: [[Int]] -> (Int, Int) -> Bool +isVisible grid (x, y) = any startStep [(-1, 0), (1, 0), (0, -1), (0, 1)] + where startGridValue = grid !! y !! x + (gridWidth, gridHeight) = getGridSize grid + startStep (facingX, facingY) = isVisible' (x + facingX, y + facingY) (facingX, facingY) + isVisible' (gridX, gridY) (facingX, facingY) + | gridX < 0 || gridY < 0 || gridX > (gridWidth - 1) || gridY > (gridHeight - 1) = True + | otherwise = (grid !! gridY !! gridX) < startGridValue + && isVisible' (gridX + facingX, gridY + facingY) (facingX, facingY) + +listPermutations :: [a] -> [a] -> [(a, a)] +listPermutations a = concatMap (\b' -> map (, b') a) + +getAllCoordinates grid = listPermutations [0..gridWidth-1] [0..gridHeight-1] + where (gridWidth, gridHeight) = getGridSize grid + +main = interact $ + show + . length . filter id + . (\grid -> map (isVisible grid) $ getAllCoordinates grid) + . parse \ No newline at end of file diff --git a/8-b.hs b/8-b.hs new file mode 100644 index 0000000..5fe69dd --- /dev/null +++ b/8-b.hs @@ -0,0 +1,30 @@ +{-# LANGUAGE TupleSections #-} +import Data.List + +parse :: String -> [[Int]] +parse = map (map (read . singleton)) . lines + +getGridSize :: [[a]] -> (Int, Int) +getGridSize grid = (length $ head grid, length grid) + +getScenicScore :: [[Int]] -> (Int, Int) -> Int +getScenicScore grid (x, y) = product $ map startStep [(-1, 0), (1, 0), (0, -1), (0, 1)] + where startGridValue = grid !! y !! x + (gridWidth, gridHeight) = getGridSize grid + startStep (facingX, facingY) = scenicScoreStep (x + facingX, y + facingY) (facingX, facingY) + scenicScoreStep (gridX, gridY) (facingX, facingY) + | gridX < 0 || gridY < 0 || gridX > (gridWidth - 1) || gridY > (gridHeight - 1) = 0 + | (grid !! gridY !! gridX) < startGridValue = 1 + scenicScoreStep (gridX + facingX, gridY + facingY) (facingX, facingY) + | otherwise = 1 + +listPermutations :: [a] -> [a] -> [(a, a)] +listPermutations a = concatMap (\b' -> map (, b') a) + +getAllCoordinates grid = listPermutations [0..gridWidth-1] [0..gridHeight-1] + where (gridWidth, gridHeight) = getGridSize grid + +main = interact $ + show + . maximum + . (\grid -> map (getScenicScore grid) $ getAllCoordinates grid) + . parse \ No newline at end of file