commit ae3890394a931b966aee24cd1d1cf08b5ea40be8 Author: Amneesh Singh Date: Tue Dec 2 01:03:20 2025 +0530 day 1: init 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/LICENSE b/LICENSE new file mode 100644 index 0000000..d9f3460 --- /dev/null +++ b/LICENSE @@ -0,0 +1,11 @@ +Copyright 2024 Amneesh Singh + +Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. + +2. 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. + +3. Neither the name of the copyright holder nor the names of its 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 HOLDER 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. diff --git a/README.md b/README.md new file mode 100644 index 0000000..ffb817d --- /dev/null +++ b/README.md @@ -0,0 +1,17 @@ +# Advent of Code 2024 + +My dumb AoC solutions, again in haskell this year + +## Cabal +``` +cabal build +``` +``` +cabal run +``` + +## Nix + +``` +nix build +``` diff --git a/aoc2025.cabal b/aoc2025.cabal new file mode 100644 index 0000000..bf19b85 --- /dev/null +++ b/aoc2025.cabal @@ -0,0 +1,31 @@ +cabal-version: 3.0 +name: aoc2025 +version: 0.1.0.0 +license: BSD-3-Clause +author: Amneesh +maintainer: natto@weirdnatto.in +build-type: Simple + +common common + ghc-options: -Wall -O3 + default-extensions: + LambdaCase + MultiWayIf + TupleSections + ViewPatterns + + build-depends: + , base >=4.14 && <5 + , parsec >=3 + +library libaoc + import: common + exposed: False + hs-source-dirs: lib + exposed-modules: AoC + +executable day01 + import: common + hs-source-dirs: src + main-is: Day01.hs + build-depends: libaoc diff --git a/fetch.sh b/fetch.sh new file mode 100755 index 0000000..fb7c184 --- /dev/null +++ b/fetch.sh @@ -0,0 +1,38 @@ +#!/usr/bin/env bash + +SOURCE_DIR=$(realpath "$(dirname "$0")") +INPUT_DIR=${INPUT_DIR:="$SOURCE_DIR/inputs"} + +help () { + echo "usage: $0 " + echo "example: $0 day15" + echo " $0 15" + echo " $0 all" +} + +fetch () { + TMPFILE=$(mktemp) + curl "https://adventofcode.com/2025/day/${1}/input" \ + -s --fail-with-body --cookie "session=$AOC_SESSION" \ + -o "${TMPFILE}" + + mkdir -p "$INPUT_DIR" + mv "$TMPFILE" "$INPUT_DIR/day${1}.in" + echo "Fetched Day $1" +} + +if [[ $# != 1 || ! "${1#day}" =~ ^([1-9]|1[0-9]|2[0-5]|all)$ ]]; then + help + exit 1 +fi + +if [[ -z "${AOC_SESSION}" ]]; then + echo "\$AOC_SESSION is not set" + exit 1 +fi + +if [[ $1 == "all" ]]; then + for x in {1..25}; do fetch "$x"; done +else + fetch "${1#day}" +fi diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..7bb550b --- /dev/null +++ b/flake.lock @@ -0,0 +1,77 @@ +{ + "nodes": { + "flake-parts": { + "inputs": { + "nixpkgs-lib": "nixpkgs-lib" + }, + "locked": { + "lastModified": 1763759067, + "narHash": "sha256-LlLt2Jo/gMNYAwOgdRQBrsRoOz7BPRkzvNaI/fzXi2Q=", + "owner": "hercules-ci", + "repo": "flake-parts", + "rev": "2cccadc7357c0ba201788ae99c4dfa90728ef5e0", + "type": "github" + }, + "original": { + "owner": "hercules-ci", + "repo": "flake-parts", + "type": "github" + } + }, + "haskell-flake": { + "locked": { + "lastModified": 1763904090, + "narHash": "sha256-Mq99c4O6Ovx/JQgi6B6fnI9xR77nQ9+GcFTM2113nMI=", + "owner": "srid", + "repo": "haskell-flake", + "rev": "73c006f8e6531acab28bbde362ae8f13ee0ac72b", + "type": "github" + }, + "original": { + "owner": "srid", + "repo": "haskell-flake", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1764587062, + "narHash": "sha256-hdFa0TAVQAQLDF31cEW3enWmBP+b592OvHs6WVe3D8k=", + "owner": "nixos", + "repo": "nixpkgs", + "rev": "c1cb7d097cb250f6e1904aacd5f2ba5ffd8a49ce", + "type": "github" + }, + "original": { + "owner": "nixos", + "ref": "nixpkgs-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "nixpkgs-lib": { + "locked": { + "lastModified": 1761765539, + "narHash": "sha256-b0yj6kfvO8ApcSE+QmA6mUfu8IYG6/uU28OFn4PaC8M=", + "owner": "nix-community", + "repo": "nixpkgs.lib", + "rev": "719359f4562934ae99f5443f20aa06c2ffff91fc", + "type": "github" + }, + "original": { + "owner": "nix-community", + "repo": "nixpkgs.lib", + "type": "github" + } + }, + "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..febc0f3 --- /dev/null +++ b/flake.nix @@ -0,0 +1,54 @@ +{ + 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, ... }: + { + formatter = pkgs.nixfmt-tree; + + 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; + apps = + let + name = n: "day${(if n < 10 then "0" else "") + toString n}"; + in + with pkgs.lib; + genAttrs (map name (range 1 1)) (n: { + type = "app"; + program = "${self'.packages.aoc2025}/bin/${n}"; + }); + }; + }; +} diff --git a/fourmolu.yaml b/fourmolu.yaml new file mode 100644 index 0000000..ed2de01 --- /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..904bce1 --- /dev/null +++ b/lib/AoC.hs @@ -0,0 +1,8 @@ +module AoC where + +import Text.Parsec (ParseError) + +-- extract Right value after parsing +extract :: Either ParseError a -> a +extract (Left err) = error ("Parsing failed: " ++ show err) +extract (Right val) = val diff --git a/src/Day01.hs b/src/Day01.hs new file mode 100644 index 0000000..d893efc --- /dev/null +++ b/src/Day01.hs @@ -0,0 +1,35 @@ +import qualified AoC as A (extract) +import Text.Parsec (char, digit, many1, newline, parse, sepEndBy1, (<|>)) +import Text.Parsec.String (Parser) + +data Rotation = L Int | R Int + +parseRotations :: Parser [Rotation] +parseRotations = + sepEndBy1 + ( (char 'L' >> (L . read <$> many1 digit)) + <|> (char 'R' >> (R . read <$> many1 digit)) + ) + newline + +part1 :: [Rotation] -> Int +part1 = length . filter (== 0) . scanl applyRotation 50 + where + applyRotation :: Int -> Rotation -> Int + applyRotation n (L x) = (n - x) `mod` 100 + applyRotation n (R x) = (n + x) `mod` 100 + +part2 :: [Rotation] -> Int +part2 = sum . map snd . scanl applyRotation (50, 0) + where + applyRotation :: (Int, Int) -> Rotation -> (Int, Int) + applyRotation (n, _) (L x) = ((n - x) `mod` 100, ((100 - n) `mod` 100 + x) `div` 100) + applyRotation (n, _) (R x) = ((n + x) `mod` 100, (n + x) `div` 100) + +main :: IO () +main = + do + raw <- readFile "./inputs/day1.in" + let rotations = A.extract $ parse parseRotations "" raw + putStr "Part 1: " >> print (part1 rotations) + putStr "Part 2: " >> print (part2 rotations)