Initial release

This commit is contained in:
Stefan Risberg 2022-12-03 10:50:09 +01:00
commit 01cdae0f7a
16 changed files with 614 additions and 0 deletions

4
.ghci Normal file
View File

@ -0,0 +1,4 @@
:set -fwarn-unused-binds -fwarn-unused-imports
:set -isrc
:set -itest
:load Spec

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
.stack-work/
*~
*.txt

7
LICENSE Normal file
View File

@ -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.

34
Makefile Normal file
View File

@ -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

1
README.md Normal file
View File

@ -0,0 +1 @@
# aoc2022

2
Setup.hs Normal file
View File

@ -0,0 +1,2 @@
import Distribution.Simple
main = defaultMain

178
aoc2022.cabal Normal file
View File

@ -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 <https://github.com/githubuser/aoc2022#readme>
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

8
app/Main.hs Normal file
View File

@ -0,0 +1,8 @@
module Main (main) where
import ClassyPrelude
--import Lib
import Day2
main :: IO ()
main = fun

9
fourmolu.yaml Normal file
View File

@ -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: []

99
package.yaml Normal file
View File

@ -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 <https://github.com/githubuser/aoc2022#readme>
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

73
src/Day2.hs Normal file
View File

@ -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)

33
src/Day3.hs Normal file
View File

@ -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)

76
src/Lib.hs Normal file
View File

@ -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)

66
stack.yaml Normal file
View File

@ -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

12
stack.yaml.lock Normal file
View File

@ -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

9
test/Spec.hs Normal file
View File

@ -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