Browse Source

Use stack as build tool

master
Nicolas Lenz 8 months ago
parent
commit
ebb315dea7
7 changed files with 101 additions and 39 deletions
  1. 0
    39
      01.hs
  2. 37
    0
      advent-of-code.cabal
  3. 0
    0
      day01.input
  4. 27
    0
      package.yaml
  5. 23
    0
      src/Day01.hs
  6. 10
    0
      src/Main.hs
  7. 4
    0
      stack.yaml

+ 0
- 39
01.hs View File

@@ -1,39 +0,0 @@
import System.Environment

main :: IO ()
main = do
args <- getArgs
case args of
["part1"] -> part1
["part2"] -> part2
_ -> putStrLn "gief part"

part1 :: IO ()
part1 = do
freqChanges <- readFreqChanges
print $ last $ applySeq freqChanges 0

part2 :: IO ()
part2 = do
freqChanges <- readFreqChanges
print $ firstTwice $ applySeq (cycle freqChanges) 0

readFreqChanges :: IO [Integer -> Integer]
readFreqChanges = do
input <- readFile "01.input"
return (toFunction <$> lines input)

toFunction :: String -> (Integer -> Integer)
toFunction ('+':as) = (+(read as))
toFunction ('-':as) = (\x -> x - read as)

applySeq :: [a -> a] -> a -> [a]
applySeq [] start = [start]
applySeq (f:fs) start = start : applySeq fs (f start)

firstTwice :: Eq a => [a] -> Maybe a
firstTwice as = loop as [] where
loop [] found = Nothing
loop todo@(x:xs) found
| x `elem` found = Just x
| otherwise = loop xs (x:found)

+ 37
- 0
advent-of-code.cabal View File

@@ -0,0 +1,37 @@
cabal-version: 1.12

-- This file has been generated from package.yaml by hpack version 0.31.1.
--
-- see: https://github.com/sol/hpack
--
-- hash: ec04a239f02e42ba3551e0a7529c4268de82830c0633716ed9ffd4c2ff152247

name: advent-of-code
version: 0.0.0
synopsis: My solutions for the Advent of Code
description: Please see the README at <https://git.eisfunke.com/research/advent-of-code#readme>
category: Puzzle
author: Nicolas Lenz
maintainer: nicolas@eisfunke.com
copyright: 2018 Nicolas Lenz
license: MIT
license-file: LICENSE
build-type: Simple
extra-source-files:
README.md

source-repository head
type: git
location: https://git.eisfunke.com/research/advent-of-code

executable advent-of-code
main-is: Main.hs
other-modules:
Day01
Paths_advent_of_code
hs-source-dirs:
src
ghc-options: -O2 -threaded -rtsopts -with-rtsopts=-N
build-depends:
base >=4.7 && <5
default-language: Haskell2010

01.input → day01.input View File


+ 27
- 0
package.yaml View File

@@ -0,0 +1,27 @@
name: advent-of-code
version: 0.0.0
license: MIT
git: "https://git.eisfunke.com/research/advent-of-code"
author: "Nicolas Lenz"
maintainer: "nicolas@eisfunke.com"
copyright: "2018 Nicolas Lenz"

extra-source-files:
- README.md

synopsis: My solutions for the Advent of Code
category: Puzzle
description: Please see the README at <https://git.eisfunke.com/research/advent-of-code#readme>

dependencies:
- base >= 4.7 && < 5

executables:
advent-of-code:
main: Main.hs
source-dirs: src
ghc-options:
- -O2
- -threaded
- -rtsopts
- -with-rtsopts=-N

+ 23
- 0
src/Day01.hs View File

@@ -0,0 +1,23 @@
module Day01 (part1, part2) where

part1 :: [String] -> IO ()
part1 input = print $ last $ applySeq (toFunction <$> input) 0

part2 :: [String] -> IO ()
part2 input = print $ firstTwice $ applySeq (cycle (toFunction <$> input)) 0


toFunction :: String -> (Integer -> Integer)
toFunction ('+':as) = (+(read as))
toFunction ('-':as) = (\x -> x - read as)

applySeq :: [a -> a] -> a -> [a]
applySeq [] start = [start]
applySeq (f:fs) start = start : applySeq fs (f start)

firstTwice :: Eq a => [a] -> Maybe a
firstTwice as = loop as [] where
loop [] found = Nothing
loop todo@(x:xs) found
| x `elem` found = Just x
| otherwise = loop xs (x:found)

+ 10
- 0
src/Main.hs View File

@@ -0,0 +1,10 @@
import System.Environment
import Day01

main :: IO ()
main = do
args <- getArgs
case args of
["01-1"] -> readFile "day01.input" >>= Day01.part1 . lines
["01-2"] -> readFile "day01.input" >>= Day01.part1 . lines
_ -> putStrLn "Example call: advent-of-code 01-1 for day 01, part 1\nInput should be named like day01.input."

+ 4
- 0
stack.yaml View File

@@ -0,0 +1,4 @@
resolver: lts-12.19

packages:
- .

Loading…
Cancel
Save