day 1: init

Signed-off-by: Amneesh Singh <natto@weirdnatto.in>
This commit is contained in:
2025-12-02 01:03:20 +05:30
commit ae3890394a
11 changed files with 284 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
*~

11
LICENSE Normal file
View File

@@ -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.

17
README.md Normal file
View File

@@ -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
```

31
aoc2025.cabal Normal file
View File

@@ -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

38
fetch.sh Executable file
View File

@@ -0,0 +1,38 @@
#!/usr/bin/env bash
SOURCE_DIR=$(realpath "$(dirname "$0")")
INPUT_DIR=${INPUT_DIR:="$SOURCE_DIR/inputs"}
help () {
echo "usage: $0 <day1-day25>"
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

77
flake.lock generated Normal file
View File

@@ -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
}

54
flake.nix Normal file
View File

@@ -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}";
});
};
};
}

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

8
lib/AoC.hs Normal file
View File

@@ -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

35
src/Day01.hs Normal file
View File

@@ -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)