{
  "$type": "site.standard.document",
  "canonicalUrl": "https://unnecessary.tech/posts/aoc-2024-day6",
  "path": "/posts/aoc-2024-day6",
  "publishedAt": "2024-12-06T15:35:14.000Z",
  "site": "at://did:plc:jx54v4rmscfwzit7fmgz24ba/site.standard.publication/3mnrsqmzz3w2e",
  "tags": [
    "gleam",
    "programming"
  ],
  "textContent": "I thought Day 6 of Advent of Code was a\npretty interesting problem. The first part was relatively straightforward. You\nare given a grid containing empty space an obstructions and the location of a\nguard who start facing north. The guard moved forward until hitting an\nobstruction and then will always turn right and continue on his path. Eventually\nthe guard moves off the grid. The code I wrote is on github.\nTo maintain the guard's current state I used a\ntype with position and direction. I also defined a type to use if the guard's\nwalk was interrupted either by an obstruction, Collision, or going off the\ngrid, OffMap. \n\nThe first part was to count how many locations on the grid the guard occupied\nbefore moving off the grid. To accomplish this, I just saved the coordinates of\nthe guard's position as he ot she moved. I could then just count the number of\nitems in the set. This proved fairly simple, and I was ready for part 2.\n\nIn part 2 the idea is to find the locations where, by adding an obstruction,\nyou can cause the guard to move in a loop and never exit the grid. It is here\nwhere I started making a few assumptions which were incorrect.\n\nFor my first stab at this, I decided to find places where the guard crossed his\nor her previous path and put an obstruction there which would cause him or her\nto retrace his or her steps. This successfully found only 3 out of 6 of the obstruction\nlocations in the test data as it is possible to put the guard into a loop by\nplacing obstructions at locations which do not fit this criterea. I then took\nsome time to try to think of a way to select only relevant portions of the grid\non which to try putting an obstruction.\n\nFor my second attempt I put an obstruction ahead of every step the guard made\nwhich did not already have an obstruction, and then checked to see if this would\nresult in a loop. I'm sure this is not optimal, but it was not terribly slow\neither. Unfortunately I got a number that was too high! This really confused me\nfor a while, and I decided to rerun each grid from the beginning with each\nobstruction I found, and indeed I found many of these obstructions did not\nactually result in infinite loops, so where did I go wrong?\n\nIt turns out that the issue I was having was that in some cases I was placing\nobstructions along paths the guard had already tred in a different direction. \nThese obstructions were already checked earlier along the path, and\nwould have disrupted the path prior to the point I was currently checking. It\nis likely that the guard would not end up along the path he or she was currently\nwalking had there been an obstruction in that location. When I decided to skip\nchecking locations where the guard had already been, I ended up getting the\ncorrect number of obstructions.\n\nAlthough likely inefficient, this code ran in less than 2 seconds, so it is\nnot terrible, still I hate the feeling of tryping a solution in the web site\nand getting the message that your number is too high.",
  "title": "Advent of Code 2024 - Day 6"
}