Code Monkey home page Code Monkey logo

advent2021's Introduction

Advent of Code 2021

These are my Advent of Code 2021 solutions. I generally try to focus on making clean and documented Haskell solutions to each puzzle.

We'll be chatting about AoC on IRC all December. You can find AoC discussion on Libera Chat's #adventofcode and #haskell (webchat)

Building

I recommend installing ghc with ghcup.

$ curl --proto '=https' --tlsv1.2 -sSf https://get-ghcup.haskell.org | sh

I'm using GHC 9.0.1 this year

$ ghcup install ghc 9.0.1
$ ghcup set     ghc 9.0.1

cabal-install is the best way to get things built, and is provided by ghcup

$ cabal update
$ cabal build

Running solutions

All the solutions take an optional command line argument that can be an input file name or - to read the input from stdin. The default input filename is inputs/input##.txt

$ cabal run Day01
...
1681
1704

Common libraries used

I try to avoid using too many dependencies for these problems.

  • containers - Almost all of the solutions will benefit from having access to Map and Set types from this package.
  • array - Standard mutable and immutable array library
  • doctest - Having checked examples in the documentation makes it easier to understand what code does and easy to add unit tests.
  • alex - Lexer generator package I use for my input format DSL
  • happy - Parser generator package I use for my input format DSL

Input file DSL

To speed up the process of writing input parsers, I use a quasiquoter that generates ReadP parsers.

Day 13 used in input format of pairs of numbers and fold instructions as seen below.

6,10
0,14
9,10
0,3
10,4
4,11
6,0
6,12
4,1
0,13
10,12
3,4
3,0
8,4
1,10
2,14
8,10
9,0

fold along y=7
fold along x=5

This can be parsed using the following pattern string from Day13.hs

data A = Ax | Ay
[format|13 (%u,%u%n)*%n(fold along @A=%u%n)*|]

which processes an output of

([(6,10),(0,14),(9,10),(0,3),(10,4),(4,11),(6,0),(6,12),(4,1),(0,13),(10,12),(3,4),(3,0),(8,4),(1,10),(2,14),(8,10),(9,0)],[(Ay,7),(Ax,5)])

Getting the data into Haskell datatypes quickly allows me to focus on the problem and not the input file, and it keeps the solution files focused, as well.

advent2021's People

Contributors

glguy avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.