Optimised day 19
[advent-of-code-22.git] / README.html
1 <!DOCTYPE html>
2 <html xmlns="http://www.w3.org/1999/xhtml" lang="" xml:lang="">
3 <head>
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 2022</title>
8 <style>
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%;}
13 div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
14 ul.task-list{list-style: none;}
15 pre > code.sourceCode { white-space: pre; position: relative; }
16 pre > code.sourceCode > span { display: inline-block; line-height: 1.25; }
17 pre > code.sourceCode > span:empty { height: 1.2em; }
18 .sourceCode { overflow: visible; }
19 code.sourceCode > span { color: inherit; text-decoration: inherit; }
20 div.sourceCode { margin: 1em 0; }
21 pre.sourceCode { margin: 0; }
22 @media screen {
23 div.sourceCode { overflow: auto; }
24 }
25 @media print {
26 pre > code.sourceCode { white-space: pre-wrap; }
27 pre > code.sourceCode > span { text-indent: -5em; padding-left: 5em; }
28 }
29 pre.numberSource code
30 { counter-reset: source-line 0; }
31 pre.numberSource code > span
32 { position: relative; left: -4em; counter-increment: source-line; }
33 pre.numberSource code > span > a:first-child::before
34 { content: counter(source-line);
35 position: relative; left: -1em; text-align: right; vertical-align: baseline;
36 border: none; display: inline-block;
37 -webkit-touch-callout: none; -webkit-user-select: none;
38 -khtml-user-select: none; -moz-user-select: none;
39 -ms-user-select: none; user-select: none;
40 padding: 0 4px; width: 4em;
41 color: #aaaaaa;
42 }
43 pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa; padding-left: 4px; }
44 div.sourceCode
45 { }
46 @media screen {
47 pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
48 }
49 code span.al { color: #ff0000; font-weight: bold; } /* Alert */
50 code span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
51 code span.at { color: #7d9029; } /* Attribute */
52 code span.bn { color: #40a070; } /* BaseN */
53 code span.bu { } /* BuiltIn */
54 code span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
55 code span.ch { color: #4070a0; } /* Char */
56 code span.cn { color: #880000; } /* Constant */
57 code span.co { color: #60a0b0; font-style: italic; } /* Comment */
58 code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
59 code span.do { color: #ba2121; font-style: italic; } /* Documentation */
60 code span.dt { color: #902000; } /* DataType */
61 code span.dv { color: #40a070; } /* DecVal */
62 code span.er { color: #ff0000; font-weight: bold; } /* Error */
63 code span.ex { } /* Extension */
64 code span.fl { color: #40a070; } /* Float */
65 code span.fu { color: #06287e; } /* Function */
66 code span.im { } /* Import */
67 code span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
68 code span.kw { color: #007020; font-weight: bold; } /* Keyword */
69 code span.op { color: #666666; } /* Operator */
70 code span.ot { color: #007020; } /* Other */
71 code span.pp { color: #bc7a00; } /* Preprocessor */
72 code span.sc { color: #4070a0; } /* SpecialChar */
73 code span.ss { color: #bb6688; } /* SpecialString */
74 code span.st { color: #4070a0; } /* String */
75 code span.va { color: #19177c; } /* Variable */
76 code span.vs { color: #4070a0; } /* VerbatimString */
77 code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
78 .display.math{display: block; text-align: center; margin: 0.5rem auto;}
79 </style>
80 <link rel="stylesheet" href="modest.css" />
81 <!--[if lt IE 9]>
82 <script src="//cdnjs.cloudflare.com/ajax/libs/html5shiv/3.7.3/html5shiv-printshiv.min.js"></script>
83 <![endif]-->
84 </head>
85 <body>
86 <header id="title-block-header">
87 <h1 class="title">Advent of Code 2022</h1>
88 </header>
89 <p>Code to solve the <a href="http://adventofcode.com/2022/">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>
90 <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>
91 <p>The <a href="https://cabal.readthedocs.io/en/latest/index.html">Cabal user guide</a> and <a href="http://howistart.org/posts/haskell/1/">How I Start: Haskell</a> are good sources of using the tools.</p>
92 <h1 id="toolchain">Toolchain</h1>
93 <p>Install Ghcup following <a href="https://www.haskell.org/ghcup/install/#installation">the instructions</a>, making sure to load the updated environment with</p>
94 <div class="sourceCode" id="cb1"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="bu">source</span> /home/neil/.ghcup/env</span></code></pre></div>
95 <p>and then set the default GHC to use with <code>ghcup set ghc 9.0.1</code> .</p>
96 <p>Install <a href="https://haskell-language-server.readthedocs.io/en/latest/configuration.html">Haskell Language Server</a> for Sublime Text</p>
97 <h2 id="creating-the-repository-and-project">Creating the repository and project</h2>
98 <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>
99 <p>There’s one package per day, with the code for each package in sub-directories of the root directory.</p>
100 <p>Create the basic <code>cabal</code> project.</p>
101 <pre><code>cabal init</code></pre>
102 <p>Modify the <code>advent-of-code21.cabal</code> file as needed, such as updating the Cabal version and writing the <code>common</code> stanzas.</p>
103 <h2 id="creating-subsequent-days">Creating subsequent days</h2>
104 <p>Each day lives in a separate directory, with code in the <code>src</code> directory.</p>
105 <p>Compile with</p>
106 <pre><code>cabal build</code></pre>
107 <p>or</p>
108 <pre><code>cabal build advent01</code></pre>
109 <p>Run with</p>
110 <pre><code>cabal run advent01</code></pre>
111 <p>If you want to pass in additional RTS parameters, do it like this:</p>
112 <pre><code>cabal run advent01 -- +RTS -K0 -RTS</code></pre>
113 <p>Run interactively with</p>
114 <pre><code>cabal repl advent01</code></pre>
115 <p>or</p>
116 <pre><code>stack ghci advent01:exe:advent01</code></pre>
117 <p>if the first form is ambiguous.</p>
118 <h2 id="profiling">Profiling</h2>
119 <p>To profile, use</p>
120 <pre><code>cabal run advent01 --enable-profiling -- +RTS -N -p -s -hT</code></pre>
121 <p>Or, you can simplify the RTS options by adding them to a new stanza in the cabal file:</p>
122 <pre><code>executable advent01prof
123 import: common-extensions, build-directives
124 main-is: advent01/Main.hs
125 build-depends: text, containers, linear, array, pqueue, mtl, lens
126 ghc-options: -O2
127 -Wall
128 -threaded
129 -rtsopts &quot;-with-rtsopts=-N -p -s -hT&quot;</code></pre>
130 <p>then running</p>
131 <pre><code>cabal run advent01prof --enable-profiling</code></pre>
132 <p>Generate the profile graph with</p>
133 <pre><code>hp2ps -M advent01.hp</code></pre>
134 <h1 id="packages">Packages</h1>
135 <p>Packages I used a lot:</p>
136 <ul>
137 <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>
138 <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>
139 </ul>
140 <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>
141 <p>Packages I didn’t use much, but need to remember:</p>
142 <ul>
143 <li><a href="https://hackage.haskell.org/package/arithmoi">Arithmoi</a> for number theory</li>
144 <li><a href="https://hackage.haskell.org/package/pointedlist-0.6.1">Pointed List</a> for zipper lists (sometimes circular)</li>
145 <li><a href="https://hackage.haskell.org/package/vector">Vector</a> for array-like things</li>
146 <li><a href="https://hackage.haskell.org/package/linear">Linear</a> for coordinate-vector like things</li>
147 <li><a href="https://hackage.haskell.org/package/grid">Grid</a> for 2-d grids</li>
148 <li><a href="https://hackage.haskell.org/package/graph-wrapper">Graph-wrapper</a> for graphs</li>
149 <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>
150 <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>
151 <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>
152 <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>
153 </ul>
154 <h1 id="readme">Readme</h1>
155 <p>Build this readme file wth</p>
156 <pre><code>pandoc -s README.md &gt; README.html</code></pre>
157 <p>(Using the <a href="https://github.com/markdowncss/modest">Modest style</a>.)</p>
158 </body>
159 </html>