From 9a63ed8b4353fd196726c7fa759841d1da95d4d3 Mon Sep 17 00:00:00 2001 From: Felix Albrigtsen Date: Sun, 4 Dec 2022 21:56:11 +0100 Subject: [PATCH] Renamed files --- day04/main.hs | 68 +++++++++++++++++++++--------------------- day04/main_improved.hs | 54 --------------------------------- day04/main_original.hs | 54 +++++++++++++++++++++++++++++++++ 3 files changed, 88 insertions(+), 88 deletions(-) delete mode 100644 day04/main_improved.hs create mode 100644 day04/main_original.hs diff --git a/day04/main.hs b/day04/main.hs index 3b073e0..90db626 100644 --- a/day04/main.hs +++ b/day04/main.hs @@ -1,49 +1,49 @@ -import Data.Char - + -- main.hs as written by me, thuroughly fixed by +main :: IO () main = do - let list = [] file <- readFile "input.txt" - let inputLines = lines file - let stringpairs = map (splitWhen (==',')) inputLines + let + pairs :: [((Int, Int), (Int, Int))] + pairs = inputLinesToPairs file - let listpairs = map (map parseRange) stringpairs - let pairs = map (\x -> (head x, last x)) listpairs + putStrLn "Part 1: " + print $ part1 pairs - -- Each line of input has now gone from "1-2,3-4" to ((1,2),(3,4)) + putStrLn "Part 2: " + print $ part2 pairs - let containedWithin = map (uncurry rangeIsContained) pairs - let containedCount = length $ filter (==True) containedWithin - print "Part 1: " - print containedCount +-- Map each line of input from "1-2,3-4" to ((1,2),(3,4)) +inputLinesToPairs :: String -> [((Int, Int), (Int, Int))] +inputLinesToPairs input = result + where + stringpairs :: [[String]] + stringpairs = map (splitWhen (==',')) $ lines input - let overlap = map (uncurry rangeOverlaps) pairs - let overlapCount = length $ filter (==True) overlap - print "Part 2: " - print overlapCount + parseRange :: String -> (Int, Int) + parseRange pair = (read first, read second) + where + (first:second:_) = splitWhen (=='-') pair + + result :: [((Int, Int), (Int, Int))] + result = map (\x -> (head x, last x)) $ map (map parseRange) stringpairs --- "1-3" -> (1,3) -parseRange :: String -> (Int, Int) -parseRange pair = do - let numbers = splitWhen (=='-') pair - let first = read (numbers !! 0) :: Int - let second = read (numbers !! 1) :: Int - (first, second) +part1 :: [((Int, Int), (Int, Int))] -> Int +part1 = length . filter (uncurry rangeIsContained) --- True if the first range is entirely contained in the second range, or vice versa -rangeIsContained :: (Int, Int) -> (Int, Int) -> Bool -rangeIsContained (lo1, hi1) (lo2, hi2) - | lo1 <= lo2 && hi1 >= hi2 = True - | lo1 >= lo2 && hi1 <= hi2 = True - | otherwise = False +part2 :: [((Int, Int), (Int, Int))] -> Int +part2 = length . filter (uncurry rangeOverlaps) -- True if any part of the two ranges overlap rangeOverlaps :: (Int, Int) -> (Int, Int) -> Bool -rangeOverlaps (lo1, hi1) (lo2, hi2) - | rangeIsContained (lo1, lo1) (lo2, hi2) = True - | lo2 >= lo1 && lo2 <= hi1 = True - | hi2 >= lo1 && hi2 <= hi1 = True - | otherwise = False +rangeOverlaps (lo1, hi1) (lo2, hi2) = rangeIsContained (lo1, lo1) (lo2, hi2) + || (lo2 >= lo1 && lo2 <= hi1) + || (hi2 >= lo1 && hi2 <= hi1) + +-- True if the first range is entirely contained in the second range, or vice versa +rangeIsContained :: (Int, Int) -> (Int, Int) -> Bool +rangeIsContained (lo1, hi1) (lo2, hi2) = (lo1 <= lo2 && hi1 >= hi2) + || (lo1 >= lo2 && hi1 <= hi2) -- Modified from Prelude.words splitWhen :: (Char -> Bool) -> String -> [String] diff --git a/day04/main_improved.hs b/day04/main_improved.hs deleted file mode 100644 index 90db626..0000000 --- a/day04/main_improved.hs +++ /dev/null @@ -1,54 +0,0 @@ - -- main.hs as written by me, thuroughly fixed by -main :: IO () -main = do - file <- readFile "input.txt" - let - pairs :: [((Int, Int), (Int, Int))] - pairs = inputLinesToPairs file - - putStrLn "Part 1: " - print $ part1 pairs - - putStrLn "Part 2: " - print $ part2 pairs - --- Map each line of input from "1-2,3-4" to ((1,2),(3,4)) -inputLinesToPairs :: String -> [((Int, Int), (Int, Int))] -inputLinesToPairs input = result - where - stringpairs :: [[String]] - stringpairs = map (splitWhen (==',')) $ lines input - - parseRange :: String -> (Int, Int) - parseRange pair = (read first, read second) - where - (first:second:_) = splitWhen (=='-') pair - - result :: [((Int, Int), (Int, Int))] - result = map (\x -> (head x, last x)) $ map (map parseRange) stringpairs - - -part1 :: [((Int, Int), (Int, Int))] -> Int -part1 = length . filter (uncurry rangeIsContained) - -part2 :: [((Int, Int), (Int, Int))] -> Int -part2 = length . filter (uncurry rangeOverlaps) - --- True if any part of the two ranges overlap -rangeOverlaps :: (Int, Int) -> (Int, Int) -> Bool -rangeOverlaps (lo1, hi1) (lo2, hi2) = rangeIsContained (lo1, lo1) (lo2, hi2) - || (lo2 >= lo1 && lo2 <= hi1) - || (hi2 >= lo1 && hi2 <= hi1) - --- True if the first range is entirely contained in the second range, or vice versa -rangeIsContained :: (Int, Int) -> (Int, Int) -> Bool -rangeIsContained (lo1, hi1) (lo2, hi2) = (lo1 <= lo2 && hi1 >= hi2) - || (lo1 >= lo2 && hi1 <= hi2) - --- Modified from Prelude.words -splitWhen :: (Char -> Bool) -> String -> [String] -splitWhen p s = case dropWhile p s of - "" -> [] - s' -> w : splitWhen p s'' - where (w, s'') = break p s' - diff --git a/day04/main_original.hs b/day04/main_original.hs new file mode 100644 index 0000000..3b073e0 --- /dev/null +++ b/day04/main_original.hs @@ -0,0 +1,54 @@ +import Data.Char + +main = do + let list = [] + file <- readFile "input.txt" + let inputLines = lines file + let stringpairs = map (splitWhen (==',')) inputLines + + let listpairs = map (map parseRange) stringpairs + let pairs = map (\x -> (head x, last x)) listpairs + + -- Each line of input has now gone from "1-2,3-4" to ((1,2),(3,4)) + + let containedWithin = map (uncurry rangeIsContained) pairs + let containedCount = length $ filter (==True) containedWithin + print "Part 1: " + print containedCount + + let overlap = map (uncurry rangeOverlaps) pairs + let overlapCount = length $ filter (==True) overlap + print "Part 2: " + print overlapCount + + +-- "1-3" -> (1,3) +parseRange :: String -> (Int, Int) +parseRange pair = do + let numbers = splitWhen (=='-') pair + let first = read (numbers !! 0) :: Int + let second = read (numbers !! 1) :: Int + (first, second) + +-- True if the first range is entirely contained in the second range, or vice versa +rangeIsContained :: (Int, Int) -> (Int, Int) -> Bool +rangeIsContained (lo1, hi1) (lo2, hi2) + | lo1 <= lo2 && hi1 >= hi2 = True + | lo1 >= lo2 && hi1 <= hi2 = True + | otherwise = False + +-- True if any part of the two ranges overlap +rangeOverlaps :: (Int, Int) -> (Int, Int) -> Bool +rangeOverlaps (lo1, hi1) (lo2, hi2) + | rangeIsContained (lo1, lo1) (lo2, hi2) = True + | lo2 >= lo1 && lo2 <= hi1 = True + | hi2 >= lo1 && hi2 <= hi1 = True + | otherwise = False + +-- Modified from Prelude.words +splitWhen :: (Char -> Bool) -> String -> [String] +splitWhen p s = case dropWhile p s of + "" -> [] + s' -> w : splitWhen p s'' + where (w, s'') = break p s' +