Skip to content

Commit

Permalink
simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
glguy committed Dec 22, 2023
1 parent 7e63b98 commit 2269d03
Showing 1 changed file with 12 additions and 17 deletions.
29 changes: 12 additions & 17 deletions solutions/src/2023/22.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{-# Language QuasiQuotes, MonadComprehensions, ImportQualifiedPost, DataKinds, GADTs #-}
{-# Language QuasiQuotes, MonadComprehensions, DataKinds, GADTs #-}
{-|
Module : Main
Description : Day 22 solution
Expand All @@ -25,11 +25,10 @@ Maintainer : [email protected]
-}
module Main (main) where

import Advent (format, count, countBy)
import Advent (format, count, countBy, pickOne)
import Advent.Box (intersectBox, Box(Pt, Dim), Box')
import Control.Parallel.Strategies (parMap, rseq)
import Control.Parallel.Strategies (parList, rseq, runEval)
import Data.List (delete, sort)
import Data.Map qualified as Map
import Data.Maybe (isNothing)
import Data.Ord (comparing)

Expand All @@ -43,15 +42,12 @@ main =
do input <- [format|2023 22 (%d,%d,%d~%d,%d,%d%n)*|]
let bricks = map toBrick input
let sunk = lowerAll bricks
let support = parMap rseq (countSupported sunk) sunk
let support = runEval (parList rseq [countFalls xs | (_,xs) <- pickOne sunk])
print (count 0 support)
print (sum support)

countSupported :: [Box' 3] -> Box' 3 -> Int
countSupported bricks brick =
let bricks' = delete brick bricks in
length bricks' -
length (lowerOnes bricks')
countSupported bricks brick = countFalls (delete brick bricks)

lowerAll :: [Box' 3] -> [Box' 3]
lowerAll = foldl lowerOne [] . sort
Expand All @@ -63,21 +59,20 @@ lowerAll = foldl lowerOne [] . sort

| otherwise = x:xs

lowerOnes :: [Box' 3] -> [Box' 3]
lowerOnes = foldl lowerOne [] . sort
countFalls :: [Box' 3] -> Int
countFalls = fst . foldl lowerOne (0, []) . sort
where
lowerOne xs x
lowerOne (n, xs) x
| Just x' <- lower x
, all (isNothing . intersectBox x') xs
= xs
= (n + 1, xs)

| otherwise = x:xs
| otherwise = (n, x:xs)

lower :: Box' 3 -> Maybe (Box' 3)
lower (Dim z1 z2 (Dim x1 x2 (Dim y1 y2 Pt))) =
[Dim (z1-1) (z2-1) (Dim x1 x2 (Dim y1 y2 Pt)) | z1 > 1]
lower (Dim z1 z2 box) = [Dim (z1 - 1) (z2 - 1) box | z1 > 1]

toBrick :: (Int,Int,Int,Int,Int,Int) -> Box' 3
toBrick :: (Int, Int, Int, Int, Int, Int) -> Box' 3
toBrick (x1,y1,z1,x2,y2,z2) = dim z1 z2 (dim x1 x2 (dim y1 y2 Pt))
where
dim a b = Dim (min a b) (max a b + 1)

0 comments on commit 2269d03

Please sign in to comment.