Thanks for using Compiler Explorer
Sponsors
Jakt
C++
Ada
Algol68
Analysis
Android Java
Android Kotlin
Assembly
C
C3
Carbon
C with Coccinelle
C++ with Coccinelle
C++ (Circle)
CIRCT
Clean
CMake
CMakeScript
COBOL
C++ for OpenCL
MLIR
Cppx
Cppx-Blue
Cppx-Gold
Cpp2-cppfront
Crystal
C#
CUDA C++
D
Dart
Elixir
Erlang
Fortran
F#
GLSL
Go
Haskell
HLSL
Hook
Hylo
IL
ispc
Java
Julia
Kotlin
LLVM IR
LLVM MIR
Modula-2
Mojo
Nim
Numba
Nix
Objective-C
Objective-C++
OCaml
Odin
OpenCL C
Pascal
Pony
PTX
Python
Racket
Raku
Ruby
Rust
Sail
Snowball
Scala
Slang
Solidity
Spice
SPIR-V
Swift
LLVM TableGen
Toit
TypeScript Native
V
Vala
Visual Basic
Vyper
WASM
Zig
Javascript
GIMPLE
Ygen
sway
haskell source #1
Output
Compile to binary object
Link to binary
Execute the code
Intel asm syntax
Demangle identifiers
Verbose demangling
Filters
Unused labels
Library functions
Directives
Comments
Horizontal whitespace
Debug intrinsics
Compiler
x86-64 ghc 8.0.2
x86-64 ghc 8.10.5
x86-64 ghc 8.4.1
x86-64 ghc 8.4.2
x86-64 ghc 8.4.3
x86-64 ghc 8.4.4
x86-64 ghc 8.6.1
x86-64 ghc 8.6.2
x86-64 ghc 9.0.1
x86-64 ghc 9.10.2
x86-64 ghc 9.12.2
x86-64 ghc 9.2.1
x86-64 ghc 9.2.2
x86-64 ghc 9.4.5
x86-64 ghc 9.6.1
x86-64 ghc 9.8.4
Options
Source code
{-| original: https://github.com/ephemient/aoc2022/blob/main/hs/src/Day14.hs Module: Day14 Description: <https://adventofcode.com/2022/day/14 Day 14: Regolith Reservoir> -} {-# LANGUAGE BlockArguments, OverloadedStrings #-} module Main (main) where import Control.Arrow ((&&&), (***)) import Control.Monad (forM_, when) import Control.Monad.ST (ST, runST) import Data.Array.MArray (MArray, newArray, readArray, writeArray) import Data.Array.ST (STUArray) import Data.Bool (bool) import Data.Ix (Ix) import Data.Maybe (fromMaybe) import Data.STRef (modifySTRef', newSTRef, readSTRef, writeSTRef) import Data.Semigroup (Max(Max), Min(Min)) import Data.Text (Text) import qualified Data.Text as T (lines, splitOn) import qualified Data.Text.Read as T (decimal) import Data.Foldable (toList) import Data.Text (Text) import qualified Data.Text as T (null) import Data.Text.Read (Reader) count :: (Foldable t) => (a -> Bool) -> t a -> Int count p = length . filter p . toList readEntire :: Reader a -> Text -> Either String a readEntire reader input = do (a, t) <- reader input if T.null t then Right a else Left "incomplete read" parse :: (Bounded i, Integral i, Ix i, MArray a Bool (ST s)) => Text -> ST s (a (i, i) Bool, i) parse input = do blocks <- newArray ( (min minX $ 500 - maxY - 1, min 0 minY) , (max maxX $ 500 + maxY + 1, max 0 maxY + 1) ) False forM_ segments $ \points -> forM_ (zip points $ tail points) $ \((x1, y1), (x2, y2)) -> forM_ [min y1 y2..max y1 y2] $ \y -> forM_ [min x1 x2..max x1 x2] $ \x -> writeArray blocks (x, y) True pure (blocks, maxY) where segments = [ [ (x', y') | [x, y] <- T.splitOn "," <$> T.splitOn " -> " line , x' <- either fail pure $ readEntire T.decimal x , y' <- either fail pure $ readEntire T.decimal y ] | line <- T.lines input ] ((Min minX, Max maxX), (Min minY, Max maxY)) = foldMap ((Min &&& Max) *** (Min &&& Max)) $ concat segments fill :: (MArray a Bool (ST s), Ix i, Num i, Show i) => a (i, i) Bool -> i -> ST s (Int, Int) fill blocks maxY = do counterAtMaxY <- newSTRef Nothing counter <- newSTRef 0 let fill' (x, y) = readArray blocks (x, y) >>= flip bool (pure ()) do when (y == maxY) $ readSTRef counterAtMaxY >>= maybe (readSTRef counter >>= writeSTRef counterAtMaxY . Just) (const $ pure ()) when (y <= maxY) $ fill' (x, y + 1) >> fill' (x - 1, y + 1) >> fill' (x + 1, y + 1) writeArray blocks (x, y) True >> modifySTRef' counter (+ 1) fill' (500, 0) counterAtMaxY <- readSTRef counterAtMaxY counter <- readSTRef counter pure (fromMaybe counter counterAtMaxY, counter) day14 :: Text -> (Int, Int) day14 input = runST $ parsed >>= uncurry fill where parsed :: ST s (STUArray s (Int, Int) Bool, Int) parsed = parse input main = print $ day14 "498,4 -> 498,6 -> 496,6\n503,4 -> 502,4 -> 502,9 -> 494,9"
Become a Patron
Sponsor on GitHub
Donate via PayPal
Source on GitHub
Mailing list
Installed libraries
Wiki
Report an issue
How it works
Contact the author
CE on Mastodon
CE on Bluesky
About the author
Statistics
Changelog
Version tree