2 <html xmlns=
"http://www.w3.org/1999/xhtml" lang=
"" xml:
lang=
"">
4 <meta charset=
"utf-8" />
5 <meta name=
"generator" content=
"pandoc" />
6 <meta name=
"viewport" content=
"width=device-width, initial-scale=1.0, user-scalable=yes" />
7 <title>Advent of Code
2020</title>
8 <style type=
"text/css">
9 code{white-space: pre-wrap;}
10 span.smallcaps{font-variant: small-caps;}
11 span.underline{text-decoration: underline;}
12 div.column{display: inline-block; vertical-align: top; width:
50%;}
14 <link rel=
"stylesheet" href=
"modest.css" />
16 <script src="//cdnjs.cloudflare.com/ajax/libs/html5shiv/3.7.3/html5shiv-printshiv.min.js"></script>
21 <h1 class=
"title">Advent of Code
2020</h1>
23 <p>Code to solve the
<a href=
"http://adventofcode.com/2020/">Advent of Code
</a> puzzles. This year, I’m using the puzzles to develop my skills in
<a href=
"https://wiki.haskell.org/Haskell">Haskell
</a>. I’m writing up a
<a href=
"https://work.njae.me.uk/tag/advent-of-code/">commentary on these puzzles and my solutions
</a> on my blog.
</p>
24 <p><a href=
"http://learnyouahaskell.com/chapters">Learn you a Haskell
</a>,
<a href=
"https://www.haskell.org/tutorial/index.html">Introduction to Haskell
98</a>, and
<a href=
"https://hackage.haskell.org/">Hackage
</a> are good resources.
</p>
25 <p>The
<a href=
"https://docs.haskellstack.org/en/stable/README/">Stack documentation
</a> and
<a href=
"http://howistart.org/posts/haskell/1/">How I Start: Haskell
</a> are good sources of using the tools.
</p>
26 <h1 id=
"toolchain">Toolchain
</h1>
27 <p>I’m using the basic Haskell Platform installation, together with
<code>stack
</code> to manage the packages and dependencies (install with
</p>
28 <pre><code>$ sudo aptitude install haskell-platform haskell-stack
</code></pre>
29 <p>), then updgrade with
</p>
30 <pre><code> stack upgrade --binary-only
</code></pre>
31 <p>as the version in the Ubuntu repos is too old to work with current Haskell Stack package sets.
</p>
32 <h2 id=
"creating-the-repository-and-project">Creating the repository and project
</h2>
33 <p>Create the repository as normal: create the project in Gitolite, clone it, and insert the
<code>.gitignore
</code> and
<code>README.md
</code> files.
</p>
34 <p>There’s one package per day, with the code for each package in sub-directories of the root directory.
</p>
35 <p>Create the basic
<code>stack
</code> project. 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 project will have to be
<code>advent-of-code
</code></p>
36 <pre><code>stack new advent-of-code --bare simple
</code></pre>
37 <p>Modify the
<code>stack.yaml
</code> file as needed, such as adding the
<code>ghc-options
</code> stanza.
</p>
38 <h2 id=
"creating-subsequent-days">Creating subsequent days
</h2>
39 <p>Each day lives in a separate directory, with its own
<code>package.yaml
</code> file and code in the
<code>src
</code> directory. (I based this configuration from
<a href=
"https://github.com/mstksg/advent-of-code-2018">mstksg’s setup
</a>.)
</p>
41 <pre><code>stack build
</code></pre>
43 <pre><code>stack build advent01
</code></pre>
45 <pre><code>stack exec advent01
</code></pre>
46 <p>If you want to pass in additional RTS parameters, do it like this:
</p>
47 <pre><code>stack exec -- advent01 +RTS -K0 -RTS
</code></pre>
48 <p>Run interactively with
</p>
49 <pre><code>stack ghci advent01
</code></pre>
51 <pre><code>stack ghci advent01:exe:advent01
</code></pre>
52 <p>if the first form is ambiguous.
</p>
53 <p>To profile, use
</p>
54 <pre><code>stack build --executable-profiling --library-profiling --ghc-options=
"-fprof-auto -rtsopts
" advent01
</code></pre>
56 <pre><code>stack exec --profile -- advent01 +RTS -p -hy
</code></pre>
57 <p>Generate the profile graph with
</p>
58 <pre><code>stack exec hp2ps advent01.hp
</code></pre>
59 <h1 id=
"packages">Packages
</h1>
60 <p>Stack is using the
<a href=
"https://www.stackage.org/lts-16.25">14.16-lts resolver
</a> for packages, so make sure you read the
<a href=
"https://www.stackage.org/lts-16.25/docs">correct documentation for the packages included in it
</a>.
</p>
61 <p>Packages I used a lot:
</p>
63 <li><a href=
"https://hackage.haskell.org/package/containers">Containers
</a> (and some
<a href=
"https://haskell-containers.readthedocs.io/en/latest/intro.html">better documentation
</a>);
<a href=
"https://hackage.haskell.org/package/unordered-containers">Unordered containers
</a> is a mostly-equivalent alternative.
</li>
64 <li><a href=
"https://hackage.haskell.org/package/attoparsec">Attoparsec
</a> (and
<a href=
"https://hackage.haskell.org/package/megaparsec">Megaparsec
</a>, and
<a href=
"https://hackage.haskell.org/package/base-4.14.1.0/docs/Text-ParserCombinators-ReadP.html">ReadP
</a> once).
</li>
66 <p>There are somewhat decent
<a href=
"https://markkarpov.com/tutorial/megaparsec.html">tutorials on Megaparsec
</a> and
<a href=
"https://www.schoolofhaskell.com/school/starting-with-haskell/libraries-and-frameworks/text-manipulation/attoparsec">Attoparsec
</a>.
</p>
67 <p>Packages I didn’t use much, but need to remember:
</p>
69 <li><a href=
"https://hackage.haskell.org/package/arithmoi">Arithmoi
</a> for number theory
</li>
70 <li><a href=
"https://hackage.haskell.org/package/pointedlist-0.6.1">Pointed List
</a> for zipper lists (sometimes circular)
</li>
71 <li><a href=
"https://hackage.haskell.org/package/vector">Vector
</a> for array-like things
</li>
72 <li><a href=
"https://hackage.haskell.org/package/linear">Linear
</a> for coordinate-vector like things
</li>
73 <li><a href=
"https://hackage.haskell.org/package/grid">Grid
</a> for
2-d grids
</li>
74 <li><a href=
"https://hackage.haskell.org/package/graph-wrapper">Graph-wrapper
</a> for graphs
</li>
75 <li><a href=
"https://hackage.haskell.org/package/lens">Lens
</a> (and a
<a href=
"https://github.com/ekmett/lens/wiki/Operators">summary of operators
</a>). I didn’t use these much this year, but did a lot last year.
</li>
76 <li><a href=
"https://hackage.haskell.org/package/mtl-2.2.2/docs/Control-Monad-RWS-Lazy.html">RWS
</a> (Reader-Writer-State monad stack); again, used a lot last year but not this year
</li>
77 <li><a href=
"https://hackage.haskell.org/package/monad-loops-0.4.3/docs/Control-Monad-Loops.html">Monad loops
</a>, and
<a href=
"https://conscientiousprogrammer.com/blog/2015/12/11/24-days-of-hackage-2015-day-11-monad-loops-avoiding-writing-recursive-functions-by-refactoring/">a description
</a></li>
78 <li><a href=
"https://github.com/jamesdbrock/replace-megaparsec">Replace-Megaparsec
</a>, for using Mpc for all sorts of things traditionally done with regex substitutions.
</li>
80 <h1 id=
"readme">Readme
</h1>
81 <p>Build this readme file wth
</p>
82 <pre><code>pandoc -s README.md
> README.html
</code></pre>
83 <p>(Using the
<a href=
"https://github.com/markdowncss/modest">Modest style
</a>.)
</p>