aboutsummaryrefslogtreecommitdiff
path: root/2025
diff options
context:
space:
mode:
authorSebastiano Tronto <sebastiano@tronto.net>2025-12-08 07:29:26 +0100
committerSebastiano Tronto <sebastiano@tronto.net>2025-12-08 07:29:26 +0100
commit5d69f923116a2b251a445c0cb9e6f23551ad686a (patch)
tree42dfa409c6198f6796b4e8513d01bbdb0a00725e /2025
parent01c2cd358b3f693181fa86e82414b3e32f9a890e (diff)
downloadaoc-5d69f923116a2b251a445c0cb9e6f23551ad686a.tar.gz
aoc-5d69f923116a2b251a445c0cb9e6f23551ad686a.zip
Day 8 2025
Diffstat (limited to '2025')
-rw-r--r--2025/08/a.py30
-rw-r--r--2025/08/b.py26
-rw-r--r--2025/README.md12
3 files changed, 66 insertions, 2 deletions
diff --git a/2025/08/a.py b/2025/08/a.py
index 5fdb11b..1980414 100644
--- a/2025/08/a.py
+++ b/2025/08/a.py
@@ -1,6 +1,32 @@
1import fileinput 1import fileinput
2 2
3N = 1000 # Change to 10 for test case
4
3with fileinput.input() as lines: 5with fileinput.input() as lines:
4 for line in lines: 6 pts = [tuple(int(x) for x in line[:-1].split(',')) for line in lines]
5 ... 7
8r = range(len(pts))
9
10def dist(p, q):
11 return (p[0]-q[0])**2 + (p[1]-q[1])**2 + (p[2]-q[2])**2
12
13d = sorted([(dist(pts[i], pts[j]), i, j) for i in r for j in r if j > i])
14
15rep = [i for i in r]
16
17def findrep(i):
18 return i if rep[i] == i else findrep(rep[i])
19
20def joinrep(i, j):
21 rep[findrep(i)] = findrep(j)
22
23for i in range(N):
24 j, k = d[i][1], d[i][2]
25 if findrep(j) != findrep(k):
26 joinrep(j, k)
6 27
28sizes = [[0, i] for i in r]
29for i in r:
30 sizes[findrep(i)][0] += 1
31sizes.sort()
32print(sizes[-1][0] * sizes[-2][0] * sizes[-3][0])
diff --git a/2025/08/b.py b/2025/08/b.py
new file mode 100644
index 0000000..4a6d610
--- /dev/null
+++ b/2025/08/b.py
@@ -0,0 +1,26 @@
1import fileinput
2
3with fileinput.input() as lines:
4 pts = [tuple(int(x) for x in line[:-1].split(',')) for line in lines]
5
6r = range(len(pts))
7
8def dist(p, q):
9 return (p[0]-q[0])**2 + (p[1]-q[1])**2 + (p[2]-q[2])**2
10
11d = sorted([(dist(pts[i], pts[j]), i, j) for i in r for j in r if j > i])
12
13rep = [i for i in r]
14
15def findrep(i):
16 return i if rep[i] == i else findrep(rep[i])
17
18def joinrep(i, j):
19 rep[findrep(i)] = findrep(j)
20
21for _, j, k in d:
22 if findrep(j) != findrep(k):
23 joinrep(j, k)
24 sol = pts[j][0] * pts[k][0]
25
26print(sol)
diff --git a/2025/README.md b/2025/README.md
index 4ca1d2e..19fbb29 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 8 00:29:14 00:33:02
14 7 00:05:27 00:20:40 15 7 00:05:27 00:20:40
15 6 00:13:38 01:49:24 16 6 00:13:38 01:49:24
16 5 00:04:39 00:22:19 17 5 00:04:39 00:22:19
@@ -100,3 +101,14 @@ end there from the previous row (that can be one or two tachyons).
100I added a second solution for part 2 that does not use a map, but only 101I added a second solution for part 2 that does not use a map, but only
101lists. This could be seen as a dynamic programming problem where the 102lists. This could be seen as a dynamic programming problem where the
102iterative implementation is more intuitive than the recursive one. 103iterative implementation is more intuitive than the recursive one.
104
105### Day 8: Playground
106
107This one required some optimization effort for part 2... unless one is
108already familiar with [the algorithm
109described](https://en.wikipedia.org/wiki/Kruskal%27s_algorithm)
110in the problem statement, and I was from back in the days of competitive
111programming. In the end it was mostly a matter of figuring out the correct
112[data structure to represent the groups of joint
113boxes](https://en.wikipedia.org/wiki/Disjoint-set_data_structure) (or
114a matter of remembering how it is implemented, if one already knows it).

Generated with cgit - Back to sebastiano.tronto.net