diff options
author | Julien Dessaux | 2023-03-21 23:27:16 +0100 |
---|---|---|
committer | Julien Dessaux | 2023-03-21 23:27:16 +0100 |
commit | 2f64de03d805ba6eaf8cdf5929dc5b4348841c1c (patch) | |
tree | ab07c14a72ec478532346cd2752929773ee35046 /2020/11-Seating_System | |
parent | 2020-10 in haskell (diff) | |
download | advent-of-code-2f64de03d805ba6eaf8cdf5929dc5b4348841c1c.tar.gz advent-of-code-2f64de03d805ba6eaf8cdf5929dc5b4348841c1c.tar.bz2 advent-of-code-2f64de03d805ba6eaf8cdf5929dc5b4348841c1c.zip |
2020-11 in haskell
Diffstat (limited to '2020/11-Seating_System')
-rw-r--r-- | 2020/11-Seating_System/example | 10 | ||||
-rw-r--r-- | 2020/11-Seating_System/first-arrays.hs | 53 | ||||
-rw-r--r-- | 2020/11-Seating_System/first.hs | 43 | ||||
-rw-r--r-- | 2020/11-Seating_System/input | 90 | ||||
-rw-r--r-- | 2020/11-Seating_System/second.hs | 53 |
5 files changed, 249 insertions, 0 deletions
diff --git a/2020/11-Seating_System/example b/2020/11-Seating_System/example new file mode 100644 index 0000000..1beaede --- /dev/null +++ b/2020/11-Seating_System/example @@ -0,0 +1,10 @@ +L.LL.LL.LL +LLLLLLL.LL +L.L.L..L.. +LLLL.LL.LL +L.LL.LL.LL +L.LLLLL.LL +..L.L..... +LLLLLLLLLL +L.LLLLLL.L +L.LLLLL.LL diff --git a/2020/11-Seating_System/first-arrays.hs b/2020/11-Seating_System/first-arrays.hs new file mode 100644 index 0000000..fac8151 --- /dev/null +++ b/2020/11-Seating_System/first-arrays.hs @@ -0,0 +1,53 @@ +module Main (main) where +import Control.Monad (when) +import Data.Array qualified as A +import Data.List (foldl') +import Data.Maybe (catMaybes) +import System.Exit (die) + +exampleExpectedOutput = 37 + +type Seating = A.Array (Int, Int) (Maybe Bool) + +inputToArrayAssociations :: String -> [((Int, Int), Maybe Bool)] +inputToArrayAssociations = snd . foldl' nextSeat ((0, 0), []) + where + nextSeat :: ((Int, Int), [((Int, Int), Maybe Bool)]) -> Char -> ((Int, Int), [((Int, Int), Maybe Bool)]) + nextSeat ((x, y), acc) '\n' = ((0, y+1), acc) + nextSeat ((x, y), acc) '.' = ((x+1, y), acc ++ [((x, y), Nothing)]) + nextSeat ((x, y), acc) 'L' = ((x+1, y), acc ++ [((x, y), Just False)]) + +parseInput :: String -> IO Seating +parseInput filename = do + input <- readFile filename + let ls = lines input + height = (length ls) - 1 + width = (length (ls !! 0)) - 1 + return $ A.array ((0,0), (width, height)) (inputToArrayAssociations input) + +compute :: Seating -> Int +compute seating + | seating == seating' = length . filter id . catMaybes $ A.elems seating + | otherwise = compute seating' + where + (width, height) = snd $ A.bounds seating + seating' :: Seating + seating' = seating A.// (foldl' next [] (A.assocs seating)) + next :: [((Int, Int), Maybe Bool)] -> ((Int, Int), Maybe Bool) -> [((Int, Int), Maybe Bool)] + next acc ((x, y), Just False) = ((x, y), Just $ around (x, y) == []) : acc + next acc ((x, y), Just True) = ((x, y), Just $ length (around (x,y)) < 4) : acc + next acc _ = acc + around :: (Int, Int) -> [Bool] + around (x, y) = filter id . catMaybes $ map lookup [(x-1, y-1), (x, y-1), (x+1, y-1), (x-1, y), (x+1, y), (x-1, y+1), (x, y+1), (x+1, y+1)] + lookup :: (Int, Int) -> Maybe Bool + lookup (x, y) + | x < 0 || y < 0 || x > width || y > height = Nothing + | otherwise = seating A.! (x, y) + +main :: IO () +main = do + example <- parseInput "example" + let exampleOutput = compute example + when (exampleOutput /= exampleExpectedOutput) (die $ "example failed: got " ++ show exampleOutput ++ " instead of " ++ show exampleExpectedOutput) + input <- parseInput "input" + print $ compute input diff --git a/2020/11-Seating_System/first.hs b/2020/11-Seating_System/first.hs new file mode 100644 index 0000000..d5871d5 --- /dev/null +++ b/2020/11-Seating_System/first.hs @@ -0,0 +1,43 @@ +module Main (main) where +import Control.Monad (when) +import Data.List (foldl') +import Data.Maybe (mapMaybe) +import qualified Data.Map as M +import System.Exit (die) + +exampleExpectedOutput = 37 + +type Seating = M.Map (Int, Int) Bool + +inputToList :: String -> [((Int, Int), Bool)] +inputToList = snd . foldl' nextSeat ((0, 0), []) + where + nextSeat :: ((Int, Int), [((Int, Int), Bool)]) -> Char -> ((Int, Int), [((Int, Int), Bool)]) + nextSeat ((x, y), acc) '\n' = ((0, y+1), acc) + nextSeat ((x, y), acc) '.' = ((x+1, y), acc) + nextSeat ((x, y), acc) 'L' = ((x+1, y), acc ++ [((x, y), False)]) + +parseInput :: String -> IO Seating +parseInput filename = do + input <- readFile filename + return $ M.fromList (inputToList input) + +compute :: Seating -> Int +compute seating + | seating == seating' = length $ M.filter id seating + | otherwise = compute seating' + where + seating' = M.mapWithKey next seating + next :: (Int, Int) -> Bool -> Bool + next (x, y) False = around (x, y) == [] + next (x, y) True = length (around (x,y)) < 4 + around :: (Int, Int) -> [Bool] + around (x, y) = filter id $ mapMaybe (\v -> M.lookup v seating) [(x-1, y-1), (x, y-1), (x+1, y-1), (x-1, y), (x+1, y), (x-1, y+1), (x, y+1), (x+1, y+1)] + +main :: IO () +main = do + example <- parseInput "example" + let exampleOutput = compute example + when (exampleOutput /= exampleExpectedOutput) (die $ "example failed: got " ++ show exampleOutput ++ " instead of " ++ show exampleExpectedOutput) + input <- parseInput "input" + print $ compute input diff --git a/2020/11-Seating_System/input b/2020/11-Seating_System/input new file mode 100644 index 0000000..6e3d9b3 --- /dev/null +++ b/2020/11-Seating_System/input @@ -0,0 +1,90 @@ +LLLLLL.LLLL..LLLLLL.LLLLLLLLLLLLLLLLLLLLL.LLLLLL.LLLLLLLL.LLLLL.LLLLLL.L.LL.LLLLLL.LLLLLLLLLLLLLLLL +LLLLLL.LLLLLLLLL.LLLLLLLLLLLLLL.LLLLLLLLLLLLLLLL.LLLLLLLLLL.LLL.LLLL.L.LLLLLL.LLLLLLLLLLLL.LLLLLLLL +LLLLLLLL.LLL.LL..LLLLLLLLL.L.LLLLLLLLLLLLLLLLLLLLLLLLLLLL.LLLLLLLLLLLL.LLLL.LLLL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L.LLLLL.LLLLLLLL.LL.LLLL.L.LL.LLLLLL.LLLLLL +LLLLLL.LLLLLLLLLLLLLLLLLLL.LLLLLLL.LLLLLL..LLLLL.LLLLLLLL.LLLLLLLLLLLL.LLLLLLLLLLLLLLLL.LLLLLLL.LLL +L..LL...............L...L.......LLL.....LL...L.........L.....L...L..L.......L.LLLL..L.L..LL.LL....L +LLLLLL.LLLLLLLLL.LLLLLL.LL.LLLL.LLLLLLLLL.LLLLLL.LLLLLL.LLLLLLL.LLL.LL.LLLLLLLLLLL.LLLLLLLLL.LLLLLL +LLLLLL.LLLLLLLLLLLLLLLL.LL.LLLL.LLLLLLLLL.LLLLLL.LLLLLLLL.LLLLLLLLLLLL.LLLL.LLLLLL.LLLLL.LLL.LLLLLL +LLLLLL.LLLLLL.LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL.LLLLLLLL.LLLLL.LLLLLL.LLLLLLLLLLL..LLLLLLLLLLLLLLL +.LLLLLLL.LLLLLLL.LLLLLLLLL..LLL.LLLLLLLLL..LLLLL.LLLLLLLL.LLLLL.LLLLLL.LLLL.L.L.LL..LLLLLLLL.L.LLLL +LLLLLLLLLLLLLLLL.LLLLLLLLL.LLLL.LLLLLLLLL.LLLLLL.LLLLLLLL.LLLLLLLLLLL..LLLL.LLL.LL.LLLLLLLLL.LLLLLL +LLLLLLLLLLLLLLLLLLLLLLLLLL.LLLL.LLLLLLLLL.LLLLLL.LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL.LLL.LLL.L.LLLLL. +LLLLLLLLLLLLLLLLLLLLLLLLLL.LLLL.LL.LLLLLLLLLLLLLLLLLLLLLL.LLLLLLLLLLL..LLLLLLLLLLL.LLLLLLLLL.LLLLLL +....L....L.L...L....L..L........L.L..LLL..L.L.L.L..L..L.....L.L....LL...LL..L..LL..LLL....LL...LL.L +.LLLLLLLLLLLLLLL.LLLLLLLLL..LLLLLLLLL.LL..LLLLLL.L.LLLLLL.LLLLLLL.LLLL.LLLLLL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LLLLLLLLLL.LLLLLLLLL.LLLLLL +L.L.....LL........L..L....L.....LLL.L.L.L........L.........L........L..L..L..L.....L....LLL..L..LL. +L.LLLL.LLL.LLLLLLL.LLLLLLL.LLLL.LLLLLLLLL.LLLLLL.LLLLLLL..LLLLL.LLLLLLLLLLLLLLLLL..LLLLLLL.L.LLLLLL +LLLLLL.LLLL.LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL.LLLLLLLLLLLL.L.LLLLLL.LLLL.LLLLLL.LLLLLLLLLLLLLLLL +LLLLLL.LLLLLLLLL.LLLLLLLLLL.LLLLLLLLLLLL.LLLLLLLLLLLLLLL...LLLL.LLLLLL.LLLLLLLLLLL.LLLLLLLLL.LLLLLL +LLLL.L.LL.L.LLLL.LLLLLLLLL.LLLL.LLLLLLLLL.LLLLLL.LLLLLLLL.LLLLL.LLLLLLLLLLLLLLLLLLLLLLLLLL.L.L.LLLL +LL.L.L.L..L..L..L....L....L.L.LLL.L.LLL.......L..LL.....LLL......L.L.L.L..L.LLL...L.......L........ +LLLLLL.LLLLLLLLL.LLLL.LLLL.LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL.LLL.LLLLLL.LLLL.LLLLLL.LLLLLLLLL.LLLLLL +LLLLLL.LLLLLLLLL.LL.LLLLLLLLLLLLLLL..LLLL.LLLLLL.LLLLLLLLLLLLLL.LLLLLLLLLLLLLLLLLLLLLLLLLLLL.LLLLLL +LLLLLL.LLLLLLL.L.L.LLLLLLL.LLLL.LLLLLLLLL.LLLLLLLLLLLL.LL.LLLLL.LLLLLL.LLLL.LLLLLLLLLLLLLLLLLLLLLLL +LLLLLLLLLLLLLLLL.LLLLLLLLLLLLLL.LLLLLLLLL.LLLLLL.LLLLL..LLLLLLLLLL.LLL.LLLL..LLLLL.LLLLLLL.LL.LLLLL +LLLLLLLLLLLLLLLL.LLLL.LLLL.LLLLLLL.LLLLLL.LLLLLLLLLLLLLLL.LLLLL.LLLLLLLLLLL.LLLLLLLLLLLLLLLLLLLLLLL diff --git a/2020/11-Seating_System/second.hs b/2020/11-Seating_System/second.hs new file mode 100644 index 0000000..4b60a8a --- /dev/null +++ b/2020/11-Seating_System/second.hs @@ -0,0 +1,53 @@ +module Main (main) where +import Control.Monad (when) +import Data.List (foldl') +import Data.Maybe (catMaybes, mapMaybe) +import qualified Data.Map as M +import System.Exit (die) + +exampleExpectedOutput = 26 + +type Seating = M.Map (Int, Int) (Maybe Bool) + +inputToList :: String -> [((Int, Int), Maybe Bool)] +inputToList = snd . foldl' nextSeat ((0, 0), []) + where + nextSeat :: ((Int, Int), [((Int, Int), Maybe Bool)]) -> Char -> ((Int, Int), [((Int, Int), Maybe Bool)]) + nextSeat ((x, y), acc) '\n' = ((0, y+1), acc) + nextSeat ((x, y), acc) '.' = ((x+1, y), ((x, y), Nothing) : acc) + nextSeat ((x, y), acc) 'L' = ((x+1, y), ((x, y), Just False) : acc) + +parseInput :: String -> IO Seating +parseInput filename = do + input <- readFile filename + return $ M.fromList (inputToList input) + +compute :: Seating -> Int +compute seating + | seating == seating' = length . filter id . catMaybes $ map snd (M.toList seating) + | otherwise = compute seating' + where + seating' :: Seating + seating' = M.mapWithKey next seating + next :: (Int, Int) -> Maybe Bool -> Maybe Bool + next _ Nothing = Nothing + next (x, y) (Just False) = Just $ around (x, y) == [] + next (x, y) (Just True) = Just $ length (around (x,y)) < 5 + around :: (Int, Int) -> [Bool] + around (x, y) = filter id . catMaybes $ mapMaybe lookup [(-1, -1), (0, -1), (1, -1), (-1, 0), (1, 0), (-1, 1), (0, 1), (1, 1)] + where + lookup :: (Int, Int) -> Maybe (Maybe Bool) + lookup (dx, dy) = lookup' (1, dx, dy) + lookup' :: (Int, Int, Int) -> Maybe (Maybe Bool) + lookup' (i, dx, dy) = case M.lookup (x + i * dx, y + i * dy) seating of + Just (Just a) -> Just $ Just a + Just Nothing -> lookup' (i+1, dx, dy) + Nothing -> Nothing + +main :: IO () +main = do + example <- parseInput "example" + let exampleOutput = compute example + when (exampleOutput /= exampleExpectedOutput) (die $ "example failed: got " ++ show exampleOutput ++ " instead of " ++ show exampleExpectedOutput) + input <- parseInput "input" + print $ compute input |