-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
263 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/day*.txt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,8 @@ | ||
# [Advent of Code 2023](https://adventofcode.com/2023) | ||
### my answers | ||
|
||
Development occurs in language-specific directories: | ||
|
||
|[Haskell](hs)| | ||
|--:| | ||
|[Day1.hs](hs/src/Day1.hs)| |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
day*.txt | ||
cabal.project.local | ||
dist-* | ||
.ghci | ||
.ghcid | ||
*~ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
Copyright (c) 2023, Daniel Lin | ||
|
||
All rights reserved. | ||
|
||
Redistribution and use in source and binary forms, with or without | ||
modification, are permitted provided that the following conditions are met: | ||
|
||
* Redistributions of source code must retain the above copyright | ||
notice, this list of conditions and the following disclaimer. | ||
|
||
* Redistributions in binary form must reproduce the above | ||
copyright notice, this list of conditions and the following | ||
disclaimer in the documentation and/or other materials provided | ||
with the distribution. | ||
|
||
* Neither the name of Daniel Lin nor the names of other | ||
contributors may be used to endorse or promote products derived | ||
from this software without specific prior written permission. | ||
|
||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# [Advent of Code 2023](https://adventofcode.com/2023) | ||
### my answers in [Haskell](https://www.haskell.org/) | ||
|
||
This project builds with [The Haskell Cabal](https://www.haskell.org/cabal/). | ||
|
||
Setup: | ||
|
||
```sh | ||
curl --proto '=https' --tlsv1.2 -sSf https://get-ghcup.haskell.org | sh | ||
ghcup install cabal latest | ||
ghcup install ghc 9.4.7 | ||
cabal configure --with-compiler ghc-9.4 --enable-tests | ||
``` | ||
|
||
Run the [Hspec](https://hspec.github.io/) test suite: | ||
|
||
```sh | ||
cabal test test:aoc2023-test | ||
``` | ||
|
||
Run [criterion](http://www.serpentine.com/criterion/) benchmarks ([results online](https://ephemient.github.io/aoc2023/aoc2023-bench.html)): | ||
|
||
```sh | ||
cabal bench bench:aoc2023-bench | ||
``` | ||
|
||
Print solutions for the inputs provided in local data files: | ||
|
||
```sh | ||
cabal run exe:aoc2023 | ||
``` | ||
|
||
Generate [Haddock](https://www.haskell.org/haddock/) API documentation: | ||
|
||
```sh | ||
cabal haddock lib:aoc2023 | ||
``` | ||
|
||
Run [hlint](https://github.com/ndmitchell/hlint) source code suggestions: | ||
|
||
```sh | ||
cabal install hlint | ||
hlint src test bench | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
cabal-version: 2.4 | ||
name: aoc2023 | ||
version: 0.1.0.0 | ||
synopsis: Advent of Code 2023 - my solutions in Haskell | ||
|
||
-- A longer description of the package. | ||
description: Please see the README on GitHub at <https://github.com/ephemient/aoc2023/blob/main/hs/README.md> | ||
homepage: https://github.com/ephemient/aoc2023/tree/main/hs | ||
|
||
-- A URL where users can report bugs. | ||
bug-reports: https://github.com/ephemient/aoc2023/issues | ||
license: BSD-3-Clause | ||
license-file: LICENSE | ||
author: Daniel Lin | ||
maintainer: ephemient@gmail.com | ||
|
||
-- A copyright notice. | ||
copyright: (c) 2023 Daniel Lin | ||
-- category: | ||
extra-source-files: README.md | ||
|
||
library | ||
exposed-modules: | ||
Day1 | ||
|
||
-- Modules included in this library but not exported. | ||
other-modules: | ||
Common | ||
|
||
-- LANGUAGE extensions used by modules in this package. | ||
-- other-extensions: | ||
build-depends: | ||
base ^>=4.17.2.0, | ||
text ^>=2.0.1 | ||
hs-source-dirs: src | ||
default-language: GHC2021 | ||
|
||
executable aoc2023 | ||
main-is: Main.hs | ||
|
||
-- Modules included in this executable, other than Main. | ||
-- other-modules: | ||
|
||
-- LANGUAGE extensions used by modules in this package. | ||
-- other-extensions: | ||
build-depends: | ||
aoc2023, | ||
base ^>=4.17.2.0, | ||
filepath ^>=1.4.2.2, | ||
text ^>=2.0.1 | ||
ghc-options: -threaded -rtsopts "-with-rtsopts=-N" | ||
|
||
hs-source-dirs: app | ||
default-language: GHC2021 | ||
|
||
test-suite aoc2023-test | ||
default-language: GHC2021 | ||
type: exitcode-stdio-1.0 | ||
|
||
-- Directories containing source files. | ||
hs-source-dirs: test | ||
main-is: Main.hs | ||
other-modules: | ||
Day1Spec | ||
build-depends: | ||
aoc2023, | ||
base ^>=4.17.2.0, | ||
hspec ^>= 2.11.7, | ||
text ^>=2.0.1 | ||
build-tool-depends: | ||
hspec-discover:hspec-discover ^>=2.11.7 | ||
ghc-options: -threaded -rtsopts "-with-rtsopts=-N" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
{-# LANGUAGE NondecreasingIndentation #-} | ||
module Main (main) where | ||
|
||
import qualified Day1 (part1, part2) | ||
|
||
import Control.Monad (ap, when) | ||
import Data.Foldable (find) | ||
import Data.Maybe (fromMaybe) | ||
import Data.Text (Text) | ||
import qualified Data.Text.IO as TIO (putStrLn, readFile) | ||
import System.Environment (getArgs, lookupEnv) | ||
import System.FilePath (combine) | ||
|
||
getDayInput :: Int -> IO Text | ||
getDayInput i = do | ||
dataDir <- fromMaybe "." . find (not . null) <$> lookupEnv "AOC2023_DATADIR" | ||
TIO.readFile . combine dataDir $ "day" ++ show i ++ ".txt" | ||
|
||
run :: Int -> (a -> IO ()) -> [Text -> a] -> IO () | ||
run = run' `ap` show | ||
|
||
run' :: Int -> String -> (a -> IO ()) -> [Text -> a] -> IO () | ||
run' day name showIO funcs = do | ||
args <- getArgs | ||
when (null args || name `elem` args) $ do | ||
putStrLn $ "Day " ++ name | ||
contents <- getDayInput day | ||
mapM_ (showIO . ($ contents)) funcs | ||
putStrLn "" | ||
|
||
main :: IO () | ||
main = do | ||
run 1 print [Day1.part1, Day1.part2] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
packages: . | ||
with-compiler: ghc-9.4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
{-| | ||
Module: Day1 | ||
Description: <https://adventofcode.com/2023/day/1 Day 1: Trebuchet?! > | ||
-} | ||
{-# LANGUAGE OverloadedStrings #-} | ||
module Day1 (part1, part2) where | ||
|
||
import Data.Maybe (fromMaybe, listToMaybe) | ||
import Data.Text (Text) | ||
import qualified Data.Text as T (inits, isPrefixOf, isSuffixOf, lines, pack, tails) | ||
|
||
part1, part2 :: Text -> Int | ||
part1 = solve digits | ||
part2 = solve extendedDigits | ||
|
||
solve :: [(Int, Text)] -> Text -> Int | ||
solve values = sum . map solve' . T.lines where | ||
solve' line = fromMaybe 0 . listToMaybe $ do | ||
x <- [i | s <- T.tails line, (i, t) <- values, t `T.isPrefixOf` s] | ||
y <- [j | s <- reverse $ T.inits line, (j, t) <- values, t `T.isSuffixOf` s] | ||
pure $ 10 * x + y | ||
|
||
digits, extendedDigits :: [(Int, Text)] | ||
digits = [(d, T.pack $ show d) | d <- [0..9]] | ||
extendedDigits = digits ++ | ||
[ (1, "one") | ||
, (2, "two") | ||
, (3, "three") | ||
, (4, "four") | ||
, (5, "five") | ||
, (6, "six") | ||
, (7, "seven") | ||
, (8, "eight") | ||
, (9, "nine") | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
{-# LANGUAGE OverloadedStrings #-} | ||
module Day1Spec (spec) where | ||
|
||
import Data.Text (Text) | ||
import qualified Data.Text as T (unlines) | ||
import Day1 (part1, part2) | ||
import Test.Hspec (Spec, describe, it, shouldBe) | ||
|
||
example1, example2 :: Text | ||
example1 = T.unlines | ||
[ "1abc2" | ||
, "pqr3stu8vwx" | ||
, "a1b2c3d4e5f" | ||
, "treb7uchet" | ||
] | ||
example2 = T.unlines | ||
[ "two1nine" | ||
, "eightwothree" | ||
, "abcone2threexyz" | ||
, "xtwone3four" | ||
, "4nineeightseven2" | ||
, "zoneight234" | ||
, "7pqrstsixteen" | ||
] | ||
|
||
spec :: Spec | ||
spec = do | ||
describe "part 1" $ do | ||
it "examples" $ do | ||
part1 example1 `shouldBe` 142 | ||
describe "part 2" $ do | ||
it "examples" $ do | ||
part2 example2 `shouldBe` 281 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{-# OPTIONS_GHC -F -pgmF hspec-discover #-} |