Added split package to dev env

This commit is contained in:
Riley Apeldoorn 2021-12-06 19:48:35 +01:00
parent 18ea7d1466
commit f818178760
2 changed files with 17 additions and 13 deletions

View file

@ -1,9 +1,17 @@
import Data.List
import Data.List.Split
import Control.Monad (join)
import Control.Concurrent
main :: IO ()
main = readFile "5/riley.txt" >>= print . solve . parse . lines
main = do
input <- readFile "5/example.txt"
let x = parse $ lines input
print $ f x
print $ g x
where
f = length . nub . solve . filter (not . diagonal)
g = length . nub . solve
parse :: [String] -> [(Point,Point)]
parse = map (parseLine . words)
@ -16,7 +24,7 @@ parseLine [ s, "->", e ] = (a, b)
toRange :: (Point,Point) -> [Point]
toRange (a @ (xa,ya), b @ (xb,yb))
| diagonal (a,b) = makeDiagonal a b
| otherwise = [ (x,y) | x <- [ x1 .. x2 ], y <- [ y1 .. y2 ] ]
| otherwise = [ (x,y) | x <- [ x1 .. x2 ], y <- [ y1 .. y2 ] ]
where
x1 = min xa xb
x2 = max xa xb
@ -26,16 +34,11 @@ toRange (a @ (xa,ya), b @ (xb,yb))
parseCoord :: String -> (Int,Int)
parseCoord l = (head n, last n)
where
n = map read $ words [ if c == ',' then ' ' else c | c <- l ]
n = map read . wordsBy (== ',') $ l
solve :: [(Point,Point)] -> (Int,Int)
solve i = (f i, g i)
where f = length . nub . overlapping . filter (not . diagonal)
g = length . nub . overlapping
overlapping :: [(Point, Point)] -> [Point]
overlapping [_] = []
overlapping (p:ps) = overlapping' p ps ++ overlapping ps
solve :: [(Point, Point)] -> [Point]
solve [_] = []
solve (p:ps) = overlapping' p ps ++ solve ps
overlapping' :: (Point, Point) -> [(Point, Point)] -> [Point]
overlapping' _ [] = []

View file

@ -1,7 +1,8 @@
{ pkgs ? import <nixpkgs> {} }: pkgs.mkShell {
nativeBuildInputs = with pkgs; [
haskell-language-server
cabal-install
ghc
(ghc.withPackages (pkgs: with pkgs; [
split
]))
];
}