DEV Community

Derp
Derp

Posted on • Updated on

Advent of Code 2021 - day 2

https://adventofcode.com/2021/day/2

Day 2 appears to be a parsing problem. So given

forward 5
down 5
forward 8
up 3
down 8
forward 2
Enter fullscreen mode Exit fullscreen mode

We want to increment the X co-ordinate when going forwards and either increment the Y co-ordinate when going up or down.

To solve this, I am first going to map my strings into tuples representing the change in X or Y. So down 5 gets translated into (0, -5). I am also going to create a monoid under sum and fold over my tuple array to get my final change in X and Y.

-- Part 2
This introduces an aim concept. To solve this, I am re-using the tuple mapping from part 1 and I will need a reducing function that takes in a current [horizontal, depth, aim] and a [forward, updown] tuple and produces a new [horizontal', depth', aim'] tuple.

https://codesandbox.io/s/reverent-fast-ytbx3

Top comments (0)