From e4c38318e9c8d1c52a28387436da23358526c753 Mon Sep 17 00:00:00 2001 From: Daniele Fucini Date: Thu, 5 Dec 2024 15:33:14 +0100 Subject: [PATCH] Day 5 --- Day2/puzzle1.hs | 4 ++-- Day4/puzzle1.hs | 6 +++--- Day4/puzzle2.hs | 4 ++-- Day5/puzzle1.hs | 16 ++++++++++++++++ Day5/puzzle2.hs | 24 ++++++++++++++++++++++++ 5 files changed, 47 insertions(+), 7 deletions(-) create mode 100644 Day5/puzzle1.hs create mode 100644 Day5/puzzle2.hs diff --git a/Day2/puzzle1.hs b/Day2/puzzle1.hs index 32925ed..edc83aa 100644 --- a/Day2/puzzle1.hs +++ b/Day2/puzzle1.hs @@ -2,9 +2,9 @@ import Data.List (sort) isSafe :: [Int] -> Bool isSafe xs = (isAscending xs || isDescending xs) && maximum distances <= 3 && minimum distances >= 1 - where isAscending xs = xs == sort xs + where isAscending xs = xs == sort xs isDescending xs = xs == reverse (sort xs) - distances = map abs $ zipWith (-) xs (tail xs) + distances = map abs $ zipWith (-) xs (tail xs) main = do contents <- lines <$> readFile "day2.txt" diff --git a/Day4/puzzle1.hs b/Day4/puzzle1.hs index 3f6692e..4db2f38 100644 --- a/Day4/puzzle1.hs +++ b/Day4/puzzle1.hs @@ -9,12 +9,12 @@ countSubstrings :: String -> [String] -> Int countSubstrings word text = sum (map (countSubstrings' word) text) + sum (map (countSubstrings' word . reverse) text) + sum (map (countSubstrings' word) cols) + sum (map (countSubstrings' word . reverse) cols) + sum (map (countSubstrings' word) diags) + sum (map (countSubstrings' word . reverse) diags) - where cols = transpose text + where cols = transpose text diags = diagonals text countSubstrings' _ [] = 0 countSubstrings' word text@(_:rest) = if word `isPrefixOf` text - then 1 + countSubstrings' word rest - else countSubstrings' word rest + then 1 + countSubstrings' word rest + else countSubstrings' word rest main = do contents <- lines <$> readFile "day4.txt" diff --git a/Day4/puzzle2.hs b/Day4/puzzle2.hs index 59a968f..0d02298 100644 --- a/Day4/puzzle2.hs +++ b/Day4/puzzle2.hs @@ -10,8 +10,8 @@ countSubstrings word text = sum (map (countSubstrings' word) diags) + sum (map ( where diags = diagonals text countSubstrings' _ [] = 0 countSubstrings' word text@(_:rest) = if word `isPrefixOf` text - then 1 + countSubstrings' word rest - else countSubstrings' word rest + then 1 + countSubstrings' word rest + else countSubstrings' word rest submatricesVert :: Int -> [String] -> [[String]] submatricesVert _ [] = [] diff --git a/Day5/puzzle1.hs b/Day5/puzzle1.hs new file mode 100644 index 0000000..331709a --- /dev/null +++ b/Day5/puzzle1.hs @@ -0,0 +1,16 @@ +import Data.List.Split (splitOn) + +isSorted :: [(String, String)] -> [String] -> Bool +isSorted rules [x] = True +isSorted rules (x:xs) = let after = [ p | (p, n) <- rules, n == x ] + in not (any (`elem` after) xs) && isSorted rules xs + +getMiddle :: [String] -> String +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 + sorted = filter (isSorted rules) updates + print . sum $ map (read . getMiddle) sorted diff --git a/Day5/puzzle2.hs b/Day5/puzzle2.hs new file mode 100644 index 0000000..5888cd5 --- /dev/null +++ b/Day5/puzzle2.hs @@ -0,0 +1,24 @@ +import Data.List ((\\)) +import Data.List.Split (splitOn) + +isSorted :: [(Int, Int)] -> [Int] -> Bool +isSorted rules [x] = True +isSorted rules (x:xs) = let after = [ p | (p, n) <- rules, n == x ] + in not (any (`elem` after) xs) && isSorted rules xs + +getMiddle :: [Int] -> Int +getMiddle xs = xs !! (length xs `div` 2) + +sortOnRules :: [(Int, Int)] -> [Int] -> [Int] +sortOnRules _ [] = [] +sortOnRules rules (x:xs) = sortOnRules rules beforeArray ++ [x] ++ sortOnRules rules afterArray + where afterArray = xs \\ before + beforeArray = xs \\ afterArray + before = [ p | (p, n) <- rules, n == x ] + +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 + fixUnsorted = map (sortOnRules rules) unsorted + print . sum $ map getMiddle fixUnsorted