aboutsummaryrefslogtreecommitdiff
path: root/2025/09
diff options
context:
space:
mode:
authorSebastiano Tronto <sebastiano@tronto.net>2025-12-09 21:46:42 +0100
committerSebastiano Tronto <sebastiano@tronto.net>2025-12-09 21:46:42 +0100
commit4dc2097b38697614dba7a58bf2ec53e1e54e52fe (patch)
tree0df81e531ef9e0551eaf5be64da6de088d86ce1b /2025/09
parente87e83de464a2ee47d020fbe532f6f558fa83369 (diff)
downloadaoc-4dc2097b38697614dba7a58bf2ec53e1e54e52fe.tar.gz
aoc-4dc2097b38697614dba7a58bf2ec53e1e54e52fe.zip
Fixed part 2 for edge cases not present in input file
Diffstat (limited to '2025/09')
-rw-r--r--2025/09/b-fixed.py48
1 files changed, 48 insertions, 0 deletions
diff --git a/2025/09/b-fixed.py b/2025/09/b-fixed.py
new file mode 100644
index 0000000..eedc275
--- /dev/null
+++ b/2025/09/b-fixed.py
@@ -0,0 +1,48 @@
1import fileinput
2
3with fileinput.input() as lines:
4 a = [tuple(int(x) for x in l[:-1].split(',')) for l in lines]
5
6# Check if the border is turning clockwise or counter-clockwise
7def dir(p, q):
8 return p[0]*q[1]-p[1]*q[0]
9t = 0
10for i in range(len(a)):
11 p, q, r = a[i%len(a)], a[(i+1)%len(a)], a[(i+2)%len(a)]
12 t += 1 if dir((q[0]-p[0],q[1]-p[1]), (r[0]-q[0],r[1]-q[1])) > 0 else -1
13
14def external_by_vertex(a, i, j):
15 p = tuple(a[(i+1)%len(a)][k] - a[i][k] for k in range(2))
16 q = tuple(a[j][k] - a[i][k] for k in range(2))
17
18 return t * dir(p, q) < 0
19
20def lbreaks(p, q, tl, br):
21 # Adjust for horizontal or vertical
22 (tt, z) = (t, 0) if p[0] == q[0] else (-t, 1)
23
24 if min(p[1-z], q[1-z]) >= br[1-z] or max(p[1-z], q[1-z]) <= tl[1-z]:
25 return False
26 if p[z] == tl[z]:
27 return tt * (q[1-z]-p[1-z]) > 0
28 if p[z] == br[z]:
29 return tt * (q[1-z]-p[1-z]) < 0
30
31 return p[z] > tl[z] and p[z] < br[z]
32
33def admissible(a, i, j):
34 # Fix (see ../README.md): check given vertices to determine if the
35 # rectangle is fully external to the figure.
36 if external_by_vertex(a, i, j):
37 return False
38
39 tl = (min(a[i][0], a[j][0]), min(a[i][1], a[j][1]))
40 br = (max(a[i][0], a[j][0]), max(a[i][1], a[j][1]))
41 return not any(lbreaks(a[k], a[(k+1)%len(a)], tl, br) for k in range(len(a)))
42
43s = 0
44for i in range(len(a)):
45 for j in range(i+1, len(a)):
46 if admissible(a, i, j):
47 s = max(s, (abs(a[i][0]-a[j][0])+1)*(abs(a[i][1]-a[j][1])+1))
48print(s)

Generated with cgit - Back to sebastiano.tronto.net