diff --git a/2023/.inputs/11 b/2023/.inputs/11 new file mode 100644 index 0000000..314dee1 --- /dev/null +++ b/2023/.inputs/11 @@ -0,0 +1,140 @@ +........................................#...........................#..........................................#............................ +.................................#.....................#...........................#.................#..............................#....... +............#................................#..................#........................................................................... +..#......................................................................................................................................... +................#.........................................................#..............#..................#.......#....................... +......................#....................................................................................................................# +......................................#.........................................................#.............................#............. +.......................................................................#................................#..........................#........ +.......#.................................................#.................................................................................. +...............#................................#........................................................................................... +........................................#............#.........................#...........#..................#............................. +.............................#......................................................................................#.................#..... +......................................................................#..............#.....................................................# +.......................#................................#.....#............................................#.............#.................. +.......#..................................#......................................................#............................#............. +.#..........#..................#............................................................................................................ +.....................................#.................................................................................................#.... +............................................................#...........................#..........................#.............#.......... +.................#.......#...............................................#.......#.......................................................... +..........#.................................................................................................#............................... +..#............................................................................................#......#................#..................#. +................................#.................#..........................................................................#.......#...... +.......................................#.....#......................#....................................................................... +................#........................................................................................................................... +.....#....................#.............................................................................#.................#................. +........................................................................#....................#.............................................. +.........#........................#..................................................................................#...................... +..............#..................................#................#...............#........................#...........................#.... +......................................#...............#..................................................................................... +.......................................................................................#.................................................... +.#.....#.......................................................#..............#....................................#........................ +................#.........#...................................................................#..................................#.......... +..................................#.........#............................................................................................... +.....................................................................................#..................#................................... +.........................................................................#.......................#.......................................... +#........................................#.....#......#.........................................................#........................... +..............#..............................................#...........................................................#..............#... +..............................................................................#..........#.................................................. +..................................................................#......................................................................... +.....#...............#............#................................................#........................#........................#...... +...........................#............................#..................#....................#...........................#............... +..........#................................................................................#...........................#...................# +........................................#................................................................................................... +.....................................................................#....................................#................................. +.............#..................#..............................................................................#..........#................. +.....................................#..........................................#........................................................... +..................................................#.......#.........................................................#...................#... +....#....................................................................................................................................... +...........#............................................................................................#................................... +..............................................#.....................................#......#......#..................................#...... +...................................#.......................................................................................................# +..........................#...........................................#......................................#...............#.............. +........................................................#.......#.......................#................................................... +......#.........#...............................................................#...................#...................................#... +.........................................#................................................................#................................. +#......................#.....#..............................................................#............................................... +............................................................................................................................................ +...........#.........................................................................#........................#............................. +.......................................#.........#.................#...................................................................#.... +...#..............#.........................................#..................#...................#........................................ +..........................................................................#........................................#........................ +....................................................................................................................................#....... +.....................#.......#.............#...................................................#............................................ +...................................#..................................................................#......#..........#..................# +.......#.......................................#.........#...........#............#...........................................#............. +............#............................................................................................................................... +.........................#..............................................................#.........#......................................... +.....................................................................................................................#...................... +................#..........................................................#............................................................#... +#...........................#............#..................#.....#............................................#............................ +.................................#.......................................................................................................... +.......................................................#.................................#.......#......#..........#......#................. +..............#..........#.....................#.....................#........#.....#....................................................... +............................................................................................................................................ +....................................#...........................................................................#..............#............ +..........#..........#.......#............#...................#...........................................#..........................#...... +........................................................................#................................................................... +...............#..............................................................................#.....................#....................... +.........................#...........................................................................#...................................... +...................................#............#.................................................................................#.....#... +.........................................................#....................#.......#..................................................... +...........................................#..........................#..................................................................... +....#......#...........#.............................................................................................#.......#.............. +..............................#..............................................................#........#.............................#....... +.....................................................#...................................................................................#.. +.........................................................................#..............#................................................... +............................................................................................................................................ +...................................#......#.....................................................................#................#.......... +................................................#........................................................................................... +......#.....#......#........................................................#............................................................... +........................#......#...........................#...........#.....................#................................#............. +....................................................#............#......................#....................#.............................. +............................................................................................................................................ +........#......#.....#......#...........................................................................#..............................#.... +...#....................................#..............#.................................................................................... +..............................................................#..................#..............................#.................#......... +.........................................................................................................................#.................# +..........................................................#.....................................#........................................... +................#..................#.......#..............................#...........................#..................................... +.......................................................................................#...........................#........................ +...............................................#............................................................................................ +........................#....................................................................................#...........................#.. +.....#........................#.....................#..............#........................................................................ +#...................................#.....................................................................................#................. +...........................................................#....................#....................................#...................... +...................#......#..................#......................................................................................#....... +......................................................................................#...................#................................. +.........#.......................................................................................#.......................................... +.....................................#.......................#......#......#.............................................#.................. +...............................#................................................................................#.......................#... +............................................................................................................................................ +..................#.......................................#................................................................................. +....#...................................#............#..................#...........................#...............................#....... +......................................................................................#............................#......#................. +......................#......................................#.............................................................................. +.............................................#................................#.........................#......................#............ +..........#........................................................#.......................#..........................#................#.... +.............................#.....#........................................................................................................ +..#..............................................#.....................................#..........#.............#........................... +..............#....................................................................................................................#........ +....................#.....................................................................................#................................. +................................#.......................................#................................................................... +.........................#...................#...........#.................................................................................. +...................................................................#......................................................#................. +.............#...............#....................................................................................................#......... +.......................................#...............................................................#........#........................... +.....#............................................#............#.............................#.......................#....................#. +........................................................#...........................#.........................................#............. +...........................................................................#................................................................ +.#...................#..............#.....................................................#................#................................ +..............#..........................#..........................#...........#........................................................... +......#......................#.............................#..................................#........#............#....................... +........................................................................#................................................................... +.................................................#..........................................................................#............... +.......................#........................................#........................................................................... +....#......#.........................................#.............................................#..............................#......... +....................................................................................#....................................................#.. +...........................#......#.....#...................#............#.................#.......................#........................ +........................................................................................................................#................... +.......#...............................................................................#.................#.................................. diff --git a/2023/haskell/aoc2023.cabal b/2023/haskell/aoc2023.cabal index 6146e9d..3844f0c 100644 --- a/2023/haskell/aoc2023.cabal +++ b/2023/haskell/aoc2023.cabal @@ -28,6 +28,7 @@ library Day08 Day09 Day10 + Day11 Day15 other-modules: diff --git a/2023/haskell/app/Main.hs b/2023/haskell/app/Main.hs index 93ec46b..7048b56 100644 --- a/2023/haskell/app/Main.hs +++ b/2023/haskell/app/Main.hs @@ -12,7 +12,8 @@ import Utils (readInput) --import qualified Day08 --import qualified Day09 --import qualified Day10 -import qualified Day15 +import qualified Day11 +--import qualified Day15 run :: (Show b, Show c) @@ -42,4 +43,5 @@ main = do --run 08 Day08.parse Day08.solveA Day08.solveB --run 09 Day09.parse Day09.solveA Day09.solveB --run 10 Day10.parse Day10.solveA Day10.solveB - run 15 Day15.parse Day15.solveA Day15.solveB + run 11 Day11.parse Day11.solveA Day11.solveA + --run 15 Day15.parse Day15.solveA Day15.solveB diff --git a/2023/haskell/solutions/Day11.hs b/2023/haskell/solutions/Day11.hs new file mode 100644 index 0000000..3e2ade1 --- /dev/null +++ b/2023/haskell/solutions/Day11.hs @@ -0,0 +1,73 @@ +{-# LANGUAGE OverloadedStrings #-} +module Day11 (parse, solveA) where + +import Data.Void (Void) +import Data.Text (Text) +import Data.List (transpose, tails) +import Data.Bifunctor (first, bimap) +import Control.Monad (join) +import Text.Megaparsec (Parsec, errorBundlePretty, runParser, many, choice, eof) +import Text.Megaparsec.Char (char, newline) +import Misc (withCoords) + + +data Cell = E | G deriving (Show, Eq) + +type Row = Int +type Col = Int +type Grid = [[Cell]] + +type Parser = Parsec Void Text + + +parse :: Text -> Either String Grid +parse = first errorBundlePretty . runParser grid "" + where + row :: Parser [Cell] + row = many (choice [E <$ char '.', G <$ char '#']) <* newline + grid :: Parser Grid + grid = many row <* eof + +pairs :: [a] -> [(a, a)] +pairs xs = [(x, y) | (x:ys) <- tails xs, y <- ys] + +input :: Text +input = + "...#......\n\ + \.......#..\n\ + \#.........\n\ + \..........\n\ + \......#...\n\ + \.#........\n\ + \.........#\n\ + \..........\n\ + \.......#..\n\ + \#...#.....\n" + +expanded :: Grid -> ([Row], [Col]) +expanded = bimap filterE (filterE . transpose) . join (,) + where + filterE :: Grid -> [Int] + filterE = map fst . filter (all (E ==) . snd) . zip [0..] + +galaxies :: Grid -> [(Row, Col)] +galaxies = map fst . filter ((G ==) . snd) . withCoords + +solveA :: Grid -> Int +solveA = do + gs <- galaxies + es <- expanded + return $ sum . map (distance es) . pairs $ gs + where + between :: Int -> Int -> Int -> Bool + between a b n + | a < b = a <= n && n <= b + | otherwise = b <= n && n <= a + crossed :: Int -> Int -> [Int] -> Int + crossed a b = length . filter (between a b) + distance :: ([Row], [Col]) -> ((Row, Col), (Row, Col)) -> Int + distance (rs, cs) ((r1, c1), (r2, c2)) = abs (r1 - r2) + abs (c1 - c2) + crossed r1 r2 rs + crossed c1 c2 cs + +-- >>> solveA <$> parse input +-- Right 374 + diff --git a/2023/haskell/tests/Main.hs b/2023/haskell/tests/Main.hs index f580301..e9f7e63 100644 --- a/2023/haskell/tests/Main.hs +++ b/2023/haskell/tests/Main.hs @@ -13,6 +13,7 @@ import qualified Day07 import qualified Day08 import qualified Day09 import qualified Day10 +import qualified Day11 import qualified Day15 @@ -276,12 +277,31 @@ day10 = \L.L7LFJ|||||FJL7||LJ\n\ \L7JLJL-JLJLJL--JLJ.L\n" +day11 :: Test +day11 = + let parsed = Day11.parse input + in TestList + [ TestCase $ assertEqual "A" (Right 374) (Day11.solveA <$> parsed) + ] + where + input = + "...#......\n\ + \.......#..\n\ + \#.........\n\ + \..........\n\ + \......#...\n\ + \.#........\n\ + \.........#\n\ + \..........\n\ + \.......#..\n\ + \#...#.....\n" + day15 :: Test day15 = let parsed = Day15.parse input in TestList [ TestCase $ assertEqual "A" (Right 1320) (Day15.solveA <$> parsed) - , TestCase $ assertEqual "A" (Right 145) (Day15.solveB <$> parsed) + , TestCase $ assertEqual "B" (Right 145) (Day15.solveB <$> parsed) ] where input = "rn=1,cm-,qp=3,cm=2,qp-,pc=4,ot=9,ab=5,pc-,pc=6,ot=7" @@ -298,6 +318,7 @@ tests = TestList , TestLabel "08" day08 , TestLabel "09" day09 , TestLabel "10" day10 + , TestLabel "11" day11 , TestLabel "15" day15 ]