day 3: init

Signed-off-by: Amneesh Singh <natto@weirdnatto.in>
This commit is contained in:
2025-12-03 17:22:59 +05:30
parent e6a451e07d
commit d32a56e50b
4 changed files with 47 additions and 5 deletions

View File

@@ -35,3 +35,9 @@ executable day02
hs-source-dirs: src hs-source-dirs: src
main-is: Day02.hs main-is: Day02.hs
build-depends: libaoc build-depends: libaoc
executable day03
import: common
hs-source-dirs: src
main-is: Day03.hs
build-depends: libaoc

6
flake.lock generated
View File

@@ -35,11 +35,11 @@
}, },
"nixpkgs": { "nixpkgs": {
"locked": { "locked": {
"lastModified": 1764587062, "lastModified": 1764642553,
"narHash": "sha256-hdFa0TAVQAQLDF31cEW3enWmBP+b592OvHs6WVe3D8k=", "narHash": "sha256-mvbFFzVBhVK1FjyPHZGMAKpNiqkr7k++xIwy+p/NQvA=",
"owner": "nixos", "owner": "nixos",
"repo": "nixpkgs", "repo": "nixpkgs",
"rev": "c1cb7d097cb250f6e1904aacd5f2ba5ffd8a49ce", "rev": "f720de59066162ee879adcc8c79e15c51fe6bfb4",
"type": "github" "type": "github"
}, },
"original": { "original": {

View File

@@ -39,13 +39,13 @@
}; };
}; };
packages.default = self'.packages.aoc2024; packages.default = self'.packages.aoc2025;
apps = apps =
let let
name = n: "day${(if n < 10 then "0" else "") + toString n}"; name = n: "day${(if n < 10 then "0" else "") + toString n}";
in in
with pkgs.lib; with pkgs.lib;
genAttrs (map name (range 1 1)) (n: { genAttrs (map name (range 1 3)) (n: {
type = "app"; type = "app";
program = "${self'.packages.aoc2025}/bin/${n}"; program = "${self'.packages.aoc2025}/bin/${n}";
}); });

36
src/Day03.hs Normal file
View File

@@ -0,0 +1,36 @@
import qualified AoC as A (extract)
import Data.Char (digitToInt)
import Text.Parsec (digit, many1, newline, parse, sepEndBy1)
import Text.Parsec.String (Parser)
type Battery = Int
type Bank = [Battery]
parseBanks :: Parser [Bank]
parseBanks = sepEndBy1 parseBank newline
where
parseBank :: Parser Bank
parseBank = many1 parseBattery
parseBattery :: Parser Battery
parseBattery = digitToInt <$> digit
joltages :: Int -> [Bank] -> Int
joltages n = sum . map (joltage n)
where
joltage :: Int -> Bank -> Int
joltage 1 xs = maximum xs
joltage k xs =
let len = length xs
window = take (len - k + 1) xs
(mx, (* (-1)) -> i) = maximum $ zip window [-1, -2 ..]
in mx * 10 ^ (k - 1) + joltage (k - 1) (drop i xs)
main :: IO ()
main =
do
raw <- readFile "./inputs/day3.in"
let banks = A.extract $ parse parseBanks "" raw
putStr "Part 1: " >> print (joltages 2 banks)
putStr "Part 2: " >> print (joltages 12 banks)