aboutsummaryrefslogtreecommitdiff
path: root/2025/README.md
diff options
context:
space:
mode:
Diffstat (limited to '2025/README.md')
-rw-r--r--2025/README.md36
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```
13Day -Part 1- -Part 2- 13Day -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
113boxes](https://en.wikipedia.org/wiki/Disjoint-set_data_structure) (or 114boxes](https://en.wikipedia.org/wiki/Disjoint-set_data_structure) (or
114a matter of remembering how it is implemented, if one already knows it). 115a matter of remembering how it is implemented, if one already knows it).
116
117### Day 9: Movie Theater
118
119For me this was the first challenging problem (at least excluding those
120that were challenging because of mis-read the problem statement or
121because of sneaky bugs).
122
123Part 1 was easy as usual. For part 2 we go through every pair of
124corners and we check if the rectangle between them is admissible. A
125rectangle is admissible if none of the line segments that make up the
126figure "breaks" it. Breaking a rectangle means that either the lines is
127internal to it (and thus the points on one of the sides of the line are
128invalid but inside the rectangle) or that the line overlaps (at least
129in part) with one of the sides of the rectangle, and the exterior of
130the figure is in the rectangle. To know which side of the line is the
131interior and which is the exterior, I do some pre-processing to compute
132if our border winds clockwise or counter-clockwise (using the [cross
133product](https://en.wikipedia.org/wiki/Cross_product)). It may be that
134every input has clockwise-winding border, but it was not much work to
135implement this pre-processing.
136
137My implementation of the function that checks if a line breaks the
138rectangle looks like a bunch of very smart Math, but don't think I am
139smart enough to write it on the first try: first I wrote it in 4 separate
140cases (line is vertical / horizontal, t is positive or negative) and
141then I merged the cases together removing the duplication. And I did
142not merge the cases together to look smart, I did it because it made
143debugging easier (did you think my code worked on the first try? ahah).
144
145This 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,
147and in fact I wasted a lot of time searching a quadratic or O(n^2log n)
148solution before I focused on formalizing a cubic one. The problem is that
149I did not have smaller inputs to try my code against, so I this solution
150was too slow I did not have any way to check that it was at least correct.

Generated with cgit - Back to sebastiano.tronto.net