• # Solution en Haskell

    Posté par . En réponse au message Advent of Code 2023, jour 11. Évalué à 2.

    Hello tout le monde, c'est mon premier post sur linuxfr (même si je fréquente le site depuis longtemps).

    Voici une solution en Haskell (pour ne pas faire comme tout le monde et accessoirement c'est mon langage préféré).
    Pas de commentaire à faire la dessus. C'était un problème assez simple.

    type Coord = (Int, Int)
    type Grid = [[Bool]]
    parser :: Parser Grid
    parser = some isGalaxy `sepEndBy1` eol where
     isGalaxy = False <$ "." <|> True <$ "#"
    manhattan :: Coord -> Coord -> Int
    manhattan (x1, y1) (x2, y2) = abs (x1 - x2) + abs (y1 - y2)
    gridToCoords :: Grid -> [Coord]
    gridToCoords grid = [(i, j) | (i, row) <- zip [0..] grid, (j, True) <- zip [0..] row] 
    countEmptyRows :: Grid -> Vector Int
    countEmptyRows = V.fromList . drop 1 . scanl' (\acc row -> if all not row then acc+1 else acc) 0
    solveWith :: Int -> Grid -> Int
    solveWith expand grid = sum (manhattan <$> expGalaxies <*> expGalaxies) `div` 2 where
     galaxies = gridToCoords grid
     emptyRows = countEmptyRows grid
     emptyCols = countEmptyRows (transpose grid)
     expGalaxies = [(x + (expand - 1) * emptyRows V.! x, y + (expand - 1) * emptyCols V.! y) | (x, y) <- galaxies]
    solve :: Text -> IO ()
    solve = aoc parser (solveWith 2) (solveWith 1000000)