From: Neil Smith Date: Sun, 20 Dec 2020 16:59:05 +0000 (+0000) Subject: Fixing overlong lines X-Git-Url: https://git.njae.me.uk/?a=commitdiff_plain;h=29632086e7a87829401fb09a8ddd809d79502347;p=advent-of-code-20.git Fixing overlong lines --- diff --git a/advent11/src/advent11.hs b/advent11/src/advent11.hs index b56dc3d..6fdfd62 100644 --- a/advent11/src/advent11.hs +++ b/advent11/src/advent11.hs @@ -85,7 +85,8 @@ neighbourhood seats here = S.intersection (M.keysSet seats) (neighbours here) allNeighbourhoods :: Seats -> Neighbourhood allNeighbourhoods seats = M.mapWithKey (\h _ -> neighbourhood seats h) seats -occupiedNeighbours seats nbrs here = M.filter (== Occupied) $ M.restrictKeys seats (nbrs!here) +occupiedNeighbours seats nbrs here = M.filter (== Occupied) + $ M.restrictKeys seats (nbrs!here) onSightLine :: Position -> Direction -> Position -> Bool @@ -106,15 +107,12 @@ closestInDirection seats here direction = take 1 sortedSeats sortedSeats = sortOn (manhattan here) seatsInDirection closestInSight :: Seats -> Position -> (S.Set Position) -closestInSight seats here = S.fromList $ concatMap (closestInDirection seats here) [d | d <- [Up .. UpLeft]] +closestInSight seats here = S.fromList + $ concatMap (closestInDirection seats here) [d | d <- [Up .. UpLeft]] allSightNeighbourhoods :: Seats -> Neighbourhood allSightNeighbourhoods seats = M.mapWithKey (\h _ -> closestInSight seats h) seats --- occupiedInSight :: Seats -> Position -> Seats --- occupiedInSight seats here = M.filter (== Occupied) $ M.restrictKeys seats $ closestInSight seats here - - readGrid :: String -> (Seats, Position) readGrid input = (seats, (maxR, maxC)) diff --git a/advent11/src/advent11naive.hs b/advent11/src/advent11naive.hs index ac6d260..4c55edf 100644 --- a/advent11/src/advent11naive.hs +++ b/advent11/src/advent11naive.hs @@ -56,7 +56,9 @@ ruleB seats here thisSeat where nOccs = M.size $ occupiedInSight seats here -neighbours (r, c) = S.delete (r, c) $ S.fromList [(r + dr, c + dc) | dr <- [-1, 0, 1], dc <- [-1, 0, 1]] +neighbours (r, c) = S.delete (r, c) + $ S.fromList [ (r + dr, c + dc) + | dr <- [-1, 0, 1], dc <- [-1, 0, 1]] neighbourhood seats here = M.restrictKeys seats (neighbours here) occupiedNeighbours seats here = M.filter (== Occupied) $ neighbourhood seats here @@ -75,15 +77,18 @@ onSightLine (r0, c0) UpRight (r, c) = ((r - r0) < 0) && ((r - r0) == (c0 - c)) manhattan (r1, c1) (r2, c2) = abs (r1 - r2) + abs (c1 - c2) closestInDirection seats here direction = take 1 sortedSeats - -- where seatsInDirection = M.keys $ M.filterWithKey (\o _ -> onSightLine here direction o) seats where seatsInDirection = filter (onSightLine here direction) $ M.keys seats sortedSeats = sortOn (manhattan here) seatsInDirection closestInSight :: Seats -> Position -> (S.Set Position) -closestInSight seats here = S.fromList $ concatMap (closestInDirection seats here) [d | d <- [Up .. UpLeft]] +closestInSight seats here = + S.fromList $ concatMap (closestInDirection seats here) + [d | d <- [Up .. UpLeft]] occupiedInSight :: Seats -> Position -> Seats -occupiedInSight seats here = M.filter (== Occupied) $ M.restrictKeys seats $ closestInSight seats here +occupiedInSight seats here = M.filter (== Occupied) + $ M.restrictKeys seats + $ closestInSight seats here readGrid :: String -> (Seats, Position)