diff options
Diffstat (limited to '2025')
| -rw-r--r-- | 2025/README.md | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/2025/README.md b/2025/README.md index 19fbb29..c1d3f2c 100644 --- a/2025/README.md +++ b/2025/README.md | |||
| @@ -11,6 +11,7 @@ Example | |||
| 11 | 11 | ||
| 12 | ``` | 12 | ``` |
| 13 | Day -Part 1- -Part 2- | 13 | Day -Part 1- -Part 2- |
| 14 | 9 00:05:05 02:11:41 | ||
| 14 | 8 00:29:14 00:33:02 | 15 | 8 00:29:14 00:33:02 |
| 15 | 7 00:05:27 00:20:40 | 16 | 7 00:05:27 00:20:40 |
| 16 | 6 00:13:38 01:49:24 | 17 | 6 00:13:38 01:49:24 |
| @@ -112,3 +113,38 @@ programming. In the end it was mostly a matter of figuring out the correct | |||
| 112 | [data structure to represent the groups of joint | 113 | [data structure to represent the groups of joint |
| 113 | boxes](https://en.wikipedia.org/wiki/Disjoint-set_data_structure) (or | 114 | boxes](https://en.wikipedia.org/wiki/Disjoint-set_data_structure) (or |
| 114 | a matter of remembering how it is implemented, if one already knows it). | 115 | a matter of remembering how it is implemented, if one already knows it). |
| 116 | |||
| 117 | ### Day 9: Movie Theater | ||
| 118 | |||
| 119 | For me this was the first challenging problem (at least excluding those | ||
| 120 | that were challenging because of mis-read the problem statement or | ||
| 121 | because of sneaky bugs). | ||
| 122 | |||
| 123 | Part 1 was easy as usual. For part 2 we go through every pair of | ||
| 124 | corners and we check if the rectangle between them is admissible. A | ||
| 125 | rectangle is admissible if none of the line segments that make up the | ||
| 126 | figure "breaks" it. Breaking a rectangle means that either the lines is | ||
| 127 | internal to it (and thus the points on one of the sides of the line are | ||
| 128 | invalid but inside the rectangle) or that the line overlaps (at least | ||
| 129 | in part) with one of the sides of the rectangle, and the exterior of | ||
| 130 | the figure is in the rectangle. To know which side of the line is the | ||
| 131 | interior and which is the exterior, I do some pre-processing to compute | ||
| 132 | if our border winds clockwise or counter-clockwise (using the [cross | ||
| 133 | product](https://en.wikipedia.org/wiki/Cross_product)). It may be that | ||
| 134 | every input has clockwise-winding border, but it was not much work to | ||
| 135 | implement this pre-processing. | ||
| 136 | |||
| 137 | My implementation of the function that checks if a line breaks the | ||
| 138 | rectangle looks like a bunch of very smart Math, but don't think I am | ||
| 139 | smart enough to write it on the first try: first I wrote it in 4 separate | ||
| 140 | cases (line is vertical / horizontal, t is positive or negative) and | ||
| 141 | then I merged the cases together removing the duplication. And I did | ||
| 142 | not merge the cases together to look smart, I did it because it made | ||
| 143 | debugging easier (did you think my code worked on the first try? ahah). | ||
| 144 | |||
| 145 | This algorithm is O(n^3) and runs in about 6 seconds on my small laptop | ||
| 146 | (or in about 4 seconds on my desktop). I was not sure O(n^3) was enough, | ||
| 147 | and in fact I wasted a lot of time searching a quadratic or O(n^2log n) | ||
| 148 | solution before I focused on formalizing a cubic one. The problem is that | ||
| 149 | I did not have smaller inputs to try my code against, so I this solution | ||
| 150 | was too slow I did not have any way to check that it was at least correct. | ||
