···119119 <div class="row">
120120 <p>Thus, to solve the first part, we can break up our input using
121121 <code>lines</code>. <code>rot</code> is a simple function that
122122- rotates a list forwards to move the last element to the first. By
123123- doing <code>rot . lines</code> upon our input, we have a list of
124124- the form <code>["+ *", "1 2 3", "4 5 6"]</code>. Next, we define
125125- an <code>align</code> function to line up the colums. This method
126126- simply breaks each item in the list by spaces, and then transposes
127127- them, so we end up with
128128- <code>[["+", "1", "2", "3"], ["*", "4", "5", "6"]]</code>:</p>
122122+ rotates a list forwards to move the last element to the first.</p>
129123 <div class="code">
130124 <div class="sourceCode" id="cb2"><pre
131131- class="sourceCode haskell haskell-top"><code class="sourceCode haskell"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true" tabindex="-1"></a><span class="kw">import</span> <span class="dt">Data.Char</span> (isSpace)</span>
132132-<span id="cb2-2"><a href="#cb2-2" aria-hidden="true" tabindex="-1"></a><span class="kw">import</span> <span class="dt">Data.List</span> (transpose)</span>
133133-<span id="cb2-3"><a href="#cb2-3" aria-hidden="true" tabindex="-1"></a><span class="kw">import</span> <span class="dt">Data.List.Split</span> (splitWhen)</span></code></pre></div>
134134- <div class="sourceCode" id="cb3"><pre
135135- class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb3-1"><a href="#cb3-1" aria-hidden="true" tabindex="-1"></a>rot <span class="ot">=</span> liftA2 (<span class="op">:</span>) <span class="fu">last</span> <span class="fu">init</span></span>
136136-<span id="cb3-2"><a href="#cb3-2" aria-hidden="true" tabindex="-1"></a></span>
137137-<span id="cb3-3"><a href="#cb3-3" aria-hidden="true" tabindex="-1"></a>p1 <span class="ot">=</span> <span class="fu">sum</span> <span class="op">.</span> <span class="fu">map</span> solve <span class="op">.</span> align <span class="op">.</span> rot <span class="op">.</span> <span class="fu">lines</span></span>
138138-<span id="cb3-4"><a href="#cb3-4" aria-hidden="true" tabindex="-1"></a> <span class="kw">where</span></span>
139139-<span id="cb3-5"><a href="#cb3-5" aria-hidden="true" tabindex="-1"></a> align <span class="ot">=</span> transpose <span class="op">.</span> <span class="fu">map</span> <span class="fu">words</span></span></code></pre></div>
125125+ class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true" tabindex="-1"></a>rot <span class="ot">=</span> liftA2 (<span class="op">:</span>) <span class="fu">last</span> <span class="fu">init</span></span></code></pre></div>
126126+ </div>
127127+ </div>
128128+ <div class="row">
129129+ <p>By doing <code>rot . lines</code> upon our input, we have a
130130+ list of the form:</p>
131131+ <div class="code">
132132+133133+ </div>
134134+ </div>
135135+ <pre><code>["+ *", "1 2 3", "4 5 6"]</code></pre>
136136+ <div class="row">
137137+ <p>Next, we define an <code>align</code> function to line up the
138138+ columns, and define our solution to part 1 like so:</p>
139139+ <div class="code">
140140+ <div class="sourceCode" id="cb4"><pre
141141+ class="sourceCode haskell haskell-top"><code class="sourceCode haskell"><span id="cb4-1"><a href="#cb4-1" aria-hidden="true" tabindex="-1"></a><span class="kw">import</span> <span class="dt">Data.List</span> (transpose)</span></code></pre></div>
142142+ <div class="sourceCode" id="cb5"><pre
143143+ class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb5-1"><a href="#cb5-1" aria-hidden="true" tabindex="-1"></a></span>
144144+<span id="cb5-2"><a href="#cb5-2" aria-hidden="true" tabindex="-1"></a>p1 <span class="ot">=</span> <span class="fu">sum</span> <span class="op">.</span> <span class="fu">map</span> solve <span class="op">.</span> align <span class="op">.</span> rot <span class="op">.</span> <span class="fu">lines</span></span>
145145+<span id="cb5-3"><a href="#cb5-3" aria-hidden="true" tabindex="-1"></a> <span class="kw">where</span></span>
146146+<span id="cb5-4"><a href="#cb5-4" aria-hidden="true" tabindex="-1"></a> align <span class="ot">=</span> transpose <span class="op">.</span> <span class="fu">map</span> <span class="fu">words</span></span></code></pre></div>
140147 </div>
141148 </div>
149149+ <div class="row">
150150+ <p>This method simply breaks each item in the list by spaces, and
151151+ then transposes them, so we end up with:</p>
152152+ <div class="code">
153153+154154+ </div>
155155+ </div>
156156+ <pre><code>[["+", "1", "2", "3"], ["*", "4", "5", "6"]]</code></pre>
142157 <h3 data-number="2.4.2" id="part-2-4">Part 2</h3>
143158 <div class="row">
144159 <p>We are now tasked with lining up the digits in a columnar
145145- fashion to for a number:</p>
160160+ fashion to form a number:</p>
146161 <div class="code">
147162148163 </div>
···185200 operators to the start. Our <code>align</code> function can now be
186201 defined as below.</p>
187202 <div class="code">
188188- <div class="sourceCode" id="cb7"><pre
189189- class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb7-1"><a href="#cb7-1" aria-hidden="true" tabindex="-1"></a>p2 <span class="ot">=</span> <span class="fu">sum</span> <span class="op">.</span> <span class="fu">map</span> solve <span class="op">.</span> align <span class="op">.</span> rot <span class="op">.</span> <span class="fu">lines</span></span>
190190-<span id="cb7-2"><a href="#cb7-2" aria-hidden="true" tabindex="-1"></a> <span class="kw">where</span></span>
191191-<span id="cb7-3"><a href="#cb7-3" aria-hidden="true" tabindex="-1"></a> align <span class="ot">=</span></span>
192192-<span id="cb7-4"><a href="#cb7-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">zipWith</span> (<span class="op">:</span>)</span>
193193-<span id="cb7-5"><a href="#cb7-5" aria-hidden="true" tabindex="-1"></a> <span class="op"><$></span> (<span class="fu">words</span> <span class="op">.</span> <span class="fu">head</span>)</span>
194194-<span id="cb7-6"><a href="#cb7-6" aria-hidden="true" tabindex="-1"></a> <span class="op"><*></span> (splitWhen (<span class="fu">all</span> <span class="fu">isSpace</span>) <span class="op">.</span> transpose <span class="op">.</span> <span class="fu">tail</span>)</span></code></pre></div>
203203+ <div class="sourceCode" id="cb10"><pre
204204+ class="sourceCode haskell haskell-top"><code class="sourceCode haskell"><span id="cb10-1"><a href="#cb10-1" aria-hidden="true" tabindex="-1"></a><span class="kw">import</span> <span class="dt">Data.List.Split</span> (splitWhen)</span></code></pre></div>
205205+ <div class="sourceCode" id="cb11"><pre
206206+ class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb11-1"><a href="#cb11-1" aria-hidden="true" tabindex="-1"></a>p2 <span class="ot">=</span> <span class="fu">sum</span> <span class="op">.</span> <span class="fu">map</span> solve <span class="op">.</span> align <span class="op">.</span> rot <span class="op">.</span> <span class="fu">lines</span></span>
207207+<span id="cb11-2"><a href="#cb11-2" aria-hidden="true" tabindex="-1"></a> <span class="kw">where</span></span>
208208+<span id="cb11-3"><a href="#cb11-3" aria-hidden="true" tabindex="-1"></a> align <span class="ot">=</span></span>
209209+<span id="cb11-4"><a href="#cb11-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">zipWith</span> (<span class="op">:</span>)</span>
210210+<span id="cb11-5"><a href="#cb11-5" aria-hidden="true" tabindex="-1"></a> <span class="op"><$></span> (<span class="fu">words</span> <span class="op">.</span> <span class="fu">head</span>)</span>
211211+<span id="cb11-6"><a href="#cb11-6" aria-hidden="true" tabindex="-1"></a> <span class="op"><*></span> (splitWhen (<span class="fu">all</span> (<span class="ch">' '</span> <span class="op">==</span>)) <span class="op">.</span> transpose <span class="op">.</span> <span class="fu">tail</span>)</span></code></pre></div>
195212 </div>
196213 </div>
197214 <div class="row">
···205222 <pre><code>["+", "1", "2", ...]</code></pre>
206223 <div class="row">
207224 <p>Remember that this is the format <code>solve</code>
208208- accepts!.</p>
225225+ accepts!</p>
209226 <div class="code">
210227211228 </div>
···242259 <pre><code>["1 ","24 ","356"," ","369","248","8 ", ...]</code></pre>
243260 <div class="row">
244261 <p>There is an element containing all spaces (which was present
245245- between each column!). This is what we use to separate each
246246- column.</p>
262262+ between each column), <code>Data.List.Split.splitWhen</code> is
263263+ employed to detect and chunk our list into runs of valid
264264+ numbers.</p>
247265 <div class="code">
248266249267 </div>
···251269 <div class="row">
252270 <p>Finally, a main function to wrap it all up:</p>
253271 <div class="code">
254254- <div class="sourceCode" id="cb12"><pre
255255- class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb12-1"><a href="#cb12-1" aria-hidden="true" tabindex="-1"></a>main <span class="ot">=</span> <span class="kw">do</span></span>
256256-<span id="cb12-2"><a href="#cb12-2" aria-hidden="true" tabindex="-1"></a> n <span class="ot"><-</span> <span class="fu">getContents</span></span>
257257-<span id="cb12-3"><a href="#cb12-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">print</span> <span class="op">$</span> p1 n</span>
258258-<span id="cb12-4"><a href="#cb12-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">print</span> <span class="op">$</span> p2 n</span></code></pre></div>
272272+ <div class="sourceCode" id="cb16"><pre
273273+ class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb16-1"><a href="#cb16-1" aria-hidden="true" tabindex="-1"></a>main <span class="ot">=</span> <span class="kw">do</span></span>
274274+<span id="cb16-2"><a href="#cb16-2" aria-hidden="true" tabindex="-1"></a> n <span class="ot"><-</span> <span class="fu">getContents</span></span>
275275+<span id="cb16-3"><a href="#cb16-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">print</span> <span class="op">$</span> p1 n</span>
276276+<span id="cb16-4"><a href="#cb16-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">print</span> <span class="op">$</span> p2 n</span></code></pre></div>
259277 </div>
260278 </div>
261279 </body>
+15-5
readme.txt
···11-aoc
22----
11+aoc.oppi.li
22+-----------
3344-solutions for aoc written in haskell
44+solutions for aoc written in literate haskell, and exported
55+to a "book of solves": aoc.oppi.li
5666-all solutions are (soon) to be written in literate haskell
77-and exported to an interactive book using ./book/build.sh.
77+to compile the unlit program:
88+99+ ghc unlit/unlit.hs -o bin/unlit
1010+1111+to execute a solution:
1212+1313+ cat input | runhaskell --ghc-arg="-pgmL ./bin/unlit" 01.lhs
1414+1515+to build the book of solves (requires pandoc):
1616+1717+ ./book/build.sh
+22-6
src/2025/06.lhs
···1111solve ("*" : rest) = product . map read $ rest
1212```
13131414-Thus, to solve the first part, we can break up our input using `lines`. `rot` is a simple function that rotates a list forwards to move the last element to the first. By doing `rot . lines` upon our input, we have a list of the form `["+ *", "1 2 3", "4 5 6"]`. Next, we define an `align` function to line up the colums. This method simply breaks each item in the list by spaces, and then transposes them, so we end up with `[["+", "1", "2", "3"], ["*", "4", "5", "6"]]`:
1414+Thus, to solve the first part, we can break up our input using `lines`. `rot` is a simple function that rotates a list forwards to move the last element to the first.
1515+1616+```haskell
1717+rot = liftA2 (:) last init
1818+```
1919+2020+By doing `rot . lines` upon our input, we have a list of the form:
2121+2222+ ["+ *", "1 2 3", "4 5 6"]
2323+2424+Next, we define an `align` function to line up the columns, and define our solution to part 1 like so:
15251626```haskell-top
1717-import Data.Char (isSpace)
1827import Data.List (transpose)
1919-import Data.List.Split (splitWhen)
2028```
21292230```haskell
2323-rot = liftA2 (:) last init
24312532p1 = sum . map solve . align . rot . lines
2633 where
2734 align = transpose . map words
2835```
29363737+This method simply breaks each item in the list by spaces, and then transposes them, so we end up with:
3838+3939+ [["+", "1", "2", "3"], ["*", "4", "5", "6"]]
4040+4141+3042### Part 2
31433232-We are now tasked with lining up the digits in a columnar fashion to for a number:
4444+We are now tasked with lining up the digits in a columnar fashion to form a number:
33453446 123 ...
3547 45 ...
···52645365Let us start with a similar base, using `lines` to break things up, and `rot` to move the line of operators to the start. Our `align` function can now be defined as below.
54666767+```haskell-top
6868+import Data.List.Split (splitWhen)
6969+```
7070+5571```haskell
5672p2 = sum . map solve . align . rot . lines
5773 where
5874 align =
5975 zipWith (:)
6076 <$> (words . head)
6161- <*> (splitWhen (all isSpace) . transpose . tail)
7777+ <*> (splitWhen (all (' ' ==)) . transpose . tail)
6278```
63796480We first start with `zipWith (:)` to join the operation `"+"` with the rest of the numbers to produce: