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
import Data.List.Split
import Control.Monad (join) import Control.Monad (join)
import Control.Concurrent import Control.Concurrent
main :: IO () 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 :: [String] -> [(Point,Point)]
parse = map (parseLine . words) parse = map (parseLine . words)
@ -16,7 +24,7 @@ parseLine [ s, "->", e ] = (a, b)
toRange :: (Point,Point) -> [Point] toRange :: (Point,Point) -> [Point]
toRange (a @ (xa,ya), b @ (xb,yb)) toRange (a @ (xa,ya), b @ (xb,yb))
| diagonal (a,b) = makeDiagonal a b | 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 where
x1 = min xa xb x1 = min xa xb
x2 = max xa xb x2 = max xa xb
@ -26,16 +34,11 @@ toRange (a @ (xa,ya), b @ (xb,yb))
parseCoord :: String -> (Int,Int) parseCoord :: String -> (Int,Int)
parseCoord l = (head n, last n) parseCoord l = (head n, last n)
where where
n = map read $ words [ if c == ',' then ' ' else c | c <- l ] n = map read . wordsBy (== ',') $ l
solve :: [(Point,Point)] -> (Int,Int) solve :: [(Point, Point)] -> [Point]
solve i = (f i, g i) solve [_] = []
where f = length . nub . overlapping . filter (not . diagonal) solve (p:ps) = overlapping' p ps ++ solve ps
g = length . nub . overlapping
overlapping :: [(Point, Point)] -> [Point]
overlapping [_] = []
overlapping (p:ps) = overlapping' p ps ++ overlapping ps
overlapping' :: (Point, Point) -> [(Point, Point)] -> [Point] overlapping' :: (Point, Point) -> [(Point, Point)] -> [Point]
overlapping' _ [] = [] overlapping' _ [] = []

View File

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