Day 1! Off to a good start (I hope)

This commit is contained in:
Steph 2022-12-01 22:28:05 +01:00
parent 05460991c1
commit def733add9
6 changed files with 2310 additions and 1 deletions

View File

@ -32,3 +32,4 @@ executable aoc2022
build-depends: base ^>=4.16.3.0
hs-source-dirs: app
default-language: Haskell2010
other-modules: Day1.Main

32
2022/app/Day1/Main.hs Normal file
View File

@ -0,0 +1,32 @@
module Day1.Main (main) where
import Data.List
import System.IO
import Control.Monad
main :: IO ()
main = do
putStrLn "Day 1"
handle <- openFile "app/Day1/input" ReadMode
contents <- hGetContents handle
let r1 = part1 contents
putStrLn $ "part 1: " ++ show r1
let r2 = part2 contents
putStrLn $ "part 2: " ++ show r2
parseInput :: String -> [Int]
parseInput contents = map sum foodItems
where
input = lines contents
elves = foldr (\a b -> if a == "" then [] : b else (a : head b) : tail b) [[]] input
foodItems = (map . map) (read :: String -> Int) elves
part1 :: String -> Int
part1 = maximum . parseInput
part2 :: String -> Int
part2 = sum . (take 3) . reverse . sort . parseInput

2249
2022/app/Day1/input Normal file

File diff suppressed because it is too large Load Diff

View File

@ -1,4 +1,7 @@
module Main where
import qualified Day1.Main as Day1
main :: IO ()
main = putStrLn "Hello, Haskell!"
main = do
Day1.main

13
2022/cabal.config Normal file
View File

@ -0,0 +1,13 @@
remote-repo-cache: /build/.cabal/packages
world-file: /build/.cabal/world
extra-prog-path: /build/.cabal/bin
build-summary: /build/.cabal/logs/build.log
remote-build-reporting: none
jobs: $ncpus
installdir: /build/.cabal/bin
haddock
init
install-dirs user
install-dirs global
program-locations
program-default-options

View File

@ -21,5 +21,16 @@ pkgs.stdenv.mkDerivation rec {
if pkgs.stdenv.isLinux
then "${pkgs.glibcLocales}/lib/locale/locale-archive"
else "";
src = ./.;
# This currently doesn't work yet.
buildPhase = ''
export HOME=$TMP
mkdir -p $TMP/.cabal $out/bin
cp cabal.config $TMP/.cabal/config
cabal build
ln -s dist-newstyle/build/x86_64-linux/ghc-9.2.4/aoc2022-0.1.0.0/x/aoc2022/build/aoc2022/aoc2022 $out/bin/aoc2022
'';
}