day1
This commit is contained in:
commit
f5b248dadc
13 changed files with 342 additions and 0 deletions
6
.dir-locals.el
Normal file
6
.dir-locals.el
Normal file
|
@ -0,0 +1,6 @@
|
|||
;;; Directory Local Variables
|
||||
;;; For more information see (info "(emacs) Directory Variables")
|
||||
|
||||
((haskell-mode
|
||||
(intero-targets "adventofcode:lib" "adventofcode:test:adventofcode-test")))
|
||||
|
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
.stack-work
|
60
.hlint.yaml
Normal file
60
.hlint.yaml
Normal file
|
@ -0,0 +1,60 @@
|
|||
# HLint configuration file
|
||||
# https://github.com/ndmitchell/hlint
|
||||
##########################
|
||||
|
||||
# This file contains a template configuration file, which is typically
|
||||
# placed as .hlint.yaml in the root of your project
|
||||
|
||||
|
||||
# Specify additional command line arguments
|
||||
#
|
||||
# - arguments: [--color, --cpp-simple, -XQuasiQuotes]
|
||||
|
||||
|
||||
# Control which extensions/flags/modules/functions can be used
|
||||
#
|
||||
# - extensions:
|
||||
# - default: false # all extension are banned by default
|
||||
# - name: [PatternGuards, ViewPatterns] # only these listed extensions can be used
|
||||
# - {name: CPP, within: CrossPlatform} # CPP can only be used in a given module
|
||||
#
|
||||
# - flags:
|
||||
# - {name: -w, within: []} # -w is allowed nowhere
|
||||
#
|
||||
# - modules:
|
||||
# - {name: [Data.Set, Data.HashSet], as: Set} # if you import Data.Set qualified, it must be as 'Set'
|
||||
# - {name: Control.Arrow, within: []} # Certain modules are banned entirely
|
||||
#
|
||||
# - functions:
|
||||
# - {name: unsafePerformIO, within: []} # unsafePerformIO can only appear in no modules
|
||||
|
||||
|
||||
# Add custom hints for this project
|
||||
#
|
||||
# Will suggest replacing "wibbleMany [myvar]" with "wibbleOne myvar"
|
||||
# - error: {lhs: "wibbleMany [x]", rhs: wibbleOne x}
|
||||
|
||||
|
||||
# Turn on hints that are off by default
|
||||
#
|
||||
# Ban "module X(module X) where", to require a real export list
|
||||
# - warn: {name: Use explicit module export list}
|
||||
#
|
||||
# Replace a $ b $ c with a . b $ c
|
||||
# - group: {name: dollar, enabled: true}
|
||||
#
|
||||
# Generalise map to fmap, ++ to <>
|
||||
# - group: {name: generalise, enabled: true}
|
||||
|
||||
|
||||
# Ignore some builtin hints
|
||||
# - ignore: {name: Use let}
|
||||
# - ignore: {name: Use const, within: SpecialModule} # Only within certain modules
|
||||
- ignore: {name: Use String}
|
||||
|
||||
# Define some custom infix operators
|
||||
# - fixity: infixr 3 ~^#^~
|
||||
|
||||
|
||||
# To generate a suitable file for HLint do:
|
||||
# $ hlint --default > .hlint.yaml
|
30
LICENSE
Normal file
30
LICENSE
Normal file
|
@ -0,0 +1,30 @@
|
|||
Copyright Yann Esposito (c) 2017
|
||||
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
* Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
|
||||
* Redistributions in binary form must reproduce the above
|
||||
copyright notice, this list of conditions and the following
|
||||
disclaimer in the documentation and/or other materials provided
|
||||
with the distribution.
|
||||
|
||||
* Neither the name of Yann Esposito nor the names of other
|
||||
contributors may be used to endorse or promote products derived
|
||||
from this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
1
README.md
Normal file
1
README.md
Normal file
|
@ -0,0 +1 @@
|
|||
# adventofcode
|
2
Setup.hs
Normal file
2
Setup.hs
Normal file
|
@ -0,0 +1,2 @@
|
|||
import Distribution.Simple
|
||||
main = defaultMain
|
62
adventofcode.cabal
Normal file
62
adventofcode.cabal
Normal file
|
@ -0,0 +1,62 @@
|
|||
-- This file has been generated from package.yaml by hpack version 0.17.1.
|
||||
--
|
||||
-- see: https://github.com/sol/hpack
|
||||
|
||||
name: adventofcode
|
||||
version: 0.1.0.0
|
||||
synopsis: Solutions for adventofcode
|
||||
homepage: https://github.com/yogsototh/adventofcode#readme
|
||||
bug-reports: https://github.com/yogsototh/adventofcode/issues
|
||||
license: BSD3
|
||||
license-file: LICENSE
|
||||
author: Yann Esposito
|
||||
maintainer: yann.esposito@gmail.com
|
||||
copyright: Copyright: © 2017 Yann Esposito
|
||||
category: Web
|
||||
build-type: Simple
|
||||
cabal-version: >= 1.10
|
||||
description: Please see the README on Github at <https://github.com/yogsototh/adventofcode#readme>
|
||||
|
||||
extra-source-files:
|
||||
README.md
|
||||
|
||||
source-repository head
|
||||
type: git
|
||||
location: https://github.com/yogsototh/adventofcode
|
||||
|
||||
library
|
||||
hs-source-dirs:
|
||||
src
|
||||
exposed-modules:
|
||||
Lib
|
||||
Day1
|
||||
other-modules:
|
||||
Paths_adventofcode
|
||||
build-depends:
|
||||
base >=4.7 && <5
|
||||
, protolude
|
||||
default-language: Haskell2010
|
||||
|
||||
executable adventofcode-exe
|
||||
hs-source-dirs:
|
||||
app
|
||||
main-is: Main.hs
|
||||
ghc-options: -threaded -rtsopts -with-rtsopts=-N
|
||||
build-depends:
|
||||
base
|
||||
, adventofcode
|
||||
default-language: Haskell2010
|
||||
|
||||
test-suite adventofcode-test
|
||||
type: exitcode-stdio-1.0
|
||||
hs-source-dirs:
|
||||
test
|
||||
main-is: Spec.hs
|
||||
build-depends:
|
||||
base
|
||||
, adventofcode
|
||||
, tasty
|
||||
, tasty-hunit
|
||||
, HUnit
|
||||
ghc-options: -threaded -rtsopts -with-rtsopts=-N
|
||||
default-language: Haskell2010
|
6
app/Main.hs
Normal file
6
app/Main.hs
Normal file
|
@ -0,0 +1,6 @@
|
|||
module Main where
|
||||
|
||||
import Lib
|
||||
|
||||
main :: IO ()
|
||||
main = someFunc
|
45
package.yaml
Normal file
45
package.yaml
Normal file
|
@ -0,0 +1,45 @@
|
|||
name: adventofcode
|
||||
version: '0.1.0.0'
|
||||
synopsis: Solutions for adventofcode
|
||||
description: Please see the README on Github at <https://github.com/yogsototh/adventofcode#readme>
|
||||
category: Web
|
||||
author: Yann Esposito
|
||||
maintainer: yann.esposito@gmail.com
|
||||
copyright: ! 'Copyright: © 2017 Yann Esposito'
|
||||
license: BSD3
|
||||
github: yogsototh/adventofcode
|
||||
extra-source-files:
|
||||
- README.md
|
||||
library:
|
||||
source-dirs: src
|
||||
exposed-modules:
|
||||
- Lib
|
||||
- Day1
|
||||
dependencies:
|
||||
- base >=4.7 && <5
|
||||
- protolude
|
||||
executables:
|
||||
adventofcode-exe:
|
||||
main: Main.hs
|
||||
source-dirs: app
|
||||
ghc-options:
|
||||
- -threaded
|
||||
- -rtsopts
|
||||
- -with-rtsopts=-N
|
||||
dependencies:
|
||||
- base
|
||||
- adventofcode
|
||||
tests:
|
||||
adventofcode-test:
|
||||
main: Spec.hs
|
||||
source-dirs: test
|
||||
ghc-options:
|
||||
- -threaded
|
||||
- -rtsopts
|
||||
- -with-rtsopts=-N
|
||||
dependencies:
|
||||
- base
|
||||
- adventofcode
|
||||
- tasty
|
||||
- tasty-hunit
|
||||
- HUnit
|
6
src/Lib.hs
Normal file
6
src/Lib.hs
Normal file
|
@ -0,0 +1,6 @@
|
|||
module Lib
|
||||
( someFunc
|
||||
) where
|
||||
|
||||
someFunc :: IO ()
|
||||
someFunc = putStrLn "someFunc"
|
35
src/day1.hs
Normal file
35
src/day1.hs
Normal file
|
@ -0,0 +1,35 @@
|
|||
{-# LANGUAGE NoImplicitPrelude #-}
|
||||
module Day1 where
|
||||
|
||||
import Protolude
|
||||
|
||||
import Data.List (tail, foldl')
|
||||
import Data.Char (ord)
|
||||
|
||||
ex1code = "29917128875332952564321392569634257121244516819997569284938677239676779378822158323549832814412597817651244117851771257438674567254146559419528411463781241159837576747416543451994579655175322397355255587935456185669334559882554936642122347526466965746273596321419312386992922582836979771421518356285534285825212798113159911272923448284681544657616654285632235958355867722479252256292311384799669645293812691169936746744856227797779513997329663235176153745581296191298956836998758194274865327383988992499115472925731787228592624911829221985925935268785757854569131538763133427434848767475989173579655375125972435359317237712667658828722623837448758528395981635746922144957695238318954845799697142491972626942976788997427135797297649149849739186827185775786254552866371729489943881272817466129271912247236569141713377483469323737384967871876982476485658337183881519295728697121462266226452265259877781881868585356333494916519693683238733823362353424927852348119426673294798416314637799636344448941782774113142925315947664869341363354235389597893211532745789957591898692253157726576488811769461354938575527273474399545366389515353657644736458182565245181653996192644851687269744491856672563885457872883368415631469696994757636288575816146927747179133188841148212825453859269643736199836818121559198563122442483528316837885842696283932779475955796132242682934853291737434482287486978566652161245555856779844813283979453489221189332412315117573259531352875384444264457373153263878999332444178577127433891164266387721116357278222665798584824336957648454426665495982221179382794158366894875864761266695773155813823291684611617853255857774422185987921219618596814446229556938354417164971795294741898631698578989231245376826359179266783767935932788845143542293569863998773276365886375624694329228686284863341465994571635379257258559894197638117333711626435669415976255967412994139131385751822134927578932521461677534945328228131973291962134523589491173343648964449149716696761218423314765168285342711137126239639867897341514131244859826663281981251614843274762372382114258543828157464392"
|
||||
|
||||
sum :: Num a => [a] -> a
|
||||
sum = foldl' (+) 0
|
||||
|
||||
charToInt :: Char -> Int
|
||||
charToInt c = ord c - ord '0'
|
||||
|
||||
solution1 :: [Char] -> Int
|
||||
solution1 code = code
|
||||
& cycle
|
||||
& tail
|
||||
& zip code
|
||||
& filter (uncurry (==))
|
||||
& map (charToInt . fst)
|
||||
& foldl' (+) 0
|
||||
|
||||
solution2 :: [Char] -> Int
|
||||
solution2 code =
|
||||
let n = length code `div` 2
|
||||
in code
|
||||
& cycle
|
||||
& drop n
|
||||
& zip code
|
||||
& filter (uncurry (==))
|
||||
& map (charToInt . fst)
|
||||
& foldl' (+) 0
|
66
stack.yaml
Normal file
66
stack.yaml
Normal file
|
@ -0,0 +1,66 @@
|
|||
# This file was automatically generated by 'stack init'
|
||||
#
|
||||
# Some commonly used options have been documented as comments in this file.
|
||||
# For advanced use and comprehensive documentation of the format, please see:
|
||||
# https://docs.haskellstack.org/en/stable/yaml_configuration/
|
||||
|
||||
# Resolver to choose a 'specific' stackage snapshot or a compiler version.
|
||||
# A snapshot resolver dictates the compiler version and the set of packages
|
||||
# to be used for project dependencies. For example:
|
||||
#
|
||||
# resolver: lts-3.5
|
||||
# resolver: nightly-2015-09-21
|
||||
# resolver: ghc-7.10.2
|
||||
# resolver: ghcjs-0.1.0_ghc-7.10.2
|
||||
# resolver:
|
||||
# name: custom-snapshot
|
||||
# location: "./custom-snapshot.yaml"
|
||||
resolver: lts-9.14
|
||||
|
||||
# User packages to be built.
|
||||
# Various formats can be used as shown in the example below.
|
||||
#
|
||||
# packages:
|
||||
# - some-directory
|
||||
# - https://example.com/foo/bar/baz-0.0.2.tar.gz
|
||||
# - location:
|
||||
# git: https://github.com/commercialhaskell/stack.git
|
||||
# commit: e7b331f14bcffb8367cd58fbfc8b40ec7642100a
|
||||
# - location: https://github.com/commercialhaskell/stack/commit/e7b331f14bcffb8367cd58fbfc8b40ec7642100a
|
||||
# extra-dep: true
|
||||
# subdirs:
|
||||
# - auto-update
|
||||
# - wai
|
||||
#
|
||||
# A package marked 'extra-dep: true' will only be built if demanded by a
|
||||
# non-dependency (i.e. a user package), and its test suites and benchmarks
|
||||
# will not be run. This is useful for tweaking upstream packages.
|
||||
packages:
|
||||
- .
|
||||
# Dependency packages to be pulled from upstream that are not in the resolver
|
||||
# (e.g., acme-missiles-0.3)
|
||||
extra-deps: []
|
||||
|
||||
# Override default flag values for local packages and extra-deps
|
||||
flags: {}
|
||||
|
||||
# Extra package databases containing global packages
|
||||
extra-package-dbs: []
|
||||
|
||||
# Control whether we use the GHC we find on the path
|
||||
# system-ghc: true
|
||||
#
|
||||
# Require a specific version of stack, using version ranges
|
||||
# require-stack-version: -any # Default
|
||||
# require-stack-version: ">=1.5"
|
||||
#
|
||||
# Override the architecture used by stack, especially useful on Windows
|
||||
# arch: i386
|
||||
# arch: x86_64
|
||||
#
|
||||
# Extra directories used by stack for building
|
||||
# extra-include-dirs: [/path/to/dir]
|
||||
# extra-lib-dirs: [/path/to/dir]
|
||||
#
|
||||
# Allow a newer minor version of GHC than the snapshot specifies
|
||||
# compiler-check: newer-minor
|
22
test/Spec.hs
Normal file
22
test/Spec.hs
Normal file
|
@ -0,0 +1,22 @@
|
|||
import Test.Tasty
|
||||
import Test.Tasty.HUnit
|
||||
|
||||
import qualified Day1
|
||||
|
||||
main :: IO ()
|
||||
main = defaultMain $
|
||||
testGroup "Day 1"
|
||||
[ testGroup "solution 1"
|
||||
[ testCase "1122 is 3" $ Day1.solution1 "1122" @?= 3
|
||||
, testCase "1111 is 4" $ Day1.solution1 "1111" @?= 4
|
||||
, testCase "1234 is 0" $ Day1.solution1 "1234" @?= 0
|
||||
, testCase "91212129 is 9" $ Day1.solution1 "91212129" @?= 9
|
||||
]
|
||||
, testGroup "solution 2"
|
||||
[ testCase "1212 is 6" $ Day1.solution2 "1212" @?= 6
|
||||
, testCase "1221 is 0" $ Day1.solution2 "1221" @?= 0
|
||||
, testCase "123425 is 0" $ Day1.solution2 "123425" @?= 4
|
||||
, testCase "123123 is 12" $ Day1.solution2 "123123" @?= 12
|
||||
, testCase "12131415 is 4" $ Day1.solution2 "12131415" @?= 4
|
||||
]
|
||||
]
|
Loading…
Reference in a new issue