Signed-off-by: Amneesh Singh <natto@weirdnatto.in>
This commit is contained in:
2024-12-01 17:34:14 +05:30
commit 45347bf88b
9 changed files with 196 additions and 0 deletions

1
.envrc Normal file
View File

@@ -0,0 +1 @@
use flake

4
.gitignore vendored Normal file
View File

@@ -0,0 +1,4 @@
dist*
inputs/*.in
result
*~

6
README.md Normal file
View File

@@ -0,0 +1,6 @@
[haskell-flake](https://github.com/srid/haskell-flake)
Copied from: https://github.com/srid/haskell-flake/tree/0.4.0/example
[Documentation](https://zero-to-flakes.com/haskell-flake/)

26
aoc2024.cabal Normal file
View File

@@ -0,0 +1,26 @@
cabal-version: 3.0
name: aoc2024
version: 0.1.0.0
license: BSD-3-Clause
author: Amneesh
maintainer: natto@weirdnatto.in
build-type: Simple
common common
ghc-options: -Wall -O3
build-depends:
, base >=4.14 && <5
, parsec >=3
library libaoc
import: common
exposed: False
hs-source-dirs: lib
build-depends: containers
exposed-modules: AoC
executable p1
import: common
hs-source-dirs: src
main-is: P1.hs
build-depends: libaoc

74
flake.lock generated Normal file
View File

@@ -0,0 +1,74 @@
{
"nodes": {
"flake-parts": {
"inputs": {
"nixpkgs-lib": "nixpkgs-lib"
},
"locked": {
"lastModified": 1730504689,
"narHash": "sha256-hgmguH29K2fvs9szpq2r3pz2/8cJd2LPS+b4tfNFCwE=",
"owner": "hercules-ci",
"repo": "flake-parts",
"rev": "506278e768c2a08bec68eb62932193e341f55c90",
"type": "github"
},
"original": {
"owner": "hercules-ci",
"repo": "flake-parts",
"type": "github"
}
},
"haskell-flake": {
"locked": {
"lastModified": 1732461017,
"narHash": "sha256-FS4ZavcceO2GJOKGDejkJsuEngHb2Ioq+jnORGZzaKE=",
"owner": "srid",
"repo": "haskell-flake",
"rev": "725292998cdb695dc312325603a662afa97b9e86",
"type": "github"
},
"original": {
"owner": "srid",
"repo": "haskell-flake",
"type": "github"
}
},
"nixpkgs": {
"locked": {
"lastModified": 1732780316,
"narHash": "sha256-NskLIz0ue4Uqbza+1+8UGHuPVr8DrUiLfZu5VS4VQxw=",
"owner": "nixos",
"repo": "nixpkgs",
"rev": "226216574ada4c3ecefcbbec41f39ce4655f78ef",
"type": "github"
},
"original": {
"owner": "nixos",
"ref": "nixpkgs-unstable",
"repo": "nixpkgs",
"type": "github"
}
},
"nixpkgs-lib": {
"locked": {
"lastModified": 1730504152,
"narHash": "sha256-lXvH/vOfb4aGYyvFmZK/HlsNsr/0CVWlwYvo2rxJk3s=",
"type": "tarball",
"url": "https://github.com/NixOS/nixpkgs/archive/cc2f28000298e1269cea6612cd06ec9979dd5d7f.tar.gz"
},
"original": {
"type": "tarball",
"url": "https://github.com/NixOS/nixpkgs/archive/cc2f28000298e1269cea6612cd06ec9979dd5d7f.tar.gz"
}
},
"root": {
"inputs": {
"flake-parts": "flake-parts",
"haskell-flake": "haskell-flake",
"nixpkgs": "nixpkgs"
}
}
},
"root": "root",
"version": 7
}

32
flake.nix Normal file
View File

@@ -0,0 +1,32 @@
{
inputs = {
nixpkgs.url = "github:nixos/nixpkgs/nixpkgs-unstable";
flake-parts.url = "github:hercules-ci/flake-parts";
haskell-flake.url = "github:srid/haskell-flake";
};
outputs = inputs@{ self, nixpkgs, flake-parts, ... }:
flake-parts.lib.mkFlake { inherit inputs; } {
systems = nixpkgs.lib.systems.flakeExposed;
imports = [ inputs.haskell-flake.flakeModule ];
perSystem = { self', pkgs, ... }: {
haskellProjects.default = {
basePackages = pkgs.haskellPackages;
devShell = {
enable = true;
hlsCheck.enable = true;
tools = hp: {
inherit (pkgs)
nixpkgs-fmt;
inherit (hp)
cabal-fmt
fourmolu;
};
};
};
packages.default = self'.packages.aoc2024;
};
};
}

8
fourmolu.yaml Normal file
View File

@@ -0,0 +1,8 @@
indentation: 2
comma-style: leading
record-brace-space: true
indent-wheres: true
diff-friendly-import-export: true
respectful: true
haddock-style: multi-line
newlines-between-decls: 1

10
lib/AoC.hs Normal file
View File

@@ -0,0 +1,10 @@
module AoC where
import Text.Parsec
extract :: Either ParseError a -> a
extract (Left err) = error ("Parsing failed: " ++ show err)
extract (Right val) = val
count :: (Eq a) => a -> [a] -> Int
count x = length . filter (x ==)

35
src/P1.hs Normal file
View File

@@ -0,0 +1,35 @@
module Main where
import qualified AoC as A (count, extract)
import Data.List (sort)
import Text.Parsec (digit, many1, newline, parse, space, try)
import Text.Parsec.String (Parser)
parseLists :: Parser ([Int], [Int])
parseLists =
unzip
-- multiple lines
<$> many1
( (,)
-- first number
<$> (read <$> many1 digit)
-- second number
<*> (many1 space *> (read <$> many1 digit) <* try newline)
)
part1 :: [Int] -> [Int] -> Int
part1 xs ys = sum $ zipWith ((abs .) . (-)) xs ys
part2 :: [Int] -> [Int] -> Int
part2 xs ys = sum $ map (\x -> x * A.count x ys) xs
main :: IO ()
main =
do
raw <- readFile "./inputs/p1.in"
-- parse the input
let (xs', ys') = A.extract $ parse parseLists "" raw
-- sort the lists
let (xs, ys) = (sort xs', sort ys')
putStr "Part 1: " >> print (part1 xs ys)
putStr "Part 2: " >> print (part2 xs ys)