~rootmos/AoC

643de9693cfc1c9c7169f2e559afed9fe1bbbb46 — Gustav Behm 11 days ago 4051ab6
Add solution to AoC 2023 #14a
3 files changed, 195 insertions(+), 0 deletions(-)

A 2023/14.example
A 2023/14.hs
A 2023/14.input
A 2023/14.example => 2023/14.example +10 -0
@@ 0,0 1,10 @@
O....#....
O.OO#....#
.....##...
OO.#O....O
.O.....O#.
O.#..O.#.#
..O..#O..O
.......O..
#....###..
#OO..#....

A 2023/14.hs => 2023/14.hs +85 -0
@@ 0,0 1,85 @@
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE NoImplicitPrelude #-}
module Main where

import Qulude

--import Data.Array ( (!) )
import qualified Data.Array as Array
import qualified Data.Array.ST as STArray
import qualified Data.Either as Either
import qualified Data.List as List
--import Data.Map ( (!) )
import qualified Data.Map as Map
import qualified Data.Maybe as Maybe
import qualified Data.Sequence as Seq
import qualified Data.Set as Set

data Rock = Round | Cube
  deriving ( Show, Eq )

type Pos = (Int,Int)
type Bounds = (Pos, Pos)

data Grid a = Grid (Map.Map Pos a) Bounds
  deriving ( Show, Eq )

parseGrid :: Parser (Maybe a) -> Parser (Grid a)
parseGrid p = do
  raw <- rawP
  let maxY = maximum $ fmap (\(y,_) -> y) raw
      maxX = maximum $ fmap (\(_, xs) -> maximum $ fmap (\(x, _) -> x) xs) raw
  return $ Grid (build raw) ((1,1), (maxX,maxY))
  where rawP = fmap (zip [1..]) $ many1 $ zip [1..] <$> p `manyTill` newline
        build = Map.fromList . concat . fmap f
        f (y, xs) = catMaybes $ fmap (g y) xs
        g y (_, Nothing) = Nothing
        g y (x, Just a) = Just ((x,y), a)

lookupGrid :: Grid a -> Pos -> Maybe a
lookupGrid (Grid g _) p = Map.lookup p g

inBounds :: Grid a -> Pos -> Bool
inBounds (Grid _ ((minX, minY),(maxX, maxY))) (x,y) =
  (minX <= x) && (x <= maxX) && (minY <= y) && (y <= maxY)

bounds :: Grid a -> (Pos, Pos)
bounds (Grid _ b) = b

type Parse = Grid Rock
parser :: Parser Parse
parser = parseGrid rock
  where rock = char '.' $> Nothing
           <|> char '#' $> Just Cube
           <|> char 'O' $> Just Round

tiltNorth :: Grid Rock -> Pos -> Int
tiltNorth g (x,y) =
  let (_, (_, mY)) = bounds g in
  mY - f 0 (pred y)
  where f acc 0 = acc
        f acc y = case lookupGrid g (x,y) of
                    Nothing -> f acc (pred y)
                    Just Cube -> y + acc
                    Just Round -> f (succ acc) (pred y)

roundRocks :: Grid Rock -> [Pos]
roundRocks (Grid rs _) = Map.foldlWithKey f [] rs
  where f acc p Round = p:acc
        f acc _ Cube = acc

partA :: Parse -> Int
partA g = sum $ fmap (tiltNorth g) $ roundRocks g

--partB :: Parse -> Parse
--partB = id

main :: IO ()
main = with parser $ do
  run' "14.example" "a example" partA 136
  run' "14.input" "a input" partA 106378
  --run "14.example" "b example" partB
  --run "14.input" "b input" partB

