Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Advent of Code 2023 - a Haskeller's reflections

Advent of Code 2023 - a Haskeller's reflections

Advent of Code is an annual "advent" release of coding challenges. In this presentation I share my experience tackling the 2023 challenges using Haskell. I will begin by describing the contest structure and giving some examples. Then I will share my reflections on the strengths and weaknesses of Haskell (and pure FP in general) for the various kinds of problems that appeared in the 2023 set. We will deep dive on some of my favourite solutions - and a couple of not-so-elegant ones.

(Note: code was reviewed in a text editor and is not reproduced in the slides. Repo is located at https://github.com/frasertweedale/advent.)

Fraser Tweedale

February 16, 2024
Tweet

More Decks by Fraser Tweedale

Other Decks in Programming

Transcript

  1. Advent of Code Advent of Code is an Advent calendar

    of small programming puzzles for a variety of skill sets and skill levels that can be solved in any programming language you like. People use them as interview prep, company training, university course- work, practice problems, a speed contest, or to challenge each other. —https://adventofcode.com/2023/about
  2. Advent of Code ▶ Founded in 2015 by Eric Wastl

    ▶ Two-part puzzle released each day during December ▶ Unique puzzle input (and solution) for each user ▶ Global and private leaderboards (fastest correct submissions)
  3. Advent of Code - puzzle structure ▶ Part 1: ▶

    problem description + sample input and result ▶ unique puzzle input (≫ sample) ▶ Part 2: ▶ revealed when correct Part 1 answer submitted ▶ same input, reinterpreted in some way ▶ sample input and result
  4. Advent of Code - part 1 vs part 2 ▶

    generalise the part 1 solution ▶ naive/brute force → efficient algorithm ▶ sometimes a complete change of approach
  5. Advent of Code - problem difficulty Problems tend toward higher

    complexity/difficulty: 42 src/Y2023/D01.hs 107 src/Y2023/D12.hs 78 src/Y2023/D02.hs 68 src/Y2023/D13.hs 104 src/Y2023/D03.hs 71 src/Y2023/D14.hs 55 src/Y2023/D04.hs 88 src/Y2023/D15.hs 130 src/Y2023/D05.hs 96 src/Y2023/D16.hs 46 src/Y2023/D06.hs 126 src/Y2023/D17.hs 118 src/Y2023/D07.hs 161 src/Y2023/D18.hs 104 src/Y2023/D08.hs 183 src/Y2023/D19.hs 47 src/Y2023/D09.hs 204 src/Y2023/D20.hs 164 src/Y2023/D10.hs 192 src/Y2023/D21.hs 68 src/Y2023/D11.hs
  6. Advent of Code - my results ▶ Most problems took

    < 2h, some much longer ▶ I stopped after Day 21 (got too busy) ▶ Didn’t make the daily "Top 100" leaderboard (sometimes close)
  7. Advent of Code - tips ▶ No invalid inputs—you don’t

    need to handle them. ▶ The sample input is given for a reason. Test your code! ▶ Write lots of comments (as you go) to explain your solution. ▶ Don’t be afraid to use pen and paper!
  8. Advent of Code - gotchas The problem description does not

    always contain everything you need to solve the problem. Occasionally it depends on properties of the input, which you must discover yourself. e.g. https://adventofcode.com/2023/day/21
  9. Advent of Code - common topics Most common: ▶ parsing

    ▶ list processing ▶ graphs (shortest path, cycles) ▶ matrices (spacial problems) ▶ state machines Occasional: ▶ combinatorics ▶ dynamic programming
  10. Advent of Code - data structures and algorithms Frequently needed:

    ▶ list / non-empty list ▶ matrix = list-of-list ▶ map/dict and set ▶ graph ▶ directed and undirected ▶ adjacency-list and matrix representations ▶ DFS, BFS, shortest path, cycle detection Not used (yet. . . ): ▶ tree, heap, stack, zipper
  11. Advent of Code 2023 - self-imposed constraints Limited to the

    Haskell base library: ▶ had to implement Map type myself (AVL tree) ▶ had to implement parsing library myself ▶ using mature libraries (e.g. vector, containers) would reduce development time; solutions somewhat faster It was worthwhile to review some fundamentals. I would use off-the-shelf libraries next time.
  12. Advent of Code - where Haskell shines ▶ parsing ▶

    list processing (incl. strings) ▶ laziness ▶ state machines ▶ problem-specific data domains (e.g. playing cards)
  13. Advent of Code - where Haskell does not shine ▶

    dynamic programming (memoisation) ▶ space leaks (deepseq can help) ▶ debugging
  14. Final remarks ▶ Advent of Code is fun ▶ Do

    it in whatever way you want ▶ learn a new language ▶ practice fundamentals ▶ space it out ▶ challenge your friends/workmates ▶ . . . or solve it together ▶ r/adventofcode for tips/ideas/help ▶ Haskell is a great language to do Advent of Code ▶ My code is available: github.com/frasertweedale/advent
  15. © 2024 Fraser Tweedale Except where otherwise noted this work

    is licensed under http://creativecommons.org/licenses/by/4.0/ Code https://github.com/frasertweedale Blog frasertweedale.github.io/blog-fp Fediverse @[email protected]