Renamed files
This commit is contained in:
parent
f7bb5b70c5
commit
9a63ed8b43
@ -1,49 +1,49 @@
|
|||||||
import Data.Char
|
-- main.hs as written by me, thuroughly fixed by <https://github.com/h7x4>
|
||||||
|
main :: IO ()
|
||||||
main = do
|
main = do
|
||||||
let list = []
|
|
||||||
file <- readFile "input.txt"
|
file <- readFile "input.txt"
|
||||||
let inputLines = lines file
|
let
|
||||||
let stringpairs = map (splitWhen (==',')) inputLines
|
pairs :: [((Int, Int), (Int, Int))]
|
||||||
|
pairs = inputLinesToPairs file
|
||||||
|
|
||||||
let listpairs = map (map parseRange) stringpairs
|
putStrLn "Part 1: "
|
||||||
let pairs = map (\x -> (head x, last x)) listpairs
|
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
|
-- Map each line of input from "1-2,3-4" to ((1,2),(3,4))
|
||||||
let containedCount = length $ filter (==True) containedWithin
|
inputLinesToPairs :: String -> [((Int, Int), (Int, Int))]
|
||||||
print "Part 1: "
|
inputLinesToPairs input = result
|
||||||
print containedCount
|
where
|
||||||
|
stringpairs :: [[String]]
|
||||||
|
stringpairs = map (splitWhen (==',')) $ lines input
|
||||||
|
|
||||||
let overlap = map (uncurry rangeOverlaps) pairs
|
parseRange :: String -> (Int, Int)
|
||||||
let overlapCount = length $ filter (==True) overlap
|
parseRange pair = (read first, read second)
|
||||||
print "Part 2: "
|
where
|
||||||
print overlapCount
|
(first:second:_) = splitWhen (=='-') pair
|
||||||
|
|
||||||
|
result :: [((Int, Int), (Int, Int))]
|
||||||
|
result = map (\x -> (head x, last x)) $ map (map parseRange) stringpairs
|
||||||
|
|
||||||
|
|
||||||
-- "1-3" -> (1,3)
|
part1 :: [((Int, Int), (Int, Int))] -> Int
|
||||||
parseRange :: String -> (Int, Int)
|
part1 = length . filter (uncurry rangeIsContained)
|
||||||
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
|
part2 :: [((Int, Int), (Int, Int))] -> Int
|
||||||
rangeIsContained :: (Int, Int) -> (Int, Int) -> Bool
|
part2 = length . filter (uncurry rangeOverlaps)
|
||||||
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
|
-- True if any part of the two ranges overlap
|
||||||
rangeOverlaps :: (Int, Int) -> (Int, Int) -> Bool
|
rangeOverlaps :: (Int, Int) -> (Int, Int) -> Bool
|
||||||
rangeOverlaps (lo1, hi1) (lo2, hi2)
|
rangeOverlaps (lo1, hi1) (lo2, hi2) = rangeIsContained (lo1, lo1) (lo2, hi2)
|
||||||
| rangeIsContained (lo1, lo1) (lo2, hi2) = True
|
|| (lo2 >= lo1 && lo2 <= hi1)
|
||||||
| lo2 >= lo1 && lo2 <= hi1 = True
|
|| (hi2 >= lo1 && hi2 <= hi1)
|
||||||
| hi2 >= lo1 && hi2 <= hi1 = True
|
|
||||||
| otherwise = False
|
-- 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
|
-- Modified from Prelude.words
|
||||||
splitWhen :: (Char -> Bool) -> String -> [String]
|
splitWhen :: (Char -> Bool) -> String -> [String]
|
||||||
|
@ -1,54 +0,0 @@
|
|||||||
-- main.hs as written by me, thuroughly fixed by <https://github.com/h7x4>
|
|
||||||
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'
|
|
||||||
|
|
54
day04/main_original.hs
Normal file
54
day04/main_original.hs
Normal file
@ -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'
|
||||||
|
|
Loading…
Reference in New Issue
Block a user