feat: Set project up and implement day1 part1
This commit is contained in:
commit
022cf2a609
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
|
@ -0,0 +1,2 @@
|
|||
dist/
|
||||
dist-*/
|
20
LICENSE
Normal file
20
LICENSE
Normal file
|
@ -0,0 +1,20 @@
|
|||
Copyright (c) 2024 kalmenn
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining
|
||||
a copy of this software and associated documentation files (the
|
||||
"Software"), to deal in the Software without restriction, including
|
||||
without limitation the rights to use, copy, modify, merge, publish,
|
||||
distribute, sublicense, and/or sell copies of the Software, and to
|
||||
permit persons to whom the Software is furnished to do so, subject to
|
||||
the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included
|
||||
in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
||||
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
||||
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
||||
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
41
day1/Main.hs
Normal file
41
day1/Main.hs
Normal file
|
@ -0,0 +1,41 @@
|
|||
module Main where
|
||||
|
||||
import System.Environment (getArgs)
|
||||
import System.IO (Handle, hIsEOF, hGetLine, withFile, IOMode(ReadMode))
|
||||
import Text.Read (readMaybe)
|
||||
|
||||
import Data.List (sort)
|
||||
import Data.Char (isDigit, isSpace)
|
||||
|
||||
mapSnd :: (b -> c) -> (a, b) -> (a, c)
|
||||
mapSnd f (a, b) = (a, f b)
|
||||
|
||||
parseLine :: String -> IO (Int, Int)
|
||||
parseLine line = let (l, (_, r)) = (mapSnd (span isSpace). span isDigit) line
|
||||
in
|
||||
case readMaybe l >>= \l' -> readMaybe r
|
||||
>>= \r' -> return (l', r') of
|
||||
Just a -> return a
|
||||
Nothing -> fail "Input file is incorrectly formatted"
|
||||
|
||||
readLists :: Handle -> IO ([Int], [Int])
|
||||
readLists h = do
|
||||
shouldStop <- hIsEOF h
|
||||
if shouldStop
|
||||
then return ([], [])
|
||||
else do
|
||||
line <- hGetLine h
|
||||
(l, r) <- parseLine line
|
||||
(ls, rs) <- readLists h
|
||||
return (l:ls, r:rs)
|
||||
|
||||
computeDistance :: [Int] -> [Int] -> Int
|
||||
computeDistance l r = sum (zipWith (curry (abs . uncurry (-))) l r)
|
||||
|
||||
main :: IO ()
|
||||
main = do
|
||||
args <- getArgs
|
||||
(fp:_) <- return args
|
||||
(l, r) <- withFile fp ReadMode readLists
|
||||
|
||||
putStrLn ("Solution (Part 1) : " ++ show (computeDistance (sort l) (sort r)))
|
1000
day1/puzzle_input
Normal file
1000
day1/puzzle_input
Normal file
File diff suppressed because it is too large
Load diff
6
day1/test_input
Normal file
6
day1/test_input
Normal file
|
@ -0,0 +1,6 @@
|
|||
3 4
|
||||
4 3
|
||||
2 5
|
||||
1 3
|
||||
3 9
|
||||
3 3
|
27
flake.lock
Normal file
27
flake.lock
Normal file
|
@ -0,0 +1,27 @@
|
|||
{
|
||||
"nodes": {
|
||||
"nixpkgs": {
|
||||
"locked": {
|
||||
"lastModified": 1733015953,
|
||||
"narHash": "sha256-t4BBVpwG9B4hLgc6GUBuj3cjU7lP/PJfpTHuSqE+crk=",
|
||||
"owner": "nixos",
|
||||
"repo": "nixpkgs",
|
||||
"rev": "ac35b104800bff9028425fec3b6e8a41de2bbfff",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
"owner": "nixos",
|
||||
"ref": "nixos-unstable",
|
||||
"repo": "nixpkgs",
|
||||
"type": "github"
|
||||
}
|
||||
},
|
||||
"root": {
|
||||
"inputs": {
|
||||
"nixpkgs": "nixpkgs"
|
||||
}
|
||||
}
|
||||
},
|
||||
"root": "root",
|
||||
"version": 7
|
||||
}
|
26
flake.nix
Normal file
26
flake.nix
Normal file
|
@ -0,0 +1,26 @@
|
|||
{
|
||||
description = "Advent Of Code 2024";
|
||||
|
||||
inputs = {
|
||||
nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable";
|
||||
};
|
||||
|
||||
outputs = { self, nixpkgs }: let
|
||||
supportedSystems = [
|
||||
"x86_64-linux" "aarch64-linux" "x86_64-linux" "aarch64-darwin"
|
||||
];
|
||||
forAllSystems = nixpkgs.lib.genAttrs supportedSystems;
|
||||
in {
|
||||
devShells = forAllSystems (system: let
|
||||
pkgs = nixpkgs.legacyPackages.${system};
|
||||
in {
|
||||
default = pkgs.mkShell {
|
||||
packages = with pkgs; [
|
||||
ghc
|
||||
cabal-install
|
||||
haskellPackages.haskell-language-server
|
||||
];
|
||||
};
|
||||
});
|
||||
};
|
||||
}
|
20
kalmennAOC2024.cabal
Normal file
20
kalmennAOC2024.cabal
Normal file
|
@ -0,0 +1,20 @@
|
|||
cabal-version: 3.0
|
||||
name: kalmennAOC2024
|
||||
version: 0.1.0.0
|
||||
license: MIT
|
||||
license-file: LICENSE
|
||||
author: kalmenn
|
||||
maintainer: kale@kalmenn.fr
|
||||
category: Challenge
|
||||
build-type: Simple
|
||||
extra-doc-files: CHANGELOG.md
|
||||
|
||||
common warnings
|
||||
ghc-options: -Wall
|
||||
|
||||
executable day1
|
||||
import: warnings
|
||||
main-is: Main.hs
|
||||
build-depends: base ^>=4.18.2.1
|
||||
hs-source-dirs: day1
|
||||
default-language: Haskell2010
|
Loading…
Reference in a new issue