Fix indentation
This commit is contained in:
parent
0038d22c5b
commit
c277c8d1fc
@ -13,8 +13,8 @@ countOccurrences word text = sum (map (countOccurrences' word) text) + sum (map
|
|||||||
diags = diagonals text
|
diags = diagonals text
|
||||||
countOccurrences' _ [] = 0
|
countOccurrences' _ [] = 0
|
||||||
countOccurrences' word text@(_:rest) = if word `isPrefixOf` text
|
countOccurrences' word text@(_:rest) = if word `isPrefixOf` text
|
||||||
then 1 + countOccurrences' word rest
|
then 1 + countOccurrences' word rest
|
||||||
else countOccurrences' word rest
|
else countOccurrences' word rest
|
||||||
|
|
||||||
main = do
|
main = do
|
||||||
contents <- lines <$> readFile "day4.txt"
|
contents <- lines <$> readFile "day4.txt"
|
||||||
|
@ -2,16 +2,16 @@ import Data.List (transpose, isPrefixOf, tails)
|
|||||||
|
|
||||||
diagonals :: [String] -> [String]
|
diagonals :: [String] -> [String]
|
||||||
diagonals xs = diagonals' xs ++ diagonals' ((transpose . reverse) xs)
|
diagonals xs = diagonals' xs ++ diagonals' ((transpose . reverse) xs)
|
||||||
where diagonals' xs = transpose (zipWith drop [0..] xs)
|
where diagonals' xs = transpose (zipWith drop [0..] xs)
|
||||||
++ transpose (zipWith drop [1..] (transpose xs))
|
++ transpose (zipWith drop [1..] (transpose xs))
|
||||||
|
|
||||||
countOccurrences :: String -> [String] -> Int
|
countOccurrences :: String -> [String] -> Int
|
||||||
countOccurrences word text = sum (map (countOccurrences' word) diags) + sum (map (countOccurrences' word . reverse) diags)
|
countOccurrences word text = sum (map (countOccurrences' word) diags) + sum (map (countOccurrences' word . reverse) diags)
|
||||||
where diags = diagonals text
|
where diags = diagonals text
|
||||||
countOccurrences' _ [] = 0
|
countOccurrences' _ [] = 0
|
||||||
countOccurrences' word text@(_:rest) = if word `isPrefixOf` text
|
countOccurrences' word text@(_:rest) = if word `isPrefixOf` text
|
||||||
then 1 + countOccurrences' word rest
|
then 1 + countOccurrences' word rest
|
||||||
else countOccurrences' word rest
|
else countOccurrences' word rest
|
||||||
|
|
||||||
submatricesVert :: Int -> [String] -> [[String]]
|
submatricesVert :: Int -> [String] -> [[String]]
|
||||||
submatricesVert _ [] = []
|
submatricesVert _ [] = []
|
||||||
|
@ -12,9 +12,9 @@ getMiddle xs = xs !! (length xs `div` 2)
|
|||||||
sortOnRules :: [(Int, Int)] -> [Int] -> [Int]
|
sortOnRules :: [(Int, Int)] -> [Int] -> [Int]
|
||||||
sortOnRules _ [] = []
|
sortOnRules _ [] = []
|
||||||
sortOnRules rules (x:xs) = sortOnRules rules beforeArray ++ [x] ++ sortOnRules rules afterArray
|
sortOnRules rules (x:xs) = sortOnRules rules beforeArray ++ [x] ++ sortOnRules rules afterArray
|
||||||
where afterArray = xs \\ before
|
where afterArray = xs \\ before
|
||||||
beforeArray = xs \\ afterArray
|
beforeArray = xs \\ afterArray
|
||||||
before = [ p | (p, n) <- rules, n == x ]
|
before = [ p | (p, n) <- rules, n == x ]
|
||||||
|
|
||||||
main = do
|
main = do
|
||||||
contents <- map (splitOn "|") . lines <$> readFile "day5.txt"
|
contents <- map (splitOn "|") . lines <$> readFile "day5.txt"
|
||||||
|
@ -13,8 +13,8 @@ getDirection '<' = L
|
|||||||
|
|
||||||
getStartPosition:: Char -> Grid -> Position
|
getStartPosition:: Char -> Grid -> Position
|
||||||
getStartPosition c grid = (x, y)
|
getStartPosition c grid = (x, y)
|
||||||
where x = fromMaybe (-1) . elemIndex True . map (isJust . elemIndex c) $ grid
|
where x = fromMaybe (-1) . elemIndex True . map (isJust . elemIndex c) $ grid
|
||||||
y = if x == -1 then -1 else fromMaybe (-1) . elemIndex c $ grid !! x
|
y = if x == -1 then -1 else fromMaybe (-1) . elemIndex c $ grid !! x
|
||||||
|
|
||||||
getGridVal :: Position -> Grid -> Char
|
getGridVal :: Position -> Grid -> Char
|
||||||
getGridVal (x, y) grid = (grid !! x) !! y
|
getGridVal (x, y) grid = (grid !! x) !! y
|
||||||
@ -26,23 +26,23 @@ getNextPosition :: Position -> Direction -> Grid -> (Position, Direction)
|
|||||||
getNextPosition (x, y) U grid = let newPos = (x - 1, y)
|
getNextPosition (x, y) U grid = let newPos = (x - 1, y)
|
||||||
gridVal = getGridVal newPos grid
|
gridVal = getGridVal newPos grid
|
||||||
in if newPos `isInside` grid && gridVal == '#'
|
in if newPos `isInside` grid && gridVal == '#'
|
||||||
then getNextPosition (x, y) R grid
|
then getNextPosition (x, y) R grid
|
||||||
else (newPos, U)
|
else (newPos, U)
|
||||||
getNextPosition (x, y) R grid = let newPos = (x, y + 1)
|
getNextPosition (x, y) R grid = let newPos = (x, y + 1)
|
||||||
gridVal = getGridVal newPos grid
|
gridVal = getGridVal newPos grid
|
||||||
in if newPos `isInside` grid && gridVal == '#'
|
in if newPos `isInside` grid && gridVal == '#'
|
||||||
then getNextPosition (x, y) D grid
|
then getNextPosition (x, y) D grid
|
||||||
else (newPos, R)
|
else (newPos, R)
|
||||||
getNextPosition (x, y) D grid = let newPos = (x + 1, y)
|
getNextPosition (x, y) D grid = let newPos = (x + 1, y)
|
||||||
gridVal = getGridVal newPos grid
|
gridVal = getGridVal newPos grid
|
||||||
in if newPos `isInside` grid && gridVal == '#'
|
in if newPos `isInside` grid && gridVal == '#'
|
||||||
then getNextPosition (x, y) L grid
|
then getNextPosition (x, y) L grid
|
||||||
else (newPos, D)
|
else (newPos, D)
|
||||||
getNextPosition (x, y) L grid = let newPos = (x, y - 1)
|
getNextPosition (x, y) L grid = let newPos = (x, y - 1)
|
||||||
gridVal = getGridVal newPos grid
|
gridVal = getGridVal newPos grid
|
||||||
in if newPos `isInside` grid && gridVal == '#'
|
in if newPos `isInside` grid && gridVal == '#'
|
||||||
then getNextPosition (x, y) U grid
|
then getNextPosition (x, y) U grid
|
||||||
else (newPos, L)
|
else (newPos, L)
|
||||||
|
|
||||||
markVisited :: Position -> Char -> Grid -> Grid
|
markVisited :: Position -> Char -> Grid -> Grid
|
||||||
markVisited (x, y) c grid = let row = grid !! x
|
markVisited (x, y) c grid = let row = grid !! x
|
||||||
@ -53,8 +53,8 @@ visitGrid :: Position -> Direction -> Grid -> Grid
|
|||||||
visitGrid (x, y) direction grid = let newGrid = markVisited (x, y) 'X' grid
|
visitGrid (x, y) direction grid = let newGrid = markVisited (x, y) 'X' grid
|
||||||
(nextPosition, newDirection) = getNextPosition (x, y) direction grid
|
(nextPosition, newDirection) = getNextPosition (x, y) direction grid
|
||||||
in if nextPosition `isInside` newGrid
|
in if nextPosition `isInside` newGrid
|
||||||
then visitGrid nextPosition newDirection newGrid
|
then visitGrid nextPosition newDirection newGrid
|
||||||
else newGrid
|
else newGrid
|
||||||
|
|
||||||
main = do
|
main = do
|
||||||
contents <- lines <$> readFile "day6.txt"
|
contents <- lines <$> readFile "day6.txt"
|
||||||
|
@ -19,8 +19,8 @@ printDirection L = '<'
|
|||||||
|
|
||||||
getStartPosition :: Char -> Grid -> Position
|
getStartPosition :: Char -> Grid -> Position
|
||||||
getStartPosition c grid = (x, y)
|
getStartPosition c grid = (x, y)
|
||||||
where x = fromMaybe (-1) . elemIndex True . map (isJust . elemIndex c) $ grid
|
where x = fromMaybe (-1) . elemIndex True . map (isJust . elemIndex c) $ grid
|
||||||
y = if x == -1 then -1 else fromMaybe (-1) . elemIndex c $ grid !! x
|
y = if x == -1 then -1 else fromMaybe (-1) . elemIndex c $ grid !! x
|
||||||
|
|
||||||
getGridVal :: Position -> Grid -> Char
|
getGridVal :: Position -> Grid -> Char
|
||||||
getGridVal (x, y) grid = (grid !! x) !! y
|
getGridVal (x, y) grid = (grid !! x) !! y
|
||||||
@ -32,38 +32,38 @@ getNextPosition :: Position -> Direction -> Grid -> (Position, Direction)
|
|||||||
getNextPosition (x, y) U grid = let newPos = (x - 1, y)
|
getNextPosition (x, y) U grid = let newPos = (x - 1, y)
|
||||||
gridVal = getGridVal newPos grid
|
gridVal = getGridVal newPos grid
|
||||||
in if newPos `isInside` grid && gridVal == '#'
|
in if newPos `isInside` grid && gridVal == '#'
|
||||||
then getNextPosition (x, y) R grid
|
then getNextPosition (x, y) R grid
|
||||||
else (newPos, U)
|
else (newPos, U)
|
||||||
getNextPosition (x, y) R grid = let newPos = (x, y + 1)
|
getNextPosition (x, y) R grid = let newPos = (x, y + 1)
|
||||||
gridVal = getGridVal newPos grid
|
gridVal = getGridVal newPos grid
|
||||||
in if newPos `isInside` grid && gridVal == '#'
|
in if newPos `isInside` grid && gridVal == '#'
|
||||||
then getNextPosition (x, y) D grid
|
then getNextPosition (x, y) D grid
|
||||||
else (newPos, R)
|
else (newPos, R)
|
||||||
getNextPosition (x, y) D grid = let newPos = (x + 1, y)
|
getNextPosition (x, y) D grid = let newPos = (x + 1, y)
|
||||||
gridVal = getGridVal newPos grid
|
gridVal = getGridVal newPos grid
|
||||||
in if newPos `isInside` grid && gridVal == '#'
|
in if newPos `isInside` grid && gridVal == '#'
|
||||||
then getNextPosition (x, y) L grid
|
then getNextPosition (x, y) L grid
|
||||||
else (newPos, D)
|
else (newPos, D)
|
||||||
getNextPosition (x, y) L grid = let newPos = (x, y - 1)
|
getNextPosition (x, y) L grid = let newPos = (x, y - 1)
|
||||||
gridVal = getGridVal newPos grid
|
gridVal = getGridVal newPos grid
|
||||||
in if newPos `isInside` grid && gridVal == '#'
|
in if newPos `isInside` grid && gridVal == '#'
|
||||||
then getNextPosition (x, y) U grid
|
then getNextPosition (x, y) U grid
|
||||||
else (newPos, L)
|
else (newPos, L)
|
||||||
|
|
||||||
markVisited :: Position -> Char -> Grid -> Grid
|
markVisited :: Position -> Char -> Grid -> Grid
|
||||||
markVisited (x, y) c grid = let gridVal = getGridVal (x, y) grid
|
markVisited (x, y) c grid = let gridVal = getGridVal (x, y) grid
|
||||||
in if gridVal == '#' || gridVal == '^' || gridVal == '>' || gridVal == 'v' || gridVal == '<'
|
in if gridVal == '#' || gridVal == '^' || gridVal == '>' || gridVal == 'v' || gridVal == '<'
|
||||||
then grid
|
then grid
|
||||||
else let row = grid !! x
|
else let row = grid !! x
|
||||||
newRow = take y row ++ [c] ++ drop (y + 1) row
|
newRow = take y row ++ [c] ++ drop (y + 1) row
|
||||||
in take x grid ++ [newRow] ++ drop (x + 1) grid
|
in take x grid ++ [newRow] ++ drop (x + 1) grid
|
||||||
|
|
||||||
visitGrid :: Position -> Direction -> Grid -> Grid
|
visitGrid :: Position -> Direction -> Grid -> Grid
|
||||||
visitGrid (x, y) direction grid = let newGrid = markVisited (x, y) 'X' grid
|
visitGrid (x, y) direction grid = let newGrid = markVisited (x, y) 'X' grid
|
||||||
(nextPosition, newDirection) = getNextPosition (x, y) direction grid
|
(nextPosition, newDirection) = getNextPosition (x, y) direction grid
|
||||||
in if nextPosition `isInside` newGrid
|
in if nextPosition `isInside` newGrid
|
||||||
then visitGrid nextPosition newDirection newGrid
|
then visitGrid nextPosition newDirection newGrid
|
||||||
else newGrid
|
else newGrid
|
||||||
|
|
||||||
checkGridLoop :: Position -> Direction -> Grid -> Bool
|
checkGridLoop :: Position -> Direction -> Grid -> Bool
|
||||||
checkGridLoop startPosition direction grid = let (nextPosition, newDirection) = getNextPosition startPosition direction grid
|
checkGridLoop startPosition direction grid = let (nextPosition, newDirection) = getNextPosition startPosition direction grid
|
||||||
|
Loading…
x
Reference in New Issue
Block a user