sln_2022_14
Copyright(c) Eric Mertens 2022
LicenseISC
Maintaineremertens@gmail.com
Safe HaskellNone
LanguageHaskell2010

Main

Description

https://adventofcode.com/2022/day/14

>>> :set -XQuasiQuotes
>>> let input = [format|- ((%u,%u)&( -> )%n)*|] "498,4 -> 498,6 -> 496,6\n503,4 -> 502,4 -> 502,9 -> 494,9\n"
>>> let world = Set.fromList (concatMap segs input)
>>> let limit = voidLimit world
>>> let Left world1 = fillFrom Left limit world top
>>> let picture = Data.Map.fromSet (const '█') world <> Data.Map.fromSet (const '◆') world1
>>> putStr (Advent.Coord.drawPicture picture)
······◆···
·····◆◆◆··
····█◆◆◆██
···◆█◆◆◆█·
··███◆◆◆█·
····◆◆◆◆█·
·◆·◆◆◆◆◆█·
█████████·
>>> Set.size world1 - Set.size world
24
>>> let Identity world2 = fillFrom Identity limit world top
>>> let picture = Data.Map.fromSet (const '█') world <> Data.Map.fromSet (const '◆') world2
>>> putStr (Advent.Coord.drawPicture picture)
··········◆··········
·········◆◆◆·········
········◆◆◆◆◆········
·······◆◆◆◆◆◆◆·······
······◆◆█◆◆◆██◆······
·····◆◆◆█◆◆◆█◆◆◆·····
····◆◆███◆◆◆█◆◆◆◆····
···◆◆◆◆·◆◆◆◆█◆◆◆◆◆···
··◆◆◆◆◆◆◆◆◆◆█◆◆◆◆◆◆··
·◆◆◆█████████◆◆◆◆◆◆◆·
◆◆◆◆◆·······◆◆◆◆◆◆◆◆◆
>>> Set.size world2 - Set.size world
93
Synopsis

Documentation

main :: IO () Source #

>>> :main
644
27324

top :: Coord Source #

The entry point of sand at 500,0

voidLimit :: Set Coord -> Int Source #

Find the level beyond any walls.

fillFrom Source #

Arguments

:: Monad m 
=> (Set Coord -> m (Set Coord))

behavior when sand reaches limit

-> Int

lower limit

-> Set Coord

initial wall and sand locations

-> Coord

location to fill from

-> m (Set Coord)

final wall and sand locations

Fill the given world with sand from a fill coordinate returning the final state of the world. This is parameterized over a callback for how to handle when sand reaches the bottom of the level in order to allow early termination or not.

segs :: [(Int, Int)] -> [Coord] Source #

seg :: (Int, Int) -> (Int, Int) -> [Coord] Source #