Fix ghc warnings

This commit is contained in:
daniele 2024-12-14 11:49:14 +01:00
parent 7cc43b607d
commit b0303fa23d
Signed by: fuxino
GPG Key ID: 981A2B2A3BBF5514
14 changed files with 51 additions and 36 deletions

2
.gitignore vendored
View File

@ -1 +1,3 @@
*.txt
*.hi
*.o

View File

@ -1,7 +1,8 @@
import Data.List (transpose, sort, group)
import Data.List (transpose, sort, group, uncons)
import Data.Maybe (fromJust)
similarityScore :: [Int] -> [Int] -> Int
similarityScore xs ys = let elemsY = [ (head y, length y) | y <- (group . sort) ys ]
similarityScore xs ys = let elemsY = [ (fst . fromJust $ uncons y, length y) | y <- (group . sort) ys ]
in sum [ x * snd y | x <- xs, y <- elemsY, x == fst y ]
main = do

View File

@ -1,5 +1,7 @@
import Data.Char (digitToInt)
import Data.List (uncons)
import Data.List.Split (chunksOf)
import Data.Maybe (fromJust)
import Data.Graph (graphFromEdges, path, vertices)
type Coords = (Int, Int)
@ -10,17 +12,17 @@ getValue grid (i, j) = grid !! i !! j
getEdges :: [[V]] -> Coords -> [Int]
getEdges grid (i, j) = let value = fst $ grid !! i !! j
adjI = filter (\x -> fst x >= 0 && fst x < length grid && snd x >= 0 && snd x < length (head grid)) [ (i, j + 1), (i + 1, j), (i, j - 1), (i - 1, j) ]
in [ snd x | x <- map (getValue grid) adjI, digitToInt (head value) == digitToInt (head (fst x)) - 1 ]
adjI = filter (\x -> fst x >= 0 && fst x < length grid && snd x >= 0 && snd x < length (fst . fromJust $ uncons grid)) [ (i, j + 1), (i + 1, j), (i, j - 1), (i - 1, j) ]
in [ snd x | x <- map (getValue grid) adjI, digitToInt (fst . fromJust $ uncons value) == digitToInt (fst . fromJust $ uncons (fst x)) - 1 ]
listVertices :: [String] -> [[V]]
listVertices grid = let l = length $ head grid
listVertices grid = let l = length $ fst . fromJust $ uncons grid
in chunksOf l $ zip (map (:[]) (concat grid)) [0..]
main = do
contents <- lines <$> readFile "day10.txt"
let grid = listVertices contents
edgeCoords = [ (x, y) | x <- [0..length grid -1], y <- [0..length (head grid) - 1] ]
edgeCoords = [ (x, y) | x <- [0..length grid -1], y <- [0..length (fst . fromJust $ uncons grid) - 1] ]
edgeList = [ (x, y, z) | ((x, y), z) <- zip (concat grid) (map (getEdges grid) edgeCoords) ]
(graph, nodeFromVertex, _) = graphFromEdges edgeList
startList = [ x | (_, x, _) <- filter (\(x, _, _) -> x == "0") $ map nodeFromVertex $ vertices graph ]

View File

