From 01cdae0f7a7a5e15850f31a51d9c905054d02afe Mon Sep 17 00:00:00 2001 From: Stefan Risberg Date: Sat, 3 Dec 2022 10:50:09 +0100 Subject: [PATCH] Initial release --- .ghci | 4 ++ .gitignore | 3 + LICENSE | 7 ++ Makefile | 34 +++++++++ README.md | 1 + Setup.hs | 2 + aoc2022.cabal | 178 ++++++++++++++++++++++++++++++++++++++++++++++++ app/Main.hs | 8 +++ fourmolu.yaml | 9 +++ package.yaml | 99 +++++++++++++++++++++++++++ src/Day2.hs | 73 ++++++++++++++++++++ src/Day3.hs | 33 +++++++++ src/Lib.hs | 76 +++++++++++++++++++++ stack.yaml | 66 ++++++++++++++++++ stack.yaml.lock | 12 ++++ test/Spec.hs | 9 +++ 16 files changed, 614 insertions(+) create mode 100644 .ghci create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 Makefile create mode 100644 README.md create mode 100644 Setup.hs create mode 100644 aoc2022.cabal create mode 100644 app/Main.hs create mode 100644 fourmolu.yaml create mode 100644 package.yaml create mode 100644 src/Day2.hs create mode 100644 src/Day3.hs create mode 100644 src/Lib.hs create mode 100644 stack.yaml create mode 100644 stack.yaml.lock create mode 100644 test/Spec.hs diff --git a/.ghci b/.ghci new file mode 100644 index 0000000..0bdc7f3 --- /dev/null +++ b/.ghci @@ -0,0 +1,4 @@ +:set -fwarn-unused-binds -fwarn-unused-imports +:set -isrc +:set -itest +:load Spec diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9a20c30 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.stack-work/ +*~ +*.txt diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..d150660 --- /dev/null +++ b/LICENSE @@ -0,0 +1,7 @@ +Copyright 2022 Stefan Risberg + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..2403ab0 --- /dev/null +++ b/Makefile @@ -0,0 +1,34 @@ +build: + stack haddock --haddock-deps + stack build + +ghcid: + stack exec -- ghcid --test ":main" + +build-profile: + stack build --profile + +run: + stack run + +run-profile: + stack --profile run -- +RTS -p + +run-memprof: + stack run -- +RTS -s + + +hoogle-generate: + stack hoogle -- generate --local + +hoogle-serve: + stack hoogle -- serve --local + +setup: + stack build ghcid + +# bench: +# stack --profile bench --benchmark-arguments "--output test1.html" +# profiteur CsvHaskellTest-bench.prof +# xdg-open test1.html +# xdg-open CsvHaskellTest-bench.prof.html diff --git a/README.md b/README.md new file mode 100644 index 0000000..b9a022c --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +# aoc2022 diff --git a/Setup.hs b/Setup.hs new file mode 100644 index 0000000..9a994af --- /dev/null +++ b/Setup.hs @@ -0,0 +1,2 @@ +import Distribution.Simple +main = defaultMain diff --git a/aoc2022.cabal b/aoc2022.cabal new file mode 100644 index 0000000..17be039 --- /dev/null +++ b/aoc2022.cabal @@ -0,0 +1,178 @@ +cabal-version: 1.12 + +-- This file has been generated from package.yaml by hpack version 0.35.0. +-- +-- see: https://github.com/sol/hpack + +name: aoc2022 +version: 0.1.0.0 +description: Please see the README on GitHub at +homepage: https://github.com/githubuser/aoc2022#readme +bug-reports: https://github.com/githubuser/aoc2022/issues +author: Stefan Risberg +maintainer: steffenomak@gmail.com +copyright: 2022 Stefan Risberg +license: BSD3 +license-file: LICENSE +build-type: Simple +extra-source-files: + README.md + CHANGELOG.md + +source-repository head + type: git + location: https://github.com/githubuser/aoc2022 + +library + exposed-modules: + Annans + Day2 + Day3 + Lib + other-modules: + Paths_aoc2022 + hs-source-dirs: + src + default-extensions: + AllowAmbiguousTypes + BangPatterns + BlockArguments + DataKinds + DeriveGeneric + DuplicateRecordFields + FlexibleContexts + FlexibleInstances + GADTs + GeneralizedNewtypeDeriving + LambdaCase + MultiParamTypeClasses + MultiWayIf + NoImplicitPrelude + NoMonomorphismRestriction + OverloadedLabels + OverloadedStrings + PolyKinds + RankNTypes + ScopedTypeVariables + TemplateHaskell + TupleSections + TypeApplications + TypeFamilies + TypeOperators + UndecidableInstances + ghc-options: -Wall -Wcompat -Widentities -Wincomplete-record-updates -Wincomplete-uni-patterns -Wmissing-export-lists -Wmissing-home-modules -Wpartial-fields -Wredundant-constraints -O3 + build-depends: + attoparsec + , base >=4.7 && <5 + , bytestring + , bytestring-mmap + , classy-prelude + , containers + , optics + , split + , streamly + , streamly-bytestring + , text + , unordered-containers + default-language: Haskell2010 + +executable aoc2022-exe + main-is: Main.hs + other-modules: + Paths_aoc2022 + hs-source-dirs: + app + default-extensions: + AllowAmbiguousTypes + BangPatterns + BlockArguments + DataKinds + DeriveGeneric + DuplicateRecordFields + FlexibleContexts + FlexibleInstances + GADTs + GeneralizedNewtypeDeriving + LambdaCase + MultiParamTypeClasses + MultiWayIf + NoImplicitPrelude + NoMonomorphismRestriction + OverloadedLabels + OverloadedStrings + PolyKinds + RankNTypes + ScopedTypeVariables + TemplateHaskell + TupleSections + TypeApplications + TypeFamilies + TypeOperators + UndecidableInstances + ghc-options: -Wall -Wcompat -Widentities -Wincomplete-record-updates -Wincomplete-uni-patterns -Wmissing-export-lists -Wmissing-home-modules -Wpartial-fields -Wredundant-constraints -O3 -threaded -rtsopts -with-rtsopts=-N + build-depends: + aoc2022 + , attoparsec + , base >=4.7 && <5 + , bytestring + , bytestring-mmap + , classy-prelude + , containers + , optics + , split + , streamly + , streamly-bytestring + , text + , unordered-containers + default-language: Haskell2010 + +test-suite aoc2022-test + type: exitcode-stdio-1.0 + main-is: Spec.hs + other-modules: + Paths_aoc2022 + hs-source-dirs: + test + default-extensions: + AllowAmbiguousTypes + BangPatterns + BlockArguments + DataKinds + DeriveGeneric + DuplicateRecordFields + FlexibleContexts + FlexibleInstances + GADTs + GeneralizedNewtypeDeriving + LambdaCase + MultiParamTypeClasses + MultiWayIf + NoImplicitPrelude + NoMonomorphismRestriction + OverloadedLabels + OverloadedStrings + PolyKinds + RankNTypes + ScopedTypeVariables + TemplateHaskell + TupleSections + TypeApplications + TypeFamilies + TypeOperators + UndecidableInstances + ghc-options: -Wall -Wcompat -Widentities -Wincomplete-record-updates -Wincomplete-uni-patterns -Wmissing-export-lists -Wmissing-home-modules -Wpartial-fields -Wredundant-constraints -O3 -threaded -rtsopts -with-rtsopts=-N + build-depends: + aoc2022 + , attoparsec + , base >=4.7 && <5 + , bytestring + , bytestring-mmap + , classy-prelude + , containers + , optics + , split + , streamly + , streamly-bytestring + , text + , unordered-containers + default-language: Haskell2010 diff --git a/app/Main.hs b/app/Main.hs new file mode 100644 index 0000000..559cb15 --- /dev/null +++ b/app/Main.hs @@ -0,0 +1,8 @@ +module Main (main) where + +import ClassyPrelude +--import Lib +import Day2 + +main :: IO () +main = fun diff --git a/fourmolu.yaml b/fourmolu.yaml new file mode 100644 index 0000000..755202b --- /dev/null +++ b/fourmolu.yaml @@ -0,0 +1,9 @@ +indentation: 2 +comma-style: leading +import-export-style: diff-friendly +indent-wheres: false +record-brace-space: false +respectful: true +haddock-style: multi-line +newlines-between-decls: 1 +fixities: [] diff --git a/package.yaml b/package.yaml new file mode 100644 index 0000000..00702bf --- /dev/null +++ b/package.yaml @@ -0,0 +1,99 @@ +name: aoc2022 +version: 0.1.0.0 +github: "githubuser/aoc2022" +license: BSD3 +author: "Stefan Risberg" +maintainer: "steffenomak@gmail.com" +copyright: "2022 Stefan Risberg" + +extra-source-files: +- README.md +- CHANGELOG.md + +# Metadata used when publishing your package +# synopsis: Short description of your package +# category: Web + +# To avoid duplicated efforts in documentation and dealing with the +# complications of embedding Haddock markup inside cabal files, it is +# common to point users to the README.md file. +description: Please see the README on GitHub at + +dependencies: +- base >= 4.7 && < 5 +- classy-prelude +- streamly +- streamly-bytestring +- optics +- containers +- unordered-containers +- bytestring +- text +- attoparsec +- split +- bytestring-mmap + +ghc-options: +- -Wall +- -Wcompat +- -Widentities +- -Wincomplete-record-updates +- -Wincomplete-uni-patterns +- -Wmissing-export-lists +- -Wmissing-home-modules +- -Wpartial-fields +- -Wredundant-constraints +- -O3 + +library: + source-dirs: src + +executables: + aoc2022-exe: + main: Main.hs + source-dirs: app + ghc-options: + - -threaded + - -rtsopts + - -with-rtsopts=-N + dependencies: + - aoc2022 + +tests: + aoc2022-test: + main: Spec.hs + source-dirs: test + ghc-options: + - -threaded + - -rtsopts + - -with-rtsopts=-N + dependencies: + - aoc2022 + +default-extensions: + - AllowAmbiguousTypes + - BangPatterns + - BlockArguments + - DataKinds + - DeriveGeneric + - DuplicateRecordFields + - FlexibleContexts + - FlexibleInstances + - GADTs + - GeneralizedNewtypeDeriving + - LambdaCase + - MultiParamTypeClasses + - MultiWayIf + - NoImplicitPrelude + - NoMonomorphismRestriction + - OverloadedLabels + - OverloadedStrings + - PolyKinds + - RankNTypes + - ScopedTypeVariables + - TemplateHaskell + - TupleSections + - TypeApplications + - TypeFamilies + - TypeOperators + - UndecidableInstances diff --git a/src/Day2.hs b/src/Day2.hs new file mode 100644 index 0000000..39e7090 --- /dev/null +++ b/src/Day2.hs @@ -0,0 +1,73 @@ +module Day2 (fun) where + +import ClassyPrelude + +data Hand = Rock | Paper | Scissors + deriving (Eq) + +parseHand :: Char -> Hand +parseHand h | h == 'X' || h == 'A' = Rock + | h == 'Y' || h == 'B' = Paper + | otherwise = Scissors + +points :: Hand -> Int +points = \case + Rock -> 1 + Paper -> 2 + Scissors -> 3 + +data Result = Lost | Draw | Won + deriving (Eq) + +resultPoints :: Result -> Int +resultPoints = \case + Lost -> 0 + Draw -> 3 + Won -> 6 + +beats :: Hand -> Hand -> Bool +beats Rock Scissors = True +beats Paper Rock = True +beats Scissors Paper = True +beats _ _ = False + +play :: Hand -> Hand -> Result +play a b = + if | a == b -> Draw + | beats a b -> Lost + | otherwise -> Won + +runLineProb1 :: Text -> Int +runLineProb1 l = + let a = parseHand $ headEx l + b = parseHand $ lastEx l + handPoint = points b + gamePoint = resultPoints $ play a b + in handPoint + gamePoint + +incHand :: Hand -> Hand +incHand Rock = Paper +incHand Paper = Scissors +incHand Scissors = Rock + +decideHand :: Char -> Hand -> Hand +decideHand 'X' = incHand . incHand +decideHand 'Y' = id +decideHand 'Z' = incHand +decideHand _ = id + +runLineProb2 :: Text -> Int +runLineProb2 l = + let a = parseHand $ headEx l + b = decideHand (lastEx l) a + handPoint = points b + gamePoint = resultPoints $ play a b + in handPoint + gamePoint + +fun :: IO () +fun = do + readFileUtf8 "puzzle2.txt" + >>= \res -> putStrLn $ "Day2.2: " <> tshow (sum . map runLineProb1 . lines $ res) + + readFileUtf8 "puzzle2.txt" + >>= \res -> putStrLn $ "Day2.2: " <> tshow (sum . map runLineProb2 . lines $ res) diff --git a/src/Day3.hs b/src/Day3.hs new file mode 100644 index 0000000..4f75df1 --- /dev/null +++ b/src/Day3.hs @@ -0,0 +1,33 @@ +module Day3 (run) where + +import ClassyPrelude + +import Data.List.Split (chunksOf) + +priority :: Map Char Int +priority = mapFromList . zip (['a' .. 'z'] ++ ['A' .. 'Z']) $ [1 ..] + +prob1 :: Text -> Int +prob1 dat = + let (a, b) = splitAt (div (length dat) 2) dat + in fromMaybe + 0 + ( flip lookup priority + . headEx + $ (setFromList (unpack a) :: Set Char) `intersect` setFromList (unpack b) + ) + +prob2 :: [Text] -> Char +prob2 [a, b, c] = + headEx . toList $ + (setFromList (unpack a) :: Set Char) + `intersect` setFromList (unpack b) + `intersect` setFromList (unpack c) +prob2 _ = ';' + +run :: IO () +run = do + file <- getArgs >>= readFileUtf8 . unpack . headEx + putStrLn $ "day3.1: " <> (tshow . sum . map prob1 . lines $ file) + + putStrLn $ "day3.2: " <> (tshow . sum . mapMaybe (flip lookup priority . prob2) . chunksOf 3 . lines $ file) diff --git a/src/Lib.hs b/src/Lib.hs new file mode 100644 index 0000000..2300ea3 --- /dev/null +++ b/src/Lib.hs @@ -0,0 +1,76 @@ +module Lib ( + someFunc, +) where + +import ClassyPrelude hiding (decodeUtf8, (<|)) +import Control.Monad.ST +import qualified Data.ByteString as B +import Data.Char (ord) +import Data.STRef +import System.IO.Posix.MMap + +newtype TrieGroup = TrieGroup (Int, Int, Int) + +parseNumber :: ByteString -> Int +parseNumber b = runST $ do + len <- newSTRef (length b - 1) + foldlM + ( \acc c -> do + i <- readSTRef len + modifySTRef' len (\v -> v - 1) + pure $! getNumber c * (10 ^ i) + acc + ) + 0 + b + where + getNumber :: Word8 -> Int + getNumber c = fromIntegral $ c - (fromIntegral . ord $ '0') +{-# INLINE parseNumber #-} + +insertTrieGroup :: + TrieGroup -> + Int -> + TrieGroup +insertTrieGroup t@(TrieGroup (!a, !b, !c)) i + | i > a = TrieGroup (i, a, b) + | i > b = TrieGroup (a, i, b) + | i > c = TrieGroup (a, b, i) + | otherwise = t +{-# INLINE insertTrieGroup #-} + +newline :: Word8 +newline = fromIntegral . ord $ '\n' + + +run :: ByteString -> TrieGroup +run bs = runST $ do + foundNewLine <- newSTRef False + (t, acc, s) <- + foldlM + ( \(t, acc, s) c -> do + if c == newline + then do + readSTRef foundNewLine >>= \case + True -> do + writeSTRef foundNewLine False + pure (insertTrieGroup t s, [], 0) + False -> do + writeSTRef foundNewLine True + pure (t, [], s + parseNumber (B.pack . reverse $ acc)) + else do + writeSTRef foundNewLine False + pure (t, c : acc, s) + + ) + (TrieGroup (0, 0, 0), [], 0 :: Int) + bs + + pure (insertTrieGroup t (s + parseNumber (B.pack . reverse $ acc))) + +someFunc :: IO () +someFunc = do + puzzleInput <- unsafeMMapFile "aoc_2022_day01_large_input.txt" + let TrieGroup (a, b, c) = run puzzleInput + + putStrLn $ "Day1.1: " <> tshow a + putStrLn $ "Day1.2: " <> tshow (a + b + c) diff --git a/stack.yaml b/stack.yaml new file mode 100644 index 0000000..b3688f8 --- /dev/null +++ b/stack.yaml @@ -0,0 +1,66 @@ +# This file was automatically generated by 'stack init' +# +# Some commonly used options have been documented as comments in this file. +# For advanced use and comprehensive documentation of the format, please see: +# https://docs.haskellstack.org/en/stable/yaml_configuration/ + +# Resolver to choose a 'specific' stackage snapshot or a compiler version. +# A snapshot resolver dictates the compiler version and the set of packages +# to be used for project dependencies. For example: +# +# resolver: lts-3.5 +# resolver: nightly-2015-09-21 +# resolver: ghc-7.10.2 +# +# The location of a snapshot can be provided as a file or url. Stack assumes +# a snapshot provided as a file might change, whereas a url resource does not. +# +# resolver: ./custom-snapshot.yaml +# resolver: https://example.com/snapshots/2018-01-01.yaml +resolver: lts-19.33 + +# User packages to be built. +# Various formats can be used as shown in the example below. +# +# packages: +# - some-directory +# - https://example.com/foo/bar/baz-0.0.2.tar.gz +# subdirs: +# - auto-update +# - wai +packages: +- . +# Dependency packages to be pulled from upstream that are not in the resolver. +# These entries can reference officially published versions as well as +# forks / in-progress versions pinned to a git hash. For example: +# +# extra-deps: +# - acme-missiles-0.3 +# - git: https://github.com/commercialhaskell/stack.git +# commit: e7b331f14bcffb8367cd58fbfc8b40ec7642100a +# +# extra-deps: [] + +# Override default flag values for local packages and extra-deps +# flags: {} + +# Extra package databases containing global packages +# extra-package-dbs: [] + +# Control whether we use the GHC we find on the path +# system-ghc: true +# +# Require a specific version of stack, using version ranges +# require-stack-version: -any # Default +# require-stack-version: ">=2.7" +# +# Override the architecture used by stack, especially useful on Windows +# arch: i386 +# arch: x86_64 +# +# Extra directories used by stack for building +# extra-include-dirs: [/path/to/dir] +# extra-lib-dirs: [/path/to/dir] +# +# Allow a newer minor version of GHC than the snapshot specifies +# compiler-check: newer-minor diff --git a/stack.yaml.lock b/stack.yaml.lock new file mode 100644 index 0000000..d79c369 --- /dev/null +++ b/stack.yaml.lock @@ -0,0 +1,12 @@ +# This file was autogenerated by Stack. +# You should not edit this file by hand. +# For more information, please see the documentation at: +# https://docs.haskellstack.org/en/stable/lock_files + +packages: [] +snapshots: +- completed: + sha256: 6d1532d40621957a25bad5195bfca7938e8a06d923c91bc52aa0f3c41181f2d4 + size: 619204 + url: https://raw.githubusercontent.com/commercialhaskell/stackage-snapshots/master/lts/19/33.yaml + original: lts-19.33 diff --git a/test/Spec.hs b/test/Spec.hs new file mode 100644 index 0000000..602274d --- /dev/null +++ b/test/Spec.hs @@ -0,0 +1,9 @@ +module Spec (main) where + +import ClassyPrelude +-- import Day2 +import Day3 +import System.Environment (withArgs) + +main :: IO () +main = withArgs ["puzzle3.txt"] run