diff --git a/src/Day1/Puzzle1.hs b/src/Day1/Puzzle1.hs index 04ba920..52061ab 100644 --- a/src/Day1/Puzzle1.hs +++ b/src/Day1/Puzzle1.hs @@ -1,3 +1,5 @@ +{-# OPTIONS_GHC -Wno-incomplete-uni-patterns #-} + module Day1.Puzzle1 (day1_1) where import Data.List (transpose, sort) diff --git a/src/Day1/Puzzle2.hs b/src/Day1/Puzzle2.hs index b62b8f7..c34b9cc 100644 --- a/src/Day1/Puzzle2.hs +++ b/src/Day1/Puzzle2.hs @@ -1,3 +1,5 @@ +{-# OPTIONS_GHC -Wno-incomplete-uni-patterns #-} + module Day1.Puzzle2 (day1_2) where import Data.List (transpose, sort, group, uncons) diff --git a/src/Day13/Puzzle1.hs b/src/Day13/Puzzle1.hs index 387ec09..322e48a 100644 --- a/src/Day13/Puzzle1.hs +++ b/src/Day13/Puzzle1.hs @@ -1,3 +1,5 @@ +{-# OPTIONS_GHC -Wno-type-defaults#-} + module Day13.Puzzle1 (day13_1) where import Data.Char (isDigit) @@ -26,6 +28,7 @@ solve eqSystem = let rowEchelonList = toList . fromRight (zero 1 1) $ rref eqSys cost :: [Int] -> Int cost [x, y] = 3 * x + y +cost _ = 0 day13_1 :: IO () day13_1 = do diff --git a/src/Day13/Puzzle2.hs b/src/Day13/Puzzle2.hs index c01fcfe..6bd060d 100644 --- a/src/Day13/Puzzle2.hs +++ b/src/Day13/Puzzle2.hs @@ -1,3 +1,5 @@ +{-# OPTIONS_GHC -Wno-type-defaults#-} + module Day13.Puzzle2 (day13_2) where import Data.Char (isDigit) @@ -13,6 +15,7 @@ isAlmostInt x = let diff = x - fromInteger (round x) multRes :: (Num a) => [a] -> [a] multRes [x, y, z] = [x, y, z + 10000000000000] +multRes xs = xs 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 @@ -29,6 +32,7 @@ solve eqSystem = let rowEchelonList = toList . fromRight (zero 1 1) $ rref eqSys cost :: [Int] -> Int cost [x, y] = 3 * x + y +cost _ = 0 day13_2 :: IO () day13_2 = do diff --git a/src/Day14/Puzzle1.hs b/src/Day14/Puzzle1.hs index 4ae15ab..077d701 100644 --- a/src/Day14/Puzzle1.hs +++ b/src/Day14/Puzzle1.hs @@ -1,3 +1,5 @@ +{-# OPTIONS_GHC -Wno-incomplete-uni-patterns #-} + module Day14.Puzzle1 (day14_1) where import Data.Char (isDigit) diff --git a/src/Day2/Puzzle1.hs b/src/Day2/Puzzle1.hs index 1f61a8d..ba9dd33 100644 --- a/src/Day2/Puzzle1.hs +++ b/src/Day2/Puzzle1.hs @@ -5,8 +5,8 @@ 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 == sortBy (comparing Down) xs + where isAscending x = x == sort x + isDescending x = x == sortBy (comparing Down) x distances = map abs $ zipWith (-) xs (drop 1 xs) day2_1 :: IO () diff --git a/src/Day2/Puzzle2.hs b/src/Day2/Puzzle2.hs index 8739d05..9ecaa23 100644 --- a/src/Day2/Puzzle2.hs +++ b/src/Day2/Puzzle2.hs @@ -5,8 +5,8 @@ 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 == sortBy (comparing Down) xs + where isAscending x = x == sort x + isDescending x = x == sortBy (comparing Down) x distances = map abs $ zipWith (-) xs (drop 1 xs) removeLevel :: [Int] -> [[Int]] diff --git a/src/Day3/Puzzle1.hs b/src/Day3/Puzzle1.hs index 068d59c..c91c411 100644 --- a/src/Day3/Puzzle1.hs +++ b/src/Day3/Puzzle1.hs @@ -1,7 +1,6 @@ module Day3.Puzzle1 (day3_1) where import Data.List.Split (splitOn) -import Data.Char (isDigit) import Text.Regex.TDFA (getAllTextMatches, (=~)) sumMul :: [String] -> Int diff --git a/src/Day3/Puzzle2.hs b/src/Day3/Puzzle2.hs index 5b819ba..7c7003a 100644 --- a/src/Day3/Puzzle2.hs +++ b/src/Day3/Puzzle2.hs @@ -2,7 +2,6 @@ module Day3.Puzzle2 (day3_2) where import Data.List (isPrefixOf) import Data.List.Split (split, splitOn, startsWith) -import Data.Char (isDigit) import Text.Regex.TDFA (getAllTextMatches, (=~)) sumMul :: [String] -> Int diff --git a/src/Day4/Puzzle1.hs b/src/Day4/Puzzle1.hs index 812576b..aecc32c 100644 --- a/src/Day4/Puzzle1.hs +++ b/src/Day4/Puzzle1.hs @@ -4,8 +4,8 @@ import Data.List (transpose, isPrefixOf) diagonals :: [String] -> [String] diagonals xs = diagonals' xs ++ diagonals' ((transpose . reverse) xs) - where diagonals' xs = transpose (zipWith drop [0..] xs) - ++ transpose (zipWith drop [1..] (transpose xs)) + where diagonals' x = transpose (zipWith drop [0..] x) + ++ transpose (zipWith drop [1..] (transpose x)) countOccurrences :: String -> [String] -> Int countOccurrences word text = sum (map (countOccurrences' word) text) + sum (map (countOccurrences' word . reverse) text) @@ -14,9 +14,9 @@ countOccurrences word text = sum (map (countOccurrences' word) text) + sum (map where cols = transpose text diags = diagonals text countOccurrences' _ [] = 0 - countOccurrences' word text@(_:rest) = if word `isPrefixOf` text - then 1 + countOccurrences' word rest - else countOccurrences' word rest + countOccurrences' w txt@(_:rest) = if w `isPrefixOf` txt + then 1 + countOccurrences' word rest + else countOccurrences' w rest day4_1 :: IO () day4_1 = do diff --git a/src/Day4/Puzzle2.hs b/src/Day4/Puzzle2.hs index 1a5cd54..2f4e94b 100644 --- a/src/Day4/Puzzle2.hs +++ b/src/Day4/Puzzle2.hs @@ -4,23 +4,23 @@ import Data.List (transpose, isPrefixOf, tails) diagonals :: [String] -> [String] diagonals xs = diagonals' xs ++ diagonals' ((transpose . reverse) xs) - where diagonals' xs = transpose (zipWith drop [0..] xs) - ++ transpose (zipWith drop [1..] (transpose xs)) + where diagonals' x = transpose (zipWith drop [0..] x) + ++ transpose (zipWith drop [1..] (transpose x)) countOccurrences :: String -> [String] -> Int countOccurrences word text = sum (map (countOccurrences' word) diags) + sum (map (countOccurrences' word . reverse) diags) where diags = diagonals text countOccurrences' _ [] = 0 - countOccurrences' word text@(_:rest) = if word `isPrefixOf` text - then 1 + countOccurrences' word rest - else countOccurrences' word rest + countOccurrences' w txt@(_:rest) = if w `isPrefixOf` txt + then 1 + countOccurrences' w rest + else countOccurrences' w rest submatricesVert :: Int -> [String] -> [[String]] submatricesVert _ [] = [] -submatricesVert _ [xs] = [] -submatricesVert _ [xs, ys] = [] -submatricesVert n matrix@(xs:xxs) = submatrix matrix ++ submatricesVert n xxs - where submatrix matrix = [take n $ map (take n) matrix] +submatricesVert _ [_] = [] +submatricesVert _ [_, _] = [] +submatricesVert n matrix@(_:xxs) = submatrix matrix ++ submatricesVert n xxs + where submatrix m = [take n $ map (take n) m] day4_2 :: IO () day4_2 = do diff --git a/src/Day5/Puzzle1.hs b/src/Day5/Puzzle1.hs index 3870ba7..2ee65cb 100644 --- a/src/Day5/Puzzle1.hs +++ b/src/Day5/Puzzle1.hs @@ -1,9 +1,11 @@ +{-# OPTIONS_GHC -Wno-incomplete-patterns #-} + module Day5.Puzzle1 (day5_1) where import Data.List.Split (splitOn) isSorted :: [(String, String)] -> [String] -> Bool -isSorted rules [x] = True +isSorted _ [_] = True isSorted rules (x:xs) = let after = [ p | (p, n) <- rules, n == x ] in not (any (`elem` after) xs) && isSorted rules xs @@ -17,4 +19,4 @@ day5_1 = do updates = concatMap (map (splitOn ",")) . drop 1 $ dropWhile (/= [""]) contents sorted = filter (isSorted rules) updates putStrLn $ "Day 5, Puzzle 1 solution: " - ++ show (sum $ map (read . getMiddle) sorted) + ++ (show :: Int -> String) (sum $ map (read . getMiddle) sorted) diff --git a/src/Day5/Puzzle2.hs b/src/Day5/Puzzle2.hs index 7c13eef..faf32d6 100644 --- a/src/Day5/Puzzle2.hs +++ b/src/Day5/Puzzle2.hs @@ -1,10 +1,12 @@ +{-# OPTIONS_GHC -Wno-incomplete-patterns #-} + module Day5.Puzzle2 (day5_2) where import Data.List ((\\)) import Data.List.Split (splitOn) isSorted :: [(Int, Int)] -> [Int] -> Bool -isSorted rules [x] = True +isSorted _ [_] = True isSorted rules (x:xs) = let after = [ p | (p, n) <- rules, n == x ] in not (any (`elem` after) xs) && isSorted rules xs diff --git a/src/Day6/Puzzle1.hs b/src/Day6/Puzzle1.hs index d68476a..1cdf210 100644 --- a/src/Day6/Puzzle1.hs +++ b/src/Day6/Puzzle1.hs @@ -7,11 +7,12 @@ type Grid = [String] type Position = (Int, Int) data Direction = U | R | D | L deriving Eq -getDirection :: Char -> Direction -getDirection '^' = U -getDirection '>' = R -getDirection 'v' = D -getDirection '<' = L +getDirection :: Char -> Maybe Direction +getDirection '^' = Just U +getDirection '>' = Just R +getDirection 'v' = Just D +getDirection '<' = Just L +getDirection _ = Nothing getStartPosition:: Char -> Grid -> Position getStartPosition c grid = (x, y) @@ -61,8 +62,8 @@ visitGrid (x, y) direction grid = let newGrid = markVisited (x, y) 'X' grid day6_1 :: IO () day6_1 = do contents <- lines <$> readFile "input/day6.txt" - let (x, y) = (\w x y z -> fst . fromJust $ uncons $ filter ((>= 0) . fst) [w, x, y, z]) <$> getStartPosition 'v' <*> getStartPosition '^' + let (x, y) = (\a b c d -> fst . fromJust $ uncons $ filter ((>= 0) . fst) [a, b, c, d]) <$> getStartPosition 'v' <*> getStartPosition '^' <*> getStartPosition '<' <*> getStartPosition '>' $ contents - direction = getDirection $ (contents !! x) !! y + direction = fromJust . getDirection $ (contents !! x) !! y putStrLn $ "Day 6, Puzzle 1 solution: " ++ show (length . filter (== 'X') . concat $ visitGrid (x, y) direction contents) diff --git a/src/Day6/Puzzle2.hs b/src/Day6/Puzzle2.hs index f00af62..e216ee3 100644 --- a/src/Day6/Puzzle2.hs +++ b/src/Day6/Puzzle2.hs @@ -7,11 +7,12 @@ type Grid = [String] type Position = (Int, Int) data Direction = U | R | D | L deriving Eq -getDirection :: Char -> Direction -getDirection '^' = U -getDirection '>' = R -getDirection 'v' = D -getDirection '<' = L +getDirection :: Char -> Maybe Direction +getDirection '^' = Just U +getDirection '>' = Just R +getDirection 'v' = Just D +getDirection '<' = Just L +getDirection _ = Nothing printDirection :: Direction -> Char printDirection U = '^' @@ -82,9 +83,9 @@ setGridObstacles startPosition grid = let positions = [ (x, y) | x <- [0..(lengt day6_2 :: IO () day6_2 = do contents <- lines <$> readFile "input/day6.txt" - let (x, y) = (\w x y z -> fst . fromJust $ uncons $ filter ((>= 0) . fst) [w, x, y, z]) <$> getStartPosition 'v' <*> getStartPosition '^' + let (x, y) = (\a b c d -> fst . fromJust $ uncons $ filter ((>= 0) . fst) [a, b, c, d]) <$> getStartPosition 'v' <*> getStartPosition '^' <*> getStartPosition '<' <*> getStartPosition '>' $ contents - direction = getDirection $ (contents !! x) !! y + direction = fromJust . getDirection $ (contents !! x) !! y grid = visitGrid (x, y) direction contents gridObstacles = setGridObstacles (x, y) grid loops = filter (checkGridLoop (x, y) direction) gridObstacles diff --git a/src/Day8/Puzzle1.hs b/src/Day8/Puzzle1.hs index 633a9ad..02b3b47 100644 --- a/src/Day8/Puzzle1.hs +++ b/src/Day8/Puzzle1.hs @@ -12,7 +12,7 @@ data Antenna = Antenna { frequency :: Freq } deriving (Show, Eq) readAntenna :: Freq -> Coords -> Antenna -readAntenna frequency coordinates = Antenna {frequency=frequency, coordinates=coordinates} +readAntenna freq coords = Antenna {frequency=freq, coordinates=coords} getAntennas :: [String] -> [Antenna] getAntennas grid = concat . getZipList $ getAntennasRow <$> ZipList [0..] <*> ZipList grid diff --git a/src/Day8/Puzzle2.hs b/src/Day8/Puzzle2.hs index 84e1b16..b699544 100644 --- a/src/Day8/Puzzle2.hs +++ b/src/Day8/Puzzle2.hs @@ -13,7 +13,7 @@ data Antenna = Antenna { frequency :: Freq } deriving (Show, Eq) readAntenna :: Freq -> Coords -> Antenna -readAntenna frequency coordinates = Antenna {frequency=frequency, coordinates=coordinates} +readAntenna freq coords = Antenna {frequency=freq, coordinates=coords} getAntennas :: [String] -> [Antenna] getAntennas grid = concat . getZipList $ getAntennasRow <$> ZipList [0..] <*> ZipList grid @@ -24,7 +24,7 @@ isInside c x y = fst c >= 0 && fst c < x && snd c >= 0 && snd c < y generateCoords :: Coords -> Coords -> [Coords] generateCoords c offset = scanl shiftCoords c (repeat offset) - where shiftCoords c = bimap (fst c +) (snd c +) + where shiftCoords x = bimap (fst x +) (snd x +) getAntinodes :: Antenna -> Antenna -> Int -> Int -> [Coords] getAntinodes a b maxX maxY = let xa = fst $ coordinates a diff --git a/src/Day9/Puzzle2.hs b/src/Day9/Puzzle2.hs index 4a9cb65..bbe1f54 100644 --- a/src/Day9/Puzzle2.hs +++ b/src/Day9/Puzzle2.hs @@ -1,3 +1,5 @@ +{-# OPTIONS_GHC -Wno-incomplete-patterns #-} + module Day9.Puzzle2 (day9_2) where import Data.List (intersperse, groupBy)