{
  "$type": "site.standard.document",
  "canonicalUrl": "https://unnecessary.tech/posts/aoc-2024-day4",
  "path": "/posts/aoc-2024-day4",
  "publishedAt": "2024-12-04T18:36:09.000Z",
  "site": "at://did:plc:jx54v4rmscfwzit7fmgz24ba/site.standard.publication/3mnrsqmzz3w2e",
  "tags": [
    "gleam",
    "programming"
  ],
  "textContent": "The last few days of Advent of Code have been fairly\nstraightforward and I have\nnot really used any new language features of Gleam. I do\nfeel like I am better at spotting where the list.fold function is useful, but\non day 4 there was a great opportunity to make use of the use funcitonality.\nBefore jumping in with my explanation, Isaac Harris-Holt\nmade a video about use in Gleam which is very good, so I encourage you to\nwatch that first before I describe how I used in on day 4.\n\n{{< youtube -rtVWja_vJI >}}\n\nSo the use essentially takes a function, whose final argument is a callback\nfunction, and allows you to define the callback function immediately after the\nuse line. For example the following code which doubles each number in a list\nusing a callback function\n\ncan be rewritten without the additional indentation as below.\n\nThese are functionally equivalent returning [2, 4, 6].\n\nIn today's problem we had to parse a 140\nby 140 grid of letters into a structure we could arbitrarily search to find a\ncombination of letters in horizontal, vertical, and diagonal directions. I\ndecided to use a dict.Dict with the position of the character in X-Y coordinates\nas the key, and the letter as the value. This ended up being very easy to do\nwith use and list.index_fold. The parsing function I wrote takes a list\nof lines and is shown below. I called the dictionary \"map\" in this example,\nwhich is probably not the best name.\n\nThis is equivalent to writing the function below.\n\nI like the way use simplifies the look of the function once you get used to it,\nhowever it is important to note that for each use you end up going one nested\ncallback deeper. It can take a little time to get used to this syntax. My full\nsolutions for all the problems are available on github.",
  "title": "Advent of Code 2024 - Day 4"
}