@ -1,4 +1,6 @@
import Data.List (uncons)
import Data.List.Split (chunksOf)
import Data.Maybe (fromJust)
import Data.Graph (Tree, Vertex, graphFromEdges, scc)
import Data.Foldable (toList)
@ -10,11 +12,11 @@ getValue grid (i, j) = grid !! i !! j
getEdges :: [[V]] -> Coords -> [Int]
getEdges grid (i, j) = let value = fst $ grid !! i !! j
adjI = filter (\x -> fst x >= 0 && fst x < length grid && snd x >= 0 && snd x < length (head grid)) [ (i, j + 1), (i + 1, j), (i, j - 1), (i - 1, j) ]
in [ snd x | x <- map (getValue grid) adjI, head value == head (fst x) ]
adjI = filter (\x -> fst x >= 0 && fst x < length grid && snd x >= 0 && snd x < length (fst . fromJust $ uncons grid)) [ (i, j + 1), (i + 1, j), (i, j - 1), (i - 1, j) ]
in [ snd x | x <- map (getValue grid) adjI, (fst . fromJust $ uncons value) == (fst . fromJust $ uncons (fst x)) ]
listVertices :: [String] -> [[V]]
listVertices grid = let l = length $ head grid
listVertices grid = let l = length $ fst . fromJust $ uncons grid
in chunksOf l $ zip (map (:[]) (concat grid)) [0..]
calculatePerimeter :: (Vertex -> (String, Vertex, [Vertex])) -> Tree Vertex -> Int
@ -25,7 +27,7 @@ calculatePerimeter nodeFromVertex p = let edges = concat [ x | (_, _, x) <- toLi
main = do
contents <- lines <$> readFile "day12.txt"
let grid = listVertices contents
edgeCoords = [ (x, y) | x <- [0..length grid -1], y <- [0..length (head grid) - 1] ]
edgeCoords = [ (x, y) | x <- [0..length grid -1], y <- [0..length (fst . fromJust $ uncons grid) - 1] ]
edgeList = [ (x, y, z) | ((x, y), z) <- zip (concat grid) (map (getEdges grid) edgeCoords) ]
(graph, nodeFromVertex, _) = graphFromEdges edgeList
plots = scc graph

View File

@ -1,16 +1,17 @@
import Data.Char (isDigit)
import Data.List (uncons)
import Data.List.Split (splitOn, chunksOf)
import Data.Matrix (Matrix, fromLists, toList, rref, zero)
import Data.Either (fromRight)
import Data.Maybe (mapMaybe)
import Data.Maybe (mapMaybe, fromJust)
isAlmostInt :: (RealFrac a) => a -> Bool
isAlmostInt x = let diff = x - fromInteger (round x)
in abs diff < 0.000001
in abs diff < 0.001
getMatrix :: (Read a) => String -> Matrix a
getMatrix s = let nValues = map (map read . splitOn ",") . splitOn ":" . drop 1 $ filter (\x -> isDigit x || x == ',' || x == ':') s
eq1 = map head nValues
eq1 = map (fst . fromJust . uncons) nValues
eq2 = map last nValues
in fromLists [eq1, eq2]

View File

@ -1,8 +1,9 @@
import Data.Char (isDigit)
import Data.List (uncons)
import Data.List.Split (splitOn, chunksOf)
import Data.Matrix (Matrix, fromLists, toList, rref, zero)
import Data.Either (fromRight)
import Data.Maybe (mapMaybe)
import Data.Maybe (mapMaybe, fromJust)
isAlmostInt :: (RealFrac a) => a -> Bool
isAlmostInt x = let diff = x - fromInteger (round x)
@ -13,7 +14,7 @@ multRes [x, y, z] = [x, y, z + 10000000000000]
getMatrix :: (Num a, Read a) => String -> Matrix a
getMatrix s = let nValues = map (map read . splitOn ",") . splitOn ":" . drop 1 $ filter (\x -> isDigit x || x == ',' || x == ':') s
eq1 = multRes $ map head nValues
eq1 = multRes $ map (fst . fromJust . uncons) nValues
eq2 = multRes $ map last nValues
in fromLists [eq1, eq2]

View File

@ -1,10 +1,11 @@
import Data.List (sort)
import Data.List (sort, sortBy)
import Data.Ord
isSafe :: [Int] -> Bool
isSafe xs = (isAscending xs || isDescending xs) && maximum distances <= 3 && minimum distances >= 1
where isAscending xs = xs == sort xs
isDescending xs = xs == reverse (sort xs)
distances = map abs $ zipWith (-) xs (tail xs)
isDescending xs = xs == sortBy (comparing Down) xs
distances = map abs $ zipWith (-) xs (drop 1 xs)
main = do
contents <- lines <$> readFile "day2.txt"

View File

@ -1,15 +1,16 @@
import Data.List (sort, inits, tails)
import Data.List (sort, sortBy, inits, tails)
import Data.Ord
isSafe :: [Int] -> Bool
isSafe xs = (isAscending xs || isDescending xs) && maximum distances <= 3 && minimum distances >= 1
where isAscending xs = xs == sort xs
isDescending xs = xs == reverse (sort xs)
distances = map abs $ zipWith (-) xs (tail xs)
isDescending xs = xs == sortBy (comparing Down) xs
distances = map abs $ zipWith (-) xs (drop 1 xs)
removeLevel :: [Int] -> [[Int]]
removeLevel xs = zipWith (++) ys zs
where ys = map init $ tail (inits xs)
zs = map tail $ init (tails xs)
where ys = map init $ drop 1 (inits xs)
zs = map (drop 1) $ init (tails xs)
main = do
contents <- lines <$> readFile "day2.txt"

View File

@ -11,6 +11,6 @@ getMiddle xs = xs !! (length xs `div` 2)
main = do
contents <- map (splitOn "|") . lines <$> readFile "day5.txt"
let rules = [ (x, y) | [x, y] <- takeWhile (/= [""]) contents ]
updates = concatMap (map (splitOn ",")) . tail $ dropWhile (/= [""]) contents
updates = concatMap (map (splitOn ",")) . drop 1 $ dropWhile (/= [""]) contents
sorted = filter (isSorted rules) updates
print . sum $ map (read . getMiddle) sorted

View File

@ -19,6 +19,6 @@ sortOnRules rules (x:xs) = sortOnRules rules beforeArray ++ [x] ++ sortOnRules r
main = do
contents <- map (splitOn "|") . lines <$> readFile "day5.txt"
let rules = [ (read x, read y) | [x, y] <- takeWhile (/= [""]) contents ]
unsorted = filter (not . isSorted rules) . map (map read) $ concatMap (map (splitOn ",")) . tail $ dropWhile (/= [""]) contents
unsorted = filter (not . isSorted rules) . map (map read) $ concatMap (map (splitOn ",")) . drop 1 $ dropWhile (/= [""]) contents
fixUnsorted = map (sortOnRules rules) unsorted
print . sum $ map getMiddle fixUnsorted

View File

@ -1,5 +1,5 @@
import Data.List (elemIndex)
import Data.Maybe (isJust, fromMaybe)
import Data.List (elemIndex, uncons)
import Data.Maybe (isJust, fromMaybe, fromJust)
type Grid = [String]
type Position = (Int, Int)
@ -20,7 +20,7 @@ getGridVal :: Position -> Grid -> Char
getGridVal (x, y) grid = (grid !! x) !! y
isInside :: Position -> Grid -> Bool
isInside (x, y) grid = x >= 0 && y >= 0 && x < length grid && y < length (head grid)
isInside (x, y) grid = x >= 0 && y >= 0 && x < length grid && y < length (fst . fromJust $ uncons grid)
getNextPosition :: Position -> Direction -> Grid -> (Position, Direction)
getNextPosition (x, y) U grid = let newPos = (x - 1, y)
@ -58,7 +58,7 @@ visitGrid (x, y) direction grid = let newGrid = markVisited (x, y) 'X' grid
main = do
contents <- lines <$> readFile "day6.txt"
let (x, y) = (\w x y z -> head $ filter ((>= 0) . fst) [w, x, y, z]) <$> getStartPosition 'v' <*> getStartPosition '^'
let (x, y) = (\w x y z -> fst . fromJust $ uncons $ filter ((>= 0) . fst) [w, x, y, z]) <$> getStartPosition 'v' <*> getStartPosition '^'
<*> getStartPosition '<' <*> getStartPosition '>' $ contents
direction = getDirection $ (contents !! x) !! y
print . length . filter (== 'X') . concat $ visitGrid (x, y) direction contents

View File

@ -1,5 +1,5 @@
import Data.List (elemIndex)
import Data.Maybe (isJust, fromMaybe)
import Data.List (elemIndex, uncons)
import Data.Maybe (isJust, fromMaybe, fromJust)
type Grid = [String]
type Position = (Int, Int)
@ -26,7 +26,7 @@ getGridVal :: Position -> Grid -> Char
getGridVal (x, y) grid = (grid !! x) !! y
isInside :: Position -> Grid -> Bool
isInside (x, y) grid = x >= 0 && y >= 0 && x < length grid && y < length (head grid)
isInside (x, y) grid = x >= 0 && y >= 0 && x < length grid && y < length (fst . fromJust $ uncons grid)
getNextPosition :: Position -> Direction -> Grid -> (Position, Direction)
getNextPosition (x, y) U grid = let newPos = (x - 1, y)
@ -74,13 +74,13 @@ checkGridLoop startPosition direction grid = let (nextPosition, newDirection) =
|| checkGridLoop nextPosition newDirection newGrid)
setGridObstacles :: Position -> Grid -> [Grid]
setGridObstacles startPosition grid = let positions = [ (x, y) | x <- [0..(length grid - 1)], y <- [0..(length (head grid) - 1)], (x, y) /= startPosition, getGridVal (x, y) grid == 'X' ]
setGridObstacles startPosition grid = let positions = [ (x, y) | x <- [0..(length grid - 1)], y <- [0..(length (fst . fromJust $ uncons grid) - 1)], (x, y) /= startPosition, getGridVal (x, y) grid == 'X' ]
in zipWith (`markVisited` '#') positions (replicate (length positions) grid)
main = do
contents <- lines <$> readFile "day6.txt"
let (x, y) = (\w x y z -> head $ filter ((>= 0) . fst) [w, x, y, z]) <$> getStartPosition 'v' <*> getStartPosition '^'
let (x, y) = (\w x y z -> fst . fromJust $ uncons $ filter ((>= 0) . fst) [w, x, y, z]) <$> getStartPosition 'v' <*> getStartPosition '^'
<*> getStartPosition '<' <*> getStartPosition '>' $ contents
direction = getDirection $ (contents !! x) !! y
grid = visitGrid (x, y) direction contents

View File

@ -1,3 +1,5 @@
import Data.List (uncons)
import Data.Maybe (fromJust)
import Control.Applicative
import qualified Data.Set as Set
@ -30,6 +32,6 @@ main = do
contents <- lines <$> readFile "day8.txt"
let antennas = getAntennas contents
x = length contents
y = length $ head contents
y = length $ fst . fromJust $ uncons contents
antinodes = Set.fromList $ concat [ getAntinodes a b x y | a <- antennas, b <- antennas, a /= b, frequency a == frequency b ]
print $ length antinodes

View File

@ -1,3 +1,5 @@
import Data.List (uncons)
import Data.Maybe (fromJust)
import Control.Applicative
import Data.Set (fromList)
import Data.Bifunctor (bimap)
@ -39,6 +41,6 @@ main = do
contents <- lines <$> readFile "day8.txt"
let antennas = getAntennas contents
x = length contents
y = length $ head contents
y = length $ fst . fromJust $ uncons contents
antinodes = fromList $ concat [ getAntinodes a b x y | a <- antennas, b <- antennas, a /= b, frequency a == frequency b ]
print $ length antinodes