commit 45347bf88bb5d0e2003aafd79efe528d2630abee Author: Amneesh Singh Date: Sun Dec 1 17:34:14 2024 +0530 day 1 Signed-off-by: Amneesh Singh diff --git a/.envrc b/.envrc new file mode 100644 index 0000000..3550a30 --- /dev/null +++ b/.envrc @@ -0,0 +1 @@ +use flake diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a43a9cf --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +dist* +inputs/*.in +result +*~ \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..35c5f7e --- /dev/null +++ b/README.md @@ -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/) + diff --git a/aoc2024.cabal b/aoc2024.cabal new file mode 100644 index 0000000..9809743 --- /dev/null +++ b/aoc2024.cabal @@ -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 diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..31a5d66 --- /dev/null +++ b/flake.lock @@ -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 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..10efe1a --- /dev/null +++ b/flake.nix @@ -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; + }; + }; +} diff --git a/fourmolu.yaml b/fourmolu.yaml new file mode 100644 index 0000000..3bbb701 --- /dev/null +++ b/fourmolu.yaml @@ -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 diff --git a/lib/AoC.hs b/lib/AoC.hs new file mode 100644 index 0000000..9742783 --- /dev/null +++ b/lib/AoC.hs @@ -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 ==) diff --git a/src/P1.hs b/src/P1.hs new file mode 100644 index 0000000..942a4fa --- /dev/null +++ b/src/P1.hs @@ -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)