Skip to content

Commit

Permalink
A few comments
Browse files Browse the repository at this point in the history
  • Loading branch information
derailed-dash committed Jan 9, 2024
1 parent 6b03f0b commit d1188ae
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/AoC_2023/Dazbo's_Advent_of_Code_2023.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -4474,28 +4474,32 @@
"**My solution**:\n",
"\n",
"Grow the input according to the duplication rule:\n",
"\n",
"- Turn the 2D grid into a NumPy array, as this is faster for manipulating grids of data. I've converted all `#` to a `1` and all `.` to a `0`. This makes subsequent operations a bit easier. \n",
"- For rows: iterate through each row check if a row contains `1`. If not, insert an extra row. I've parameterised this so that we can add an arbitrary number of rows. But for Part 1, we only ever need to add one additional row wherever a row contains no galaxies.\n",
"- For cols: do the same, but transpose the array first, using the `ndarray transpose()` method. After transposing, each column now exists as a row. So I can use the same function as I used for rows. After inserting the rows, we must transpose back.\n",
"\n",
"Determine all the locations of points. This code is pretty useful:\n",
"Now we work out where the galaxies are:\n",
"\n",
"- Determine all the locations in the array where the value is 1.\n",
"\n",
"```python\n",
" # Determine all locations with a galaxy and convert to Points\n",
" y, x = np.where(array == 1) # remember that 1 = #\n",
" hash_points = [Point(x, y) for (y, x) in zip(y, x)]\n",
"```\n",
"\n",
"- Determine all the locations in the array where the value is 1.\n",
"- For each of these locations return the list of `y` values and the list of `x` values.\n",
"- Then I use [zip](https://aoc.just2good.co.uk/python/zip) to combine the two lists, to get a single list of `(y,x)` tuples. Then I use a [list comprehension](https://aoc.just2good.co.uk/python/comprehensions) to create a `Point` from each of these tuples.\n",
"\n",
"Next, get all the pairs of points using [`itertools.combinations()`](https://aoc.just2good.co.uk/python/perms_combos).\n",
"\n",
"- The `combinations()` function returns all unique combinations of points, but doesn't care about sequence. \n",
"- I.e. if we have A -> B, it will not also return B -> A. \n",
"- The instructions are pretty clear here: _\"Only count each pair once; order within the pair doesn't matter.\"_\n",
"\n",
"Now I find the _Manhattan distance_ for each pair. \n",
"Now I find the _Manhattan distance_ for each pair:\n",
"\n",
"- The Manhattan distance is the sum of horizontal and vertical distance, which is exactly what we want. \n",
"- My `Point` class already knows how to do this.\n",
"- Sum up the distances.\n",
Expand All @@ -4512,7 +4516,7 @@
"def insert_rows(array: np.ndarray, insert_number) -> np.ndarray:\n",
" new_rows = []\n",
" for row in array:\n",
" new_rows.append(row)\n",
" new_rows.append(row) # add back the original row\n",
" if 1 not in row:\n",
" new_rows.extend(row for _ in range(insert_number))\n",
"\n",
Expand Down

0 comments on commit d1188ae

Please sign in to comment.