A 2023/14.input => 2023/14.input +100 -0
@@ 0,0 1,100 @@
O....##.##.....OO...O.O......#.OO.#.#....#.#..#..##....###..O.....O#..#.#.......O.#.##...O#OO.O....O
.#..OOO..#.#..O..O...O.#.......O..O....##..O##O..#..#.......O.O.#....O...O#.O#OOO...O...#O.#......O.
..#O...#O.......##.O.O..O#..OO..O...#.O...O.#...#.OOO.OO.O......#.....#.O..##.O.#.#...O.O#..#..#O.#.
O####.O.#.O...#..#...O.......#....#..O....OO#...O..##...#..##..O..####..OO.O..O.#OO..O.O..O##O.....#
OO..O.##..#.O##O.....#..#O#.O...#.......#O#...#.O....#..O.O.....O#.OO....O......O#O.#..O..OO.O.O....
..#O.O...O...O..O.OO.O#OO...#......#.....O.#.....#.....O##.#......#O..OO.......#..#..O...#.......O.O
.O...#.......OO..O.....#....#.#.O.........#....#......#.OO...O.........O.O.O....O..#..#O..##...#.O..
O#......#.O....#.O.#....#....O.......#O.O#O..O..#...#.O.#..O.OO...O..O....#O#OOOO................#..
O..O.#..#........O......O..O..#..O.#.O...#O...O##..O.....#OO.#..#...........##.#...O##.O...#..#..O..
..O.....OO.O#..#O#.....#......OOOO#..#.O.#.#..O..O.....O.O.#....#O...O...O.OO......#O#.#.##......O.O
.#.#............OO...O.OOO#O..#.#O#.O.#O#.O.O.....#.O..........#.....O#..O...O#.....O......##O..#OO.
.O#O....O#..#....OO.....#...O...O..O.O..O..##....#.#.....O....O..#O..........#O...#O##.#....O..##O..
....OOO.O..O#..#.OO.....O#..O.O#...#.....##OO.....OO...##O.....O......O#..O##...#...##.O..O....#.#..
.#......O.#..O...........O...#..O#.O.#O#..OO.........O.O...O##.........##.O#.........#.O......#...#.
O.#...##..#..........#OO........O#..##..##.#O#O..O..#O..O....O.OO....O...OOO...O.....#.OO#..#.#.....
.##.#O.##..#....O.##O..O.#..O...#...O....O#....O...O.#O#...........##O..O..#.O....O........O.#...OOO
.O#......OOOO...#..O....#..#.O#.#.##.##O#.#.#..O....O#..........O..#..O...#.....O........#....O.#..#
.....O......#..#...O....OO...O#OO...OO.O....#..O.O.....O#OO.#....#.#.#......O......OO........#.#.##.
......O......O...O##.#O........O.......#.##.O......O.#.#.#.....#.O....#..#O...O##..#..#.##...O#O.O.O
..OO.O...#..OO#.OO..O......##......O.#...#....O.OO..O...#.......#O##..OO....OO.O..#.O...#O..#.#O....
O...O.#.O..#.OOO...........O..#.#....OO..OO.O....O#O#O#..OOOO....OOO.OOO#....#....O#..O#.O.#....O...
.......O.....O..O.#...O.#...#O.O..#......#...O#OOOO#..OOO..#..O....#...O..O..O.#....O.O.O.O...#O....
#...O.#.........OO...#......OO....O.##OOO#O.......#.O..#..O.#O#.O...O.OOO.O##OO.#.O.O..#.....O...O#.
O.OOOOO##O#.......O#.....O...O.#...O..OOO.....##...O...O....O.#....##O....#.#OO..#..#O..OO#OO#......
...........O...#.O....O##O..O.O....#.#O...O..OO.......#..#.#...#.........##..#.O...#.O...OOO..O.#OO.
..O##....#....O.#.O#O##.OOOOO..O.#..#...O..#...#.##.#O.....#..O..O.O#...#.O..#....#O.#........O#...#
#...O.......#.O....O......OO#.##..##O..O..O....O.O##.OOO......#......#...OO..O.O..........OO.O.O..O.
........OO.O.#.#.O...............O......O...O....#........O..O....O..O.....O.O..O.O.O#O#.O..##......
O..O.......#O..O..O.O....O......O##.#...O##..#...O.O#.#.O..O.O#.#.....O...O#.#.#.....O.....#.#......
.#......O..O..#O....O...O...O.......#..#..#.O...##..#......O.O.OOOO...##..#.OO......#O.O.O#...O.##O#
..OOO...#.O.O..#.#..O....##.O....#.O..OO#.O.......O..##O##.#OO.....#.....#.....O.#O.OO#....#O..OO.O.
..O#OO#.#....O.#..O.#OO#.#...O.#.#.OO....O..O.#...#..O..OO.#.O#..##.#......#O.#.#..O..O...OO#..OOO..
#.#OO.....O...O......O............O...###..O.#.OO...O...#...O.....##OO#.......#...#.OO.#...#....#...
....O....#.#OO##OO...OO...#..#.#.....O.....O..O....#......O....O#....OO.#....O....O...#.O..#OOO.OOOO
.#..O.O.O.O.#.O##O..#..O...........O..........OO#.#..O...#O....#OO...O.#.....O.OOOO....O.......#O.#.
...O##....O.....#O..#.OO........#...O..#O#..OO#........###.O..O......O..#...##.#..#..#O.....O.#..#.#
.#..O.....O...OO#OO..#......O.#....#..#.#.....O.....OO.O...#.....###...O....O.......OO#..#....O.#..O
O...#.O..O....#.O....#...#.O....#.O..##.O.O#.O#O.O.....O.#OO#.#O......#.O..#.#..#........O.O...O..O#
......O.OOOO.O....O.#.##.O...#O....OOO...O..O###..OOO.........OO.O..O...OO.##..#.#..##.O.#...#....#.
.#O...#.##.O....#.#...##O....O#O.O.#.O.#.O...#...O.#.O#O....O.#..###.O.#OOOO.O#OO.....OOO#.....##..O
..O##O.#.#..O...#O..O.##.#O..O.#..OO.O..........O.....O#.#.OOO........#O.O#.#..O..OOO....#..O.O.#..O
OO.#O....OO#........O.O.#..#..O#.O.OO#O...O.#.O........O##.OO..O..#..O........O#.#O.............O...
................O#...........O........O#.O..O#..#O.O.O..#.....#.#O..O.O.#.....#.O.....#...#.#O..#..#
#....#...#....#.#O......##O.....#.#........#.O....OO#..OO.#....OO..#....O....OO..#..##......#O.O.#..
O####O.##....####O..#.#.OO.O..#O#O...O.OOO...#O##..##......O...O..OO..#.O#O...O.#O........O.....#..#
...OOO.O.....O..#.O.....O.OO...OOO#O.........O...O..#...#OO.....#..O....#....#.......##.....#...#.O#
#.....O#..O#.#.....#..O......#..O.......O##.....##.##O..OO##.OO#..##......O......O....O....O#....O..
O....OO......O.....O...O#.O..#.#...#....#..#....OO.O##O..##O..O#.O.#.OO#.OOO....#.OO..#O..O..#..##..
O.O#.#OO..O..O...OO......OO.......#.O#O.O#....#.O#O..O#.#...O#..O...O....O.#OO#.#..O.##..O.....#..OO
.##.O...O.O..O....OO...O#....OO..#.....#.....OO##.#.O.#......#.O...O..O...##..#O.....#.O....#O......
.OO#..O.O.....#.O.OO.##..#.....O......O..OO...O.O.O##OO.#O...O..O..O#...OO..O.....#.............O.OO
..O....#O..##.O..#...O#..#.OO....##.O.....#..##.O...#.O...O.#O.OO..#O...O##.O.......#.O.#.O..#O.....
.##..........O..#.#.........O....O..O#..##.#..#..O#OO..#..#O...O#O.#..#.#.O.OO......O###.....O.O....
...#O..O..O#..O#..O#...OO.O.O.#.OO..#.#..#.#.......O.O..#OO#..O....O...#O#..#O....O.#O.#..#.#..O...#
OO###....OO..#.##OO......#..O###..O.....O.O.#...OO..#O....OO..#O..#.#..OO.#.....O......OOO........OO
.O....#..#.O.O........O..#....O...O...O#.....OOOO.####.O..#.##.##O##..O.OO...O.OO..#O...O..O.O.#.O..
##O#...#...O.O#..O..O.#.OOO..O..##....#....##.O.#.O....O.#....O##...OO..O..#..O.O.#.....#OOO.O..O...
.O.....#....O#.O......#O...#.#.#...O.#..O.O..#...O.#O..O..O......#...........O##..O#O.#.#.#.........
OO.....O..#.O#.....##O..##....OO....O..O.O..#..O.O........O...#...O....O#O.O..#.O..##.#...#.....###.
.#..........O.....#O#O....O.OO.#..#...#..O.#...O.#.#.O#O....#O.O..#.....##O#.#.O..#O....O.##...#.O.O
.O....O....O##.O...O....O....O....O..O............#O...#.#...OOO....O#.....O#...##.......O.#........
.#.......OOO#............OO..........O.#...O..OO....#..O.#...##.O#....O..O.#.O#O##.O.O...#.#O#.O.O..
.#..#.#...........O.#..##..OO#.O..O..O.#OO##O.O..OOO....OO..#OO..#..###..O....##...........#O.###.O.
O....O..O...#........O.#OOOOO#.O.....O.#...O.OOO#.#......#.OO#.#.O..#..#O....##....#.###...#.###..#.
..O...O#.O.....#.OOO.O....O.......#O...O..#.#......#.#..O......OOO.#..O.#O..O..O#..#..#...#....O.#.O
O....O...OO.....#.....O#..#..##...O..O.....##....#O.....##.#..O#.O...#O.....O##.##O.....#O..O..O...#
O.##.OO.O.O.O...O..O..O...O.O...O.#..O...###....O..O##O#....OO.......#....#OO....#.#.......O....OO#O
.O......OO.....##...##.#..#..........##O..#.O#.#....#...#.OO..O.....#...O.......##.OO##....O..OO....
O....O#.....#.OO..O#..OO.OO#..O...O.O.OO.O.O.#...O.#OO..O#.....#.#.....#..O....O...........O#.#.#...
#..#.#..#.....#O.O.#####........O..#......#O......##.........#....#.#O.#.O...O.#..O.O..#O.#O..OO..#O
...O..O...O.OOOO.O#O###..O...#..#.......O#......O..O#O.OO.#..#.O.O...O....O.....###...#......##....O
..#.#O........O#O..##OOO....O##.#..O...#.O.O##..#OO.....#OO.#......O...#...#.O.#.O#.....O...O...##.#
O.....OO....OO#..O.....O....O..#.#.......OO#O........##.O..O#O.#.##.O..O.#.#.#......#O.##....O.#OO.O
..O..OO..O..O#....O...#....OO#O.......O....O..##O.O.O#..#O#....#..O.....#O.#.O#....##....O......#...
......#O#..O#..OO#...#......#.#.......O#..#.O#.O.O.OO...#..#O..##..#O..O...#..#.O....##O.O##O..O.#.O
..#O...#...O.........OO........O.O.O#......OO.#.....O.O..O#.#O#.....####.OO....#O#OO..#O.O..O##O...#
.#.O..#...O.#.O.OO###.#.OO#..O.#OO.#.#.#....O...#O#..OOOO#.......#.O...OO.....O#.#...##.............
.#.#.OO....O.........O..##O.#.O........O.#O.O##...O....O.O.#OOO....OO.O..O.OO...O..#OO...#......#..#
......O.....O..O...#...#......O..#O#...#...#...O...OO.........O..O.O.OOO.O.#.O.O....OO.O.#O..O...O..
........#....OO......#.....#O#...#..O.O..#..O..O.##O.O..........#...#.O...O#.O..........OOOO.O....O.
O..#.O.O....OO...#.O.......O....#.O#...O#.......OO..#O###O#O.O.O.#...#...#O#.O..O...OOO.OOO.#.#.....
.#.OO......O#.#...OO.#..O....O#O.O#..........O......#.#.O....O...O.#O.#OO........#..#...##..#....OOO
O.....#....O......O......OO#O#.##.##OO..O............O.OO...O#.....#...##...#...OO.OO.......O.....O.
.....OO.O......O.........O.#.O.....#.#O......O..O..#.#.##.........O##OO....O..OO...#........#...##..
#..###......O.....O#OO...#..#O....O.OO..O...........#..#.#...O##O...O.O....#.OO..#.#.OO....#..#O.O.#
.........O#.O#...##.#.#...O#.O.........O.#..OO........O.O.....#O....O#.#..O.O..#....#.....#.......O.
...#O...O.#...OOOO.#O..O.O#.....#.#.....O..#.....O.#.O......O.##O.O..O........#.........#O..O#..#...
#.#.#O#.O..#O.#O..#.##..O#.O..#.O.OOO...........O..O.O.#O..#....O....#O.##O.##O......O..OOO...#O...O
O.#O##.....#.O.O.OO.O..O....O...#O...........O.##O##...OO...##.#.#.#.#.O..O...#......O...O.#..#.#.O.
.OO....O.....#O.O........O#.......O...O.O##O..#....O.#O.#.O.....##..#...#.OO.O.O..##...#O.OOO#O..#.#
##O#OOO.##...#.O...#....#...O#.....O....#.OOO.O.##..OO.OO...O.O.O..O.O..#.#.#.##...O...#..#.#.O.....
..O#..#OOO#....O...#.O.#...#....#...#....O..........O.O#.....O.....O.OO...O.#.......O.#...O.........
#..#.#.O#.OO.O...O..#....#.O..OO.....#..O...O.#.#...OO....O#..O....###...###..O.O.O.O.O..OOO.....OO.
#..#..#.O..#..O.O..O#....##O.OO.O#OO.......O........#..O..#.O..#.#OOOO...O..#OOO....#..#..OOO......O
.#O.#.#.#...#OOO.OO..#O.##.....O..#O.##.O.O.O..OOO#....O.##......OOOOOO...O..O......#...#.O...O.#...
#O..O.O###...##O...O........##.O#....#O..O.O.O#.#OO##O.O.O#.#.##O.OO........O.........###O.O.#O#OO#O
O..#O......#.#O...O#...#####..#O#...O.##OO......O.....O.......O.O.#..O.O...........#OO..#..OOO....#.
....O.O#..#O#.#....#O.O##..#...O...#O.#.#.##O...OO....O..#..OO#........#O.........#O..O.#..OO.......
#..##.#.O#O..#O..OO...#.....#..O#..O....O#.....OO.O......O..#....O.##O#O...O....#..O.......O....O.##
#....#O..#O.O..#....OO#.#OO...O..O.......O..#......#.......#.O.#..OO..O........OO.......#O.O.#O....O