day 8
This commit is contained in:
parent
553cdd7d59
commit
19cd3a3e3b
|
@ -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
|
|
@ -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
|
Loading…
Reference in New Issue