haskell: 2023 11 a
This commit is contained in:
parent
4b3faa6e12
commit
ff96a275ea
140
2023/.inputs/11
Normal file
140
2023/.inputs/11
Normal file
|
@ -0,0 +1,140 @@
|
|||
........................................#...........................#..........................................#............................
|
||||
.................................#.....................#...........................#.................#..............................#.......
|
||||
............#................................#..................#...........................................................................
|
||||
..#.........................................................................................................................................
|
||||
................#.........................................................#..............#..................#.......#.......................
|
||||
......................#....................................................................................................................#
|
||||
......................................#.........................................................#.............................#.............
|
||||
.......................................................................#................................#..........................#........
|
||||
.......#.................................................#..................................................................................
|
||||
...............#................................#...........................................................................................
|
||||
........................................#............#.........................#...........#..................#.............................
|
||||
.............................#......................................................................................#.................#.....
|
||||
......................................................................#..............#.....................................................#
|
||||
.......................#................................#.....#............................................#.............#..................
|
||||
.......#..................................#......................................................#............................#.............
|
||||
.#..........#..................#............................................................................................................
|
||||
.....................................#.................................................................................................#....
|
||||
............................................................#...........................#..........................#.............#..........
|
||||
.................#.......#...............................................#.......#..........................................................
|
||||
..........#.................................................................................................#...............................
|
||||
..#............................................................................................#......#................#..................#.
|
||||
................................#.................#..........................................................................#.......#......
|
||||
.......................................#.....#......................#.......................................................................
|
||||
................#...........................................................................................................................
|
||||
.....#....................#.............................................................................#.................#.................
|
||||
........................................................................#....................#..............................................
|
||||
.........#........................#..................................................................................#......................
|
||||
..............#..................................#................#...............#........................#...........................#....
|
||||
......................................#...............#.....................................................................................
|
||||
.......................................................................................#....................................................
|
||||
.#.....#.......................................................#..............#....................................#........................
|
||||
................#.........#...................................................................#..................................#..........
|
||||
..................................#.........#...............................................................................................
|
||||
.....................................................................................#..................#...................................
|
||||
.........................................................................#.......................#..........................................
|
||||
#........................................#.....#......#.........................................................#...........................
|
||||
..............#..............................................#...........................................................#..............#...
|
||||
..............................................................................#..........#..................................................
|
||||
..................................................................#.........................................................................
|
||||
.....#...............#............#................................................#........................#........................#......
|
||||
...........................#............................#..................#....................#...........................#...............
|
||||
..........#................................................................................#...........................#...................#
|
||||
........................................#...................................................................................................
|
||||
.....................................................................#....................................#.................................
|
||||
.............#..................#..............................................................................#..........#.................
|
||||
.....................................#..........................................#...........................................................
|
||||
..................................................#.......#.........................................................#...................#...
|
||||
....#.......................................................................................................................................
|
||||
...........#............................................................................................#...................................
|
||||
..............................................#.....................................#......#......#..................................#......
|
||||
...................................#.......................................................................................................#
|
||||
..........................#...........................................#......................................#...............#..............
|
||||
........................................................#.......#.......................#...................................................
|
||||
......#.........#...............................................................#...................#...................................#...
|
||||
.........................................#................................................................#.................................
|
||||
#......................#.....#..............................................................#...............................................
|
||||
............................................................................................................................................
|
||||
...........#.........................................................................#........................#.............................
|
||||
.......................................#.........#.................#...................................................................#....
|
||||
...#..............#.........................................#..................#...................#........................................
|
||||
..........................................................................#........................................#........................
|
||||
....................................................................................................................................#.......
|
||||
.....................#.......#.............#...................................................#............................................
|
||||
...................................#..................................................................#......#..........#..................#
|
||||
.......#.......................................#.........#...........#............#...........................................#.............
|
||||
............#...............................................................................................................................
|
||||
.........................#..............................................................#.........#.........................................
|
||||
.....................................................................................................................#......................
|
||||
................#..........................................................#............................................................#...
|
||||
#...........................#............#..................#.....#............................................#............................
|
||||
.................................#..........................................................................................................
|
||||
.......................................................#.................................#.......#......#..........#......#.................
|
||||
..............#..........#.....................#.....................#........#.....#.......................................................
|
||||
............................................................................................................................................
|
||||
....................................#...........................................................................#..............#............
|
||||
..........#..........#.......#............#...................#...........................................#..........................#......
|
||||
........................................................................#...................................................................
|
||||
...............#..............................................................................#.....................#.......................
|
||||
.........................#...........................................................................#......................................
|
||||
...................................#............#.................................................................................#.....#...
|
||||
.........................................................#....................#.......#.....................................................
|
||||
...........................................#..........................#.....................................................................
|
||||
....#......#...........#.............................................................................................#.......#..............
|
||||
..............................#..............................................................#........#.............................#.......
|
||||
.....................................................#...................................................................................#..
|
||||
.........................................................................#..............#...................................................
|
||||
............................................................................................................................................
|
||||
...................................#......#.....................................................................#................#..........
|
||||
................................................#...........................................................................................
|
||||
......#.....#......#........................................................#...............................................................
|
||||
........................#......#...........................#...........#.....................#................................#.............
|
||||
....................................................#............#......................#....................#..............................
|
||||
............................................................................................................................................
|
||||
........#......#.....#......#...........................................................................#..............................#....
|
||||
...#....................................#..............#....................................................................................
|
||||
..............................................................#..................#..............................#.................#.........
|
||||
.........................................................................................................................#.................#
|
||||
..........................................................#.....................................#...........................................
|
||||
................#..................#.......#..............................#...........................#.....................................
|
||||
.......................................................................................#...........................#........................
|
||||
...............................................#............................................................................................
|
||||
........................#....................................................................................#...........................#..
|
||||
.....#........................#.....................#..............#........................................................................
|
||||
#...................................#.....................................................................................#.................
|
||||
...........................................................#....................#....................................#......................
|
||||
...................#......#..................#......................................................................................#.......
|
||||
......................................................................................#...................#.................................
|
||||
.........#.......................................................................................#..........................................
|
||||
.....................................#.......................#......#......#.............................................#..................
|
||||
...............................#................................................................................#.......................#...
|
||||
............................................................................................................................................
|
||||
..................#.......................................#.................................................................................
|
||||
....#...................................#............#..................#...........................#...............................#.......
|
||||
......................................................................................#............................#......#.................
|
||||
......................#......................................#..............................................................................
|
||||
.............................................#................................#.........................#......................#............
|
||||
..........#........................................................#.......................#..........................#................#....
|
||||
.............................#.....#........................................................................................................
|
||||
..#..............................................#.....................................#..........#.............#...........................
|
||||
..............#....................................................................................................................#........
|
||||
....................#.....................................................................................#.................................
|
||||
................................#.......................................#...................................................................
|
||||
.........................#...................#...........#..................................................................................
|
||||
...................................................................#......................................................#.................
|
||||
.............#...............#....................................................................................................#.........
|
||||
.......................................#...............................................................#........#...........................
|
||||
.....#............................................#............#.............................#.......................#....................#.
|
||||
........................................................#...........................#.........................................#.............
|
||||
...........................................................................#................................................................
|
||||
.#...................#..............#.....................................................#................#................................
|
||||
..............#..........................#..........................#...........#...........................................................
|
||||
......#......................#.............................#..................................#........#............#.......................
|
||||
........................................................................#...................................................................
|
||||
.................................................#..........................................................................#...............
|
||||
.......................#........................................#...........................................................................
|
||||
....#......#.........................................#.............................................#..............................#.........
|
||||
....................................................................................#....................................................#..
|
||||
...........................#......#.....#...................#............#.................#.......................#........................
|
||||
........................................................................................................................#...................
|
||||
.......#...............................................................................#.................#..................................
|
|
@ -28,6 +28,7 @@ library
|
|||
Day08
|
||||
Day09
|
||||
Day10
|
||||
Day11
|
||||
Day15
|
||||
|
||||
other-modules:
|
||||
|
|
|
@ -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
|
||||
|
|
73
2023/haskell/solutions/Day11.hs
Normal file
73
2023/haskell/solutions/Day11.hs
Normal file
|
@ -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
|
||||
|
|
@ -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
|
||||
]
|
||||
|
||||
|
|
Loading…
Reference in a new issue