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)
@ -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
]))
]; ];
} }