From 3a599e34c0ddccec3e5c5d20a11fad920e4628c9 Mon Sep 17 00:00:00 2001 From: Neil Smith Date: Tue, 28 Nov 2017 23:40:18 +0000 Subject: [PATCH] Initial commit, using an old problem --- .gitignore | 44 +++++ README.html | 51 ++++++ README.md | 81 +++++++++ adventofcode17.sublime-project | 8 + adventofcode1701/README.md | 1 + adventofcode1701/Setup.hs | 2 + adventofcode1701/adventofcode1701.cabal | 26 +++ adventofcode1701/app/advent01.hs | 42 +++++ adventofcode1701/src/Main.hs | 5 + data/advent01.txt | 1 + modest.css | 219 ++++++++++++++++++++++++ stack.yaml | 73 ++++++++ 12 files changed, 553 insertions(+) create mode 100644 .gitignore create mode 100644 README.html create mode 100644 README.md create mode 100644 adventofcode17.sublime-project create mode 100644 adventofcode1701/README.md create mode 100644 adventofcode1701/Setup.hs create mode 100644 adventofcode1701/adventofcode1701.cabal create mode 100644 adventofcode1701/app/advent01.hs create mode 100644 adventofcode1701/src/Main.hs create mode 100644 data/advent01.txt create mode 100644 modest.css create mode 100644 stack.yaml diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6d84b97 --- /dev/null +++ b/.gitignore @@ -0,0 +1,44 @@ +# Extensionless files +* +!/**/ +!*.* + +# Haskell bits +dist +dist-* +cabal-dev +*.o +*.hi +*.chi +*.chs.h +*.dyn_o +*.dyn_hi +.hpc +.hsenv +.cabal-sandbox/ +cabal.sandbox.config +*.prof +*.aux +*.hp +*.eventlog +.stack-work/ +cabal.project.local +.HTF/ + +# A semelance of purity! +# IPython +.ipynb* +*.ipynb + +# Sublime text +*.sublime-workspace + +# Logs +*.log + +# Profile exports +*.ps + +# KDE +.directory + diff --git a/README.html b/README.html new file mode 100644 index 0000000..6f80db8 --- /dev/null +++ b/README.html @@ -0,0 +1,51 @@ + + + + + + + Advent of Code 2017 + + + + + +

Code to solve the Advent of Code puzzles. This year, I'm using the puzzles to develop my skills in Haskell.

+

Learn you a Haskell, Introduction to Haskell 98, and Hackage are good resources.

+

Toolchain

+

I'm using the basic Haskell Platform installation, togeher with Stack to manage the packages and dependencies (install with

+
$ sudo aptitude install haskell-platform haskell-stack
+

).

+

Creating the repository and project

+

Create the repository as normal: create the project in Gitolite, clone it, and insert the .gitignore and README.md files.

+

Within the project directory, there will be one package for each day. This will save time waiting for stack to check every executable before compiling what's changed. Each package needs a separate directory tree and a separate .cabal file.

+

Creating the first package

+

Then create the basic stack project with it. This will create a new directory. Note that this new directory name can't have a hyphen-delimited word that's just digits, so the initial project will have to be adventofcode1701

+
stack new adventofcode1701 simple
+

This project will be demoted to being a package, but one that will hold the overall project.

+

Then create the top-level stack.yaml file to hold the overall project information.

+
stack init
+

Modify this top-level stack.yaml file as needed, such as adding the ghc-options stanza. You can then delete adventofcode1701/stack.yaml.

+

Creating subsequent packages

+

Each package needs a separate directory tree and a separate .cabal file.

+

To work on a project, cd into that project's directory.

+

Compile with

+
stack build
+

or

+
stack build adventofcode1701
+

Run with

+
stack exec advent01
+

Run interactively with

+
stack ghci adventofcode1701:exe:advent01
+

To profile, use

+
stack build --executable-profiling --library-profiling --ghc-options="-fprof-auto -rtsopts" adventofcode1601
+

then run with

+
stack exec -- advent01 +RTS -p -hy
+

Readme

+

Build this readme file wth

+
pandoc -s README.md > README.html
+

(Using the Modest style.)

+ + diff --git a/README.md b/README.md new file mode 100644 index 0000000..a6eb1f7 --- /dev/null +++ b/README.md @@ -0,0 +1,81 @@ +--- +title: "Advent of Code 2017" +output: html_document +css: modest.css +--- +Code to solve the [Advent of Code](http://adventofcode.com/2017/) puzzles. This year, I'm using the puzzles to develop my skills in [Haskell](https://wiki.haskell.org/Haskell). + +[Learn you a Haskell](http://learnyouahaskell.com/chapters), [Introduction to Haskell 98](https://www.haskell.org/tutorial/index.html), and [Hackage](https://hackage.haskell.org/) are good resources. + +# Toolchain + +I'm using the basic Haskell Platform installation, togeher with `Stack` to manage the packages and dependencies (install with +``` +$ sudo aptitude install haskell-platform haskell-stack +``` +). + +## Creating the repository and project +Create the repository as normal: create the project in Gitolite, clone it, and insert the `.gitignore` and `README.md` files. + +Within the project directory, there will be one package for each day. This will save time waiting for `stack` to check every executable before compiling what's changed. Each package needs a separate directory tree and a separate `.cabal` file. + +## Creating the first package +Then create the basic `stack` project with it. This will create a new directory. Note that this new directory name can't have a hyphen-delimited word that's just digits, so the initial project will have to be `adventofcode1701` + +``` +stack new adventofcode1701 simple +``` + +This project will be demoted to being a package, but one that will hold the overall project. + +Then create the top-level `stack.yaml` file to hold the overall project information. + +``` +stack init +``` + +Modify this top-level `stack.yaml` file as needed, such as adding the `ghc-options` stanza. You can then delete `adventofcode1701/stack.yaml`. + +## Creating subsequent packages + +Each package needs a separate directory tree and a separate `.cabal` file. + +To work on a project, `cd` into that project's directory. + +Compile with +``` +stack build +``` +or +``` +stack build adventofcode1701 +``` + +Run with +``` +stack exec advent01 +``` + +Run interactively with +``` +stack ghci adventofcode1701:exe:advent01 +``` + +To profile, use +``` +stack build --executable-profiling --library-profiling --ghc-options="-fprof-auto -rtsopts" adventofcode1601 +``` +then run with +``` +stack exec -- advent01 +RTS -p -hy +``` + +# Readme + +Build this readme file wth +``` +pandoc -s README.md > README.html +``` + +(Using the [Modest style](https://github.com/markdowncss/modest).) diff --git a/adventofcode17.sublime-project b/adventofcode17.sublime-project new file mode 100644 index 0000000..24db303 --- /dev/null +++ b/adventofcode17.sublime-project @@ -0,0 +1,8 @@ +{ + "folders": + [ + { + "path": "." + } + ] +} diff --git a/adventofcode1701/README.md b/adventofcode1701/README.md new file mode 100644 index 0000000..5c53720 --- /dev/null +++ b/adventofcode1701/README.md @@ -0,0 +1 @@ +# adventofcode1701 diff --git a/adventofcode1701/Setup.hs b/adventofcode1701/Setup.hs new file mode 100644 index 0000000..9a994af --- /dev/null +++ b/adventofcode1701/Setup.hs @@ -0,0 +1,2 @@ +import Distribution.Simple +main = defaultMain diff --git a/adventofcode1701/adventofcode1701.cabal b/adventofcode1701/adventofcode1701.cabal new file mode 100644 index 0000000..f7c8fc6 --- /dev/null +++ b/adventofcode1701/adventofcode1701.cabal @@ -0,0 +1,26 @@ +name: adventofcode1701 +version: 0.1.0.0 +-- synopsis: +-- description: +homepage: https://github.com/neilnjae/advent-of-code-17#readme +license: BSD3 +license-file: LICENSE +author: Neil Smith +maintainer: noone@njae.me.uk +copyright: 2017 Neil Smith +category: None +build-type: Simple +cabal-version: >=1.10 +extra-source-files: README.md + +library + hs-source-dirs: src + build-depends: base >= 4.7 && < 5 + default-language: Haskell2010 + +executable advent01 + hs-source-dirs: app + main-is: advent01.hs + default-language: Haskell2010 + build-depends: base >= 4.7 && < 5 + , parsec \ No newline at end of file diff --git a/adventofcode1701/app/advent01.hs b/adventofcode1701/app/advent01.hs new file mode 100644 index 0000000..d6d3782 --- /dev/null +++ b/adventofcode1701/app/advent01.hs @@ -0,0 +1,42 @@ +{-# LANGUAGE NegativeLiterals #-} + +module Main(main) where + +import Text.Parsec hiding (State) +-- import Text.ParserCombinators.Parsec.Number + + + +main :: IO () +main = do + text <- readFile "data/advent01.txt" + let instructions = successfulParse $ parseIline text + part1 instructions + part2 instructions + +part1 :: [Int] -> IO () +part1 instructions = do + print $ sum instructions + +part2 :: [Int] -> IO () +part2 instructions = do + print $ length $ takeWhile (> -1) $ scanl (+) 0 instructions + + + +-- instructionFile = instructionLine `endBy` newline +instructionLine = many (up <|> down) + + +up = char '(' *> pure 1 +down = char ')' *> pure -1 + +-- parseIfile :: String -> Either ParseError [[Int]] +-- parseIfile input = parse instructionFile "(unknown)" input + +parseIline :: String -> Either ParseError [Int] +parseIline input = parse instructionLine "(unknown)" input + +successfulParse :: Either ParseError [a] -> [a] +successfulParse (Left _) = [] +successfulParse (Right a) = a diff --git a/adventofcode1701/src/Main.hs b/adventofcode1701/src/Main.hs new file mode 100644 index 0000000..9cd992d --- /dev/null +++ b/adventofcode1701/src/Main.hs @@ -0,0 +1,5 @@ +module Main where + +main :: IO () +main = do + putStrLn "hello world" diff --git a/data/advent01.txt b/data/advent01.txt new file mode 100644 index 0000000..2d4ad8f --- /dev/null +++ b/data/advent01.txt @@ -0,0 +1 @@ +()(((()))(()()()((((()(((())(()(()((((((()(()(((())))((()(((()))((())(()((()()()()(((())(((((((())))()()(()(()(())(((((()()()((())(((((()()))))()(())(((())(())((((((())())))(()())))()))))()())()())((()()((()()()()(()((((((((()()())((()()(((((()(((())((())(()))()((((()((((((((())()((()())(())((()))())((((()())(((((((((((()()(((((()(()))())(((()(()))())((()(()())())())(()(((())(())())()()(()(()((()))((()))))((((()(((()))))((((()(()(()())())()(((()((((())((((()(((()()(())()()()())((()((((((()((()()))()((()))()(()()((())))(((()(((()))((()((()(()))(((()()(()(()()()))))()()(((()(((())())))))((()(((())()(()(())((()())))((((())))(()(()(()())()((()())))(((()((()(())()()((()((())(()()((())(())()))()))((()(())()))())(((((((()(()()(()(())())))))))(()((((((())((((())((())())(()()))))()(())(()())()())((())(()))))(()))(()((()))()(()((((((()()()()((((((((()(()(())((()()(()()))(())()())()((())))()))()())(((()))(())()(())()))()((()((()(()()())(())()()()((())())))((()()(()()((()(())()()())(((()(()()))))(())))(()(()())()))()()))))))()))))((((((())))())))(()(())())(()())))))(()))()))))))()((()))))()))))(()(()((()())())(()()))))(((())()))())())())(((()(()()))(())()(())(())((((((()()))))((()(()))))))(()))())(((()()(()))()())()()()())))))))))))))(())(()))(()))((()(())(()())(())())(()())(())()()(()())))()()()))(())())()))())())(())((())))))))(())))(())))))()))))((())(()(((()))))(()))()((()(())))(()())(((((()))()())()()))))()))))()))())(()(()()()))()))))))((()))))))))))()((()))((()(())((())()()(()()))()(()))))()()(()))()))(((())))(())()((())(())(()())()())())))))))())))()((())))()))(()))()()))(((((((()))())(()()))(()()(()))()(()((()())()))))))(((()()()())))(())()))()())(()()))()()))))))))(())))()))()()))))))()))()())))()(())(())))))()(())()()(()()))))())((()))))()))))(()(((((()))))))))())))())()(())()()))))(())))())()()())()()())()(()))))()))()))))))))())))((()))()))()))())))()())()()())))())))(()((())()((()))())))))())()(())((())))))))))))())()())(())())())(()))(()))()))())(()(())())()())()()(()))))(()(())))))))(())))())(())))))))())()()(())())())))(())))))()))()(()())()(()))())())))))()()(()))()))))())))))))))()))))()))))))())()())()()))))()())))())))))))))))()()))))()()(((()))()()(())()))))((()))))(()))(())())))(())()))))))(()))()))))(())())))))()))(()())))))))))))))())))))))))()((()())(()())))))))((()))))(())(())))()(()())())))())())(()()()())))()))))))())))))())()()())))))))))))()()(()))))()())()))((()())(()))))()(()))))))))))()())())(((())(()))))())()))()))()))))))()))))))(()))))()))))()(())))(())))(()))())()()(()()))()))(()()))))))))()))(()))())(()()(()(()())()()))()))))))))(())))))((()()(()))())())))))()))())(()())()()))())))()(()()()()))((())())))())()(()()))()))))))))(()))(())))()))))(()(()())(()))))()())())()))()()))())))))))))))())()))))))()))))))))())))))()))))())(()())))(())()))())())))))()()(()()())(()())))()()))(((()))(()()()))))()))))()))))((())))()((((((()()))))))())))))))))))(((()))))))))))))(())())))))())(()))))))(()))((()))())))()(()((()))()))()))))))))))())()))()(()()))))())))())(())()(()))()))())(()))()))))(()()))()()(())))))()))(())(()(()()))(()()())))))(((()))))))()))))))))))))(())(()))))()())())()()((()()))())))))(()))))())))))))()()()))))))))())))()(((()()))(())))))(((())())))))((()))()(()))(()))))(()())))(()))())))))()))))(())(())))()((()))(())())))()()))()))))))))()))(()()()(()()()(()))())(())()())(((()))(())))))))))(((()())))()()))))))))()(())(()))()((((())(())(()())))()))(((())()()()))((()))(()))())())))())))(()))())()())())(()(())())()()()(())))())(())))(())))(())()))()))(()((()))))))))())(()))))))())(()()))()()))()(()(()())))()()(()((()((((((()))(())))()()()))())()))((()()(()))())((()(()(()))(()()))))()())))()))()())))))))()()((()())(())))()))(()))(())(()))())(()(())))()()))))))(((()(((()()))()(()(())())((()()))()))()))()))()(()()()(()))((()())()(())))()()))(((())()()())(())()((()()()()(()(())(()()))()(((((()())))((())))))(()()()))))(((()(())))()))((()((()(())()(()((())))((()())()(()))(((()())()()(()))(())(((()((()())()((())()())(((()()))((()((())(()))(()())(()()()))((()))(())(()((()()())((()))(())))(())(())(())))(()())))(((((()(()(((((()())((((()(()())(())(()()(((())((()(((()()(((()()((((((())))())(()((((((()(()))()))()()((()((()))))()(()()(()((()()))))))(((((()(((((())()()()(())())))))))()))((()()(())))(())(()()()())))))(()((((())))))))()()(((()(()(()(()(()())()()()(((((((((()()())()(()))((()()()()()(((((((()())()((())()))((((((()(()(()(()())(((()(((((((()(((())(((((((((())(())())()))((()(()))(((()()())(())(()(()()(((()(())()))())))(())((((((())(()()())()()(((()(((())(()(((())(((((((()(((((((((()))(())(()(()(()))))((()))()(())())())((()(()((()()))((()()((()(())(())(()((())(((())(((()()()((((((()()(())((((())()))))(())((()(()((())))(((((()(()()())())((())())))((())((()((()()((((((())(((()()(()())())(()(()))(()(()))())())()(((((((()(((()(())()()((())((()(()()((()(()()(((((((((((())((())((((((())((()((((()(()((((()(((((((())()((()))))())()((()((((()(()(((()((()())))(())())(((()(((())((((((()(((((((((()()(())))(()(((((()((((()())))((()((()((()(()()(((())((((((((((((()(((())(()(((((()))(()()(()()()()()()((())(((((((())(((((())))))())()(()()(()(()(((()()(((((())(()((()((()(((()()((()((((())()))()((((())(())))()())(((())(())(()()((()(((()()((((((((((()()(()())())(((((((((())((((()))()()((((())(()((((()(((())())(((((((((((()((((())))(())(()(((()(((()((())(((((()((()()(()(()()((((((()((((()((()(()((()(()((((((()))))()()(((((()((()(()(())()))(())(((((((()((((()())(()((()((()(()))())))(())((()))))(((((((()()()())(()))(()()((()())()((()((()()()(()(()()))(()())(())(((((()(((((((((((()((()(((()(((((((()()((((((()(((((()(()((()(((((())((((((()))((((())((()()((())(((())()(((((()()(((((()((()(()(((((((()(((((()((()((()((())(())((())(()))()()))(()()(()(()()(((((((()(((()(((())()(((((()((((((()())((((())()((()((()(()()())(()))((((()()((((((()((()(()(()((((()((()((())((((((()(()(())((((((()((((((((((()((())()))()(()(()(((((()()()))((())))()(()((((((((((((((()(((()((((()((())((()((()(((()()(()(((()((())(()()())))()(()(()(((((()()(()(()((((()(((((())()(()(()))(((((()()(((()()(())((((((((((((((())((())(((((((((((())()()()(())()(()(()(((((((((())(((()))(()()())(()((((()(())(((((()())(())((((((((())()((((()((((((())(()((()(())(((()((((()))(((((((((()()))((((()(())()()()(())(()((())((()()))()(((())(((((())((((((()()))(((((((((()((((((())))(((((((()((()(()(())))())(()(()))()(((((()())(()))()(()(())(((()))))())()())))(((((()))())()((()(()))))((()()()((((((()))()()((((((((())((()(()(((()(()((())((()())(()((((())(()(((()()()(()(()()))())())((((((((((())())((()))()((())(())(())))())()(()()(())))())(()))(((()(()()(((()(((())))()(((()(())()((((((())()))()))()((((((()(()(((((()())))()))))())()()(((()(((((())((()()(()((()((()(()(()(())))(()()()()((()(())(((()((()))((((()))())(())))())(()))()()()())()))(((()()())()((())))(())(()()()()(()())((()(()()((((())))((()((()(())((()(()((())()(()()(((()())()()())((()))((())(((()()(())))()()))(((()((())()(((((()())(())((())()())())((((((()(()(((((()))(()( diff --git a/modest.css b/modest.css new file mode 100644 index 0000000..947a9ea --- /dev/null +++ b/modest.css @@ -0,0 +1,219 @@ +@media print { + *, + *:before, + *:after { + background: transparent !important; + color: #000 !important; + box-shadow: none !important; + text-shadow: none !important; + } + + a, + a:visited { + text-decoration: underline; + } + + a[href]:after { + content: " (" attr(href) ")"; + } + + abbr[title]:after { + content: " (" attr(title) ")"; + } + + a[href^="#"]:after, + a[href^="javascript:"]:after { + content: ""; + } + + pre, + blockquote { + border: 1px solid #999; + page-break-inside: avoid; + } + + thead { + display: table-header-group; + } + + tr, + img { + page-break-inside: avoid; + } + + img { + max-width: 100% !important; + } + + p, + h2, + h3 { + orphans: 3; + widows: 3; + } + + h2, + h3 { + page-break-after: avoid; + } +} + +pre, +code { + font-family: Menlo, Monaco, "Courier New", monospace; +} + +pre { + padding: .5rem; + line-height: 1.25; + overflow-x: scroll; +} + +a, +a:visited { + color: #3498db; +} + +a:hover, +a:focus, +a:active { + color: #2980b9; +} + +.modest-no-decoration { + text-decoration: none; +} + +html { + font-size: 12px; +} + +@media screen and (min-width: 32rem) and (max-width: 48rem) { + html { + font-size: 15px; + } +} + +@media screen and (min-width: 48rem) { + html { + font-size: 16px; + } +} + +body { + line-height: 1.85; +} + +p, +.modest-p { + font-size: 1rem; + margin-bottom: 1.3rem; +} + +h1, +.modest-h1, +h2, +.modest-h2, +h3, +.modest-h3, +h4, +.modest-h4 { + margin: 1.414rem 0 .5rem; + font-weight: inherit; + line-height: 1.42; +} + +h1, +.modest-h1 { + margin-top: 0; + font-size: 3.998rem; +} + +h2, +.modest-h2 { + font-size: 2.827rem; +} + +h3, +.modest-h3 { + font-size: 1.999rem; +} + +h4, +.modest-h4 { + font-size: 1.414rem; +} + +h5, +.modest-h5 { + font-size: 1.121rem; +} + +h6, +.modest-h6 { + font-size: .88rem; +} + +small, +.modest-small { + font-size: .707em; +} + +/* https://github.com/mrmrs/fluidity */ + +img, +canvas, +iframe, +video, +svg, +select, +textarea { + max-width: 100%; +} + +@import url(http://fonts.googleapis.com/css?family=Open+Sans+Condensed:300,300italic,700); + +@import url(http://fonts.googleapis.com/css?family=Arimo:700,700italic); + +html { + font-size: 18px; + max-width: 100%; +} + +body { + color: #444; + font-family: 'Open Sans Condensed', sans-serif; + font-weight: 300; + margin: 0 auto; + max-width: 48rem; + line-height: 1.45; + padding: .25rem; +} + +h1, +h2, +h3, +h4, +h5, +h6 { + font-family: Arimo, Helvetica, sans-serif; +} + +h1, +h2, +h3 { + border-bottom: 2px solid #fafafa; + margin-bottom: 1.15rem; + padding-bottom: .5rem; + text-align: center; +} + +blockquote { + border-left: 8px solid #fafafa; + padding: 1rem; +} + +pre, +code { + background-color: #fafafa; +} \ No newline at end of file diff --git a/stack.yaml b/stack.yaml new file mode 100644 index 0000000..3993b53 --- /dev/null +++ b/stack.yaml @@ -0,0 +1,73 @@ +# 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: +# http://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: +- adventofcode1701/ +# 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.1" +# +# 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 + +# Include GHC options +ghc-options: + # All packages + "*": -O2 -Wall -Wno-missing-signatures -threaded -rtsopts -with-rtsopts=-N + # for some package: + # some-package: -DSOME_CPP_FLAG -- 2.34.1