Skip to content

Commit

Permalink
feat: add Adlude module
Browse files Browse the repository at this point in the history
  • Loading branch information
japiirainen committed Nov 26, 2023
1 parent 9056107 commit 1b5a71a
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 32 deletions.
4 changes: 0 additions & 4 deletions Main.hs

This file was deleted.

17 changes: 2 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,10 @@
This project is usable either via `nix` or via `cabal`. I will provide
commands for both in the following sections.

### run all solutions

#### nix

```sh
nix run .#aoc
```

#### cabal

```sh
cabal run exe:aoc
```

### run specific solution
### running solution

It is possible to run only a specific day. In this example day # `01` is ran.
In this example day # `01` is ran.

#### nix

Expand Down
14 changes: 3 additions & 11 deletions aoc.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -74,17 +74,9 @@ library lib
, text

hs-source-dirs: lib
exposed-modules: Parsing

executable aoc
import: shared
build-depends:
, base
, containers
, text

hs-source-dirs: .
main-is: Main.hs
exposed-modules:
Adlude
Parsing

executable 01
import: shared
Expand Down
4 changes: 2 additions & 2 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@
};
};

packages.default = self'.packages.aoc;
apps.default = self'.apps.aoc;
packages.default = self'.packages."01";
apps.default = self'.apps."01";

devShells.default = pkgs.mkShell {
name = "aoc";
Expand Down
30 changes: 30 additions & 0 deletions lib/Adlude.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
-- | `Prelude` for Advent of Code problems.
module Adlude (
pickOne,
countBy,
count,
counts,
partialSums,
) where

import Data.Foldable (toList)
import Data.List (foldl', inits, scanl', tails)
import Data.Map (Map)
import Data.Map qualified as Map

pickOne :: [a] -> [(a, [a])]
pickOne xs = do
(ys, x : zs) <- zip (inits xs) (tails xs)
return (x, ys ++ zs)

countBy :: (Foldable f) => (a -> Bool) -> f a -> Int
countBy p = foldl' (\acc x -> if p x then acc + 1 else acc) 0

count :: (Foldable f, Eq a) => a -> f a -> Int
count = countBy . (==)

counts :: (Foldable f, Ord a) => f a -> Map a Int
counts xs = Map.fromListWith (+) [(x, 1) | x <- toList xs]

partialSums :: (Num a) => [a] -> [a]
partialSums = scanl' (+) 0

0 comments on commit 1b5a71a

Please sign in to comment.