diff options
author | Julien Dessaux | 2023-12-01 00:18:14 +0100 |
---|---|---|
committer | Julien Dessaux | 2023-12-01 00:19:04 +0100 |
commit | 9ecef46ac2259b94124124d72bba4b1876cc59a9 (patch) | |
tree | ab2e54b0c53fd002dc97b0d08f569c20aa0c7581 | |
parent | 2022-22 part 2 in haskell (diff) | |
download | advent-of-code-9ecef46ac2259b94124124d72bba4b1876cc59a9.tar.gz advent-of-code-9ecef46ac2259b94124124d72bba4b1876cc59a9.tar.bz2 advent-of-code-9ecef46ac2259b94124124d72bba4b1876cc59a9.zip |
2022-23 part 1 in haskell
-rw-r--r-- | 2022/23-Unstable-Diffusion/example | 7 | ||||
-rw-r--r-- | 2022/23-Unstable-Diffusion/first.hs | 117 | ||||
-rw-r--r-- | 2022/23-Unstable-Diffusion/input | 71 |
3 files changed, 195 insertions, 0 deletions
diff --git a/2022/23-Unstable-Diffusion/example b/2022/23-Unstable-Diffusion/example new file mode 100644 index 0000000..14005cf --- /dev/null +++ b/2022/23-Unstable-Diffusion/example @@ -0,0 +1,7 @@ +....#.. +..###.# +#...#.# +.#...## +#.###.. +##.#.## +.#..#.. diff --git a/2022/23-Unstable-Diffusion/first.hs b/2022/23-Unstable-Diffusion/first.hs new file mode 100644 index 0000000..b6069c0 --- /dev/null +++ b/2022/23-Unstable-Diffusion/first.hs @@ -0,0 +1,117 @@ +-- requires cabal install --lib megaparsec parser-combinators vector +module Main (main) where +import Control.Monad (void, when) +import Data.Functor +import Data.List qualified as L +import Data.Map qualified as M +import Data.Void (Void) +import Text.Megaparsec +import Text.Megaparsec.Char +import System.Exit (die) + +exampleExpectedOutput = 110 + +type Input = M.Map (Int, Int) Bool -- True if there is an elf + +type Parser = Parsec Void String + +inputToList :: String -> [((Int, Int), Bool)] +inputToList = snd . L.foldl' nextElf ((0, 0), []) + where + nextElf :: ((Int, Int), [((Int, Int), Bool)]) -> Char -> ((Int, Int), [((Int, Int), Bool)]) + nextElf ((x, y), acc) '\n' = ((0, y+1), acc) + nextElf ((x, y), acc) '.' = ((x+1, y), acc) + nextElf ((x, y), acc) '#' = ((x+1, y), acc ++ [((x, y), True)]) + +parseInput :: String -> IO Input +parseInput filename = do + input <- readFile filename + return $ M.fromList (inputToList input) + +type Proposals = M.Map (Int, Int) (Int, Int, Bool) + +firstHalfStep :: Input -> Int -> Proposals +firstHalfStep input iter = M.filter (\(_, _, e) -> e) $ M.foldrWithKey eval M.empty input + where + eval :: (Int, Int) -> Bool -> Proposals -> Proposals + eval (x, y) _ = eval' (x, y, 0) + eval' :: (Int, Int, Int) -> Proposals -> Proposals + eval' (x, y, 4) acc = acc + eval' (x, y, r) acc | alone = acc + | otherwise = case (iter + r) `mod` 4 of + 0 -> if M.member (x-1, y-1) input || M.member (x, y-1) input || M.member (x+1, y-1) input + then next + else if M.member (x, y-1) acc + then M.insert (x, y-1) (x, y, False) acc -- two or more elves attempted to move to the same spot + else M.insert (x, y-1) (x, y, True) acc + 1 -> if M.member (x-1, y+1) input || M.member (x, y+1) input || M.member (x+1, y+1) input + then next + else if M.member (x, y+1) acc + then M.insert (x, y+1) (x, y, False) acc -- two or more elves attempted to move to the same spot + else M.insert (x, y+1) (x, y, True) acc + 2 -> if M.member (x-1, y-1) input || M.member (x-1, y) input || M.member (x-1, y+1) input + then next + else if M.member (x-1, y) acc + then M.insert (x-1, y) (x, y, False) acc -- two or more elves attempted to move to the same spot + else M.insert (x-1, y) (x, y, True) acc + 3 -> if M.member (x+1, y-1) input || M.member (x+1, y) input || M.member (x+1, y+1) input + then next + else if M.member (x+1, y) acc + then M.insert (x+1, y) (x, y, False) acc -- two or more elves attempted to move to the same spot + else M.insert (x+1, y) (x, y, True) acc + where + alone = M.notMember (x-1, y-1) input + && M.notMember (x, y-1) input + && M.notMember (x+1, y-1) input + && M.notMember (x-1, y) input + && M.notMember (x+1, y) input + && M.notMember (x-1, y+1) input + && M.notMember (x, y+1) input + && M.notMember (x+1, y+1) input + next = eval' (x, y, r+1) acc + +secondHalfStep :: Input -> Proposals -> Input +secondHalfStep = M.foldrWithKey eval + where + eval :: (Int, Int) -> (Int, Int, Bool) -> Input -> Input + eval (x', y') (x, y, _) acc = M.insert (x', y') True $ M.delete (x, y) acc + +compute :: Input -> Int -> Int +compute input imax = compute' 0 input + where + compute' :: Int -> Input -> Int + compute' i input | imax == i = (xmax - xmin + 1) * (ymax - ymin + 1) - M.size input + | otherwise = compute' (i+1) $ secondHalfStep input (firstHalfStep input i) + where + keys = M.keys input + xs = L.map fst keys + ys = L.map snd keys + xmax = maximum xs + xmin = minimum xs + ymax = maximum ys + ymin = minimum ys + +main :: IO () +main = do + example <- parseInput "example" + --putStr $ showMap example + let exampleOutput = compute example 10 + when (exampleOutput /= exampleExpectedOutput) (die $ "example failed: got " ++ show exampleOutput ++ " instead of " ++ show exampleExpectedOutput) + input <- parseInput "input" + print $ compute input 10 + +--showMap :: Input -> String +--showMap input = printMap' xmin ymin "" +-- where +-- printMap' :: Int -> Int -> String -> String +-- printMap' x y acc | M.member (x, y) input = printMap' (x+1) y (acc ++ "#") +-- | y > ymax = acc +-- | x <= xmax = printMap' (x+1) y (acc ++ ".") +-- | otherwise = printMap' xmin (y+1) (acc ++ "\n") +-- keys = M.keys input +-- xs = L.map fst keys +-- ys = L.map snd keys +-- xmax = maximum xs +-- xmin = minimum xs +-- ymax = maximum ys +-- ymin = minimum ys diff --git a/2022/23-Unstable-Diffusion/input b/2022/23-Unstable-Diffusion/input new file mode 100644 index 0000000..67c1637 --- /dev/null +++ b/2022/23-Unstable-Diffusion/input @@ -0,0 +1,71 @@ +.###.#.#.#..###.##.#.....#.####.####..##.#.##.#....##.###.#..####.##.#. +.###.#...######....##.#..###..#.#.##.#.######..##.#.....#...#.#.##.##.. +.##....#..#...#..##.....##......#.#..#......#..#..###..#..##..#.#.##.#. +#####.###.##.#.##.#.#...##.##.#.#..####...#.#..####.#.######.###.###### +#...#.#.#####....#.#.###..#####..####.#.#....###.#.####..##..###.####.. +#.#.#.#####.....#....##....####.#..#...##.##..##..##..#..#...####.#..## +....#.#..####.#...#........##...##...#.##.##########...###..#####.#...# +.####.#...##.#..##....###.##..#.###.#.##..######.##.#.#.##....##..#.... +..#...#...#..#####......#.....#.##..#.####.##...#####..#..###.#..##.... +#..#.#.#....####...#..##..#...#..#.#####.#.##..###.#.#.#...#######.#... +.#.#.####....###...#..#.#...#.#.####..#..#........#..###....########.#. +#.##.#.##.#...#.##.###....##...####.###...#.##.##..##....#...####.#..#. +.#.#.#.#..###..#.####..######.##..#.#######...####.#.#.#.##...#.....### +#..###...##......#..#....#..#..##......#####...#.##.....#.###..#...#..# +.####...#.#....###.#.....#.##.#.....##...##.....#..#####..###.##.#.#.## +..#...#.#.#...##########..###.###....#..###....##..#.########.##.#.#.## +####..#..#.####...####..##.##.##....#..###.###.......##.......#.##...## +...#####..#.####.#.#.#.###..#..#..#.##.##..#.###.###.#...#.#.##.....#.. +##..###.##.###.##.####.#.#.....##.##.#..###..####..########.###..###.## +#.#.#..#....###.#...#.#...#..###.###..#.##.##.####.##.##.##..#..###..## +#..###.....##.#####.###..##.###.#.....##.#....###..######..##.#.###...# +#.##..##.#.#.#.##.#..##..#..##..#....#..###.......###.####..##...#.#.#. +##...#..##..#.#..##.##...#.#.#..####.#..##...#.#...######....##.#...#.. +#.###...#####..####..##..##.###..##.#.####..##.#.###.#.#..#......#..... +.#...#.#.###........#.#.#...#..####...#.#####.#####...#....###.##.###.. +....###....####..##..###.##..##.##.#.###.##.#.#.....#.....###.##...##.. +#....######.....#.#...#.#..###.....#....#..##.##.###..##....#.###...#.. +#.#.#.#.#...#.#.#...#.#.....####.###.#####.#.##..###..#.#####.#......#. +.##.####...######.#.###.##.###..#.##..#.#.#....##.#.#####.#...####....# +.#.#.##..##..###.#.#....####....#......#..##..###..########...#.##.##.# +#.###.##...#....#..###..#......##..#....#.####..#.##..#.##.#.##.#...### +#..#....##.###..###..##..#...#......#..####.#.#.#######.#...#####..#..# +..#.###.#..###.###......#.####..###..#.#..#.#.....##...#..#.#.##.###... +...##.........#..###.##..#######.######....######.#####.#.#.#.#..###### +###.##..###.#.####...###..#..#...#...##.#.#......#..#.###..#.#...#....# +...###....##....#.##.#...###....##.#...#..#...#.#.##..#..#..####....#.# +.#.#..#########....#.##.##..#.##..##.###..#.###..##.#..#.####.#.##.##.. +##..####..###.#.....#.#.#.##...#.##....##.#.#.....##.....#..##.#.##.##. +#..#.######.#.###...#..#.#.###.##.##.#.###.....#.......####..#...###..# +.##..##.###.#.###.#######.#.#.#.##.#....#...#####....###.######..#..##. +#.####....#..#...###.....###..##...###....##.#..#..##...##..#.####..#.. +..###.##.###.#.#....##.####.#.#...#.#####.####..#.#.#.###.##.###...#### +##.##..#..##.....#####..####.##.#.#.##..##.#.#..##..#.##.#.#....####... +###.#.#....#...#...#.#...#.#..#.#.###.###.##..#..#..###.##..#.#...##... +...#.....#.##...##....##...##.#.#..#.##.#.#..##.####.#.##..#.......#... +#....####.#.....###.##.#.###.#####.##....#.##..#####.....#.##.##..###.# +.#..#..##.#..##.#.######.##...##...#.##..##..#.#.#..##.#....##...###### +....##.##.#...#.###....##..#..#..#..#...#...####.##.########.###..##..# +###...####......#.#..#..#.##.####.#.##.#.##.#..##.#..###..####..#.#.### +..#####...#.#.....#....####.##..##.##..###...##.###..##.####.####..###. +..#.##.##...##..##.#..##.#.###...#.#..##.#.##.###.#.#.#.#.#..#..##.#..# +.#.....#..####..#...#.#..####.#####.#..###.##.###.###.#.......####.##.. +.#.#####.#....#..##..#.###.####.#.....#..#.####..#.#.#....#.##.##..###. +.....#...#.#..#.#.#.#.##..####.##.##.###.#......#..#.##...####.#.#.#.#. +##...#..#..##..##..#...#.##.###....#..#..##..#.##.#.###.###.#....#.##.. +#.##.##...#.....##...###.#####..###..#.#.#..#.#.....#####.###.#..##...# +.###..#.#.#...###.##...##...#..#.#.#...#.###....###......#.#.###..###.# +###..#...###...#.#.##.#..##.#.....#..##.#..#########..##.#.###.#.#.#... +.##.#.#.#.#...#...##.##..#.##..#########..#.##.###.##.####.#.##.####.## +##.#####...##.....##.....##....###...#....#...#.##.#..##.#...#.#.#...#. +##....#.#....##.##..###....####.#...######.#..##......#.####...#.#.##.# +#####.#...#.....###....#.##....#..#.##.#####.########.###.#..#.##.#..## +##..#.#..##.#.##...#...##.#####.###.####.#...##...###...##..#...###.### +#.##..#....######..#.###.....#.#.####.##..##.##.#####......#.#.###..### +.#####..####.#.##...###.....#######..######.#.##....#..##.##.##.##.#.## +...##.....#.###.####.#.###.#......##.#....#..###.....#.###.......####.. +.#.########..#.#...###.#.##....#.##.####.#.##....#.###.#.####.#.#...##. +.###.#.#.#.#..#.##..#####..#######...#...#..######.....###...##.#.##.#. +.#.....##..#.#..##.#.#..#.#..##.#..#...##..######.#.########...###.#... +..###.##..#.##..#........#.#....###..#.....##.##.##..#.##..###.#...#.## +..#.####.######.#......#...###.#...##.#...###.###.#..###.#..##.#..####. |