Signed-off-by: Amneesh Singh <natto@weirdnatto.in>
This commit is contained in:
2023-12-02 04:47:21 +05:30
commit 98121485a7
4 changed files with 115 additions and 0 deletions

1
.envrc Normal file
View File

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

40
day01.hs Normal file
View File

@@ -0,0 +1,40 @@
{-# LANGUAGE OverloadedStrings #-}
import Data.Char (digitToInt, isDigit)
import Data.Text qualified as T
import Lib (readFile')
first :: T.Text -> Int
first s = f * 10 + l
where
values = T.filter isDigit s
f = (digitToInt . T.head) values
l = (digitToInt . T.last) values
second' :: T.Text -> [Int]
second' s
| T.null s = []
| T.isPrefixOf "one" s = 1 : second' (T.tail s)
| T.isPrefixOf "two" s = 2 : second' (T.tail s)
| T.isPrefixOf "three" s = 3 : second' (T.tail s)
| T.isPrefixOf "four" s = 4 : second' (T.tail s)
| T.isPrefixOf "five" s = 5 : second' (T.tail s)
| T.isPrefixOf "six" s = 6 : second' (T.tail s)
| T.isPrefixOf "seven" s = 7 : second' (T.tail s)
| T.isPrefixOf "eight" s = 8 : second' (T.tail s)
| T.isPrefixOf "nine" s = 9 : second' (T.tail s)
| isDigit (T.head s) = digitToInt (T.head s) : second' (T.tail s)
| otherwise = second' (T.tail s)
second :: T.Text -> Int
second s = head values * 10 + last values
where
values = second' s
main :: IO ()
main = do
input <- T.lines <$> readFile' "day01.in"
putStr "Q1: "
print . sum . map first $ input
putStr "Q2: "
print . sum . map second $ input

43
flake.lock generated Normal file
View File

@@ -0,0 +1,43 @@
{
"nodes": {
"nixpkgs": {
"locked": {
"lastModified": 1701445246,
"narHash": "sha256-SNnOV9h/rSZFuF/PZDbHRjgtOzPiP8jSKzN3oWT8Clg=",
"owner": "nixos",
"repo": "nixpkgs",
"rev": "9415b539cd02e4eb3446fd0ccaa405d110eff173",
"type": "github"
},
"original": {
"owner": "nixos",
"ref": "release-23.11",
"repo": "nixpkgs",
"type": "github"
}
},
"root": {
"inputs": {
"nixpkgs": "nixpkgs",
"utils": "utils"
}
},
"utils": {
"locked": {
"lastModified": 1667395993,
"narHash": "sha256-nuEHfE/LcWyuSWnS8t12N1wc105Qtau+/OdUAjtQ0rA=",
"owner": "numtide",
"repo": "flake-utils",
"rev": "5aed5285a952e0b949eb3ba02c12fa4fcfef535f",
"type": "github"
},
"original": {
"owner": "numtide",
"repo": "flake-utils",
"type": "github"
}
}
},
"root": "root",
"version": 7
}

31
flake.nix Normal file
View File

@@ -0,0 +1,31 @@
{
description = "AoC 2023 in Haskell";
inputs = {
nixpkgs.url = github:nixos/nixpkgs/release-23.11;
utils.url = github:numtide/flake-utils;
};
outputs = { self, nixpkgs, utils }:
utils.lib.eachDefaultSystem
(system:
let
pkgs = import nixpkgs {
inherit system;
};
src = ./.;
in
with pkgs; {
devShells.default = mkShell {
buildInputs = [
ghc
haskell-language-server
];
};
apps.default = {
type = "app";
program = "${ghc}/bin/runhaskell";
};
}
);
}