diff options
| author | Sebastiano Tronto <sebastiano@tronto.net> | 2025-12-08 07:29:26 +0100 |
|---|---|---|
| committer | Sebastiano Tronto <sebastiano@tronto.net> | 2025-12-08 07:29:26 +0100 |
| commit | 5d69f923116a2b251a445c0cb9e6f23551ad686a (patch) | |
| tree | 42dfa409c6198f6796b4e8513d01bbdb0a00725e /2025 | |
| parent | 01c2cd358b3f693181fa86e82414b3e32f9a890e (diff) | |
| download | aoc-5d69f923116a2b251a445c0cb9e6f23551ad686a.tar.gz aoc-5d69f923116a2b251a445c0cb9e6f23551ad686a.zip | |
Day 8 2025
Diffstat (limited to '2025')
| -rw-r--r-- | 2025/08/a.py | 30 | ||||
| -rw-r--r-- | 2025/08/b.py | 26 | ||||
| -rw-r--r-- | 2025/README.md | 12 |
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 @@ | |||
| 1 | import fileinput | 1 | import fileinput |
| 2 | 2 | ||
| 3 | N = 1000 # Change to 10 for test case | ||
| 4 | |||
| 3 | with fileinput.input() as lines: | 5 | with 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 | |
| 8 | r = range(len(pts)) | ||
| 9 | |||
| 10 | def dist(p, q): | ||
| 11 | return (p[0]-q[0])**2 + (p[1]-q[1])**2 + (p[2]-q[2])**2 | ||
| 12 | |||
| 13 | d = sorted([(dist(pts[i], pts[j]), i, j) for i in r for j in r if j > i]) | ||
| 14 | |||
| 15 | rep = [i for i in r] | ||
| 16 | |||
| 17 | def findrep(i): | ||
| 18 | return i if rep[i] == i else findrep(rep[i]) | ||
| 19 | |||
| 20 | def joinrep(i, j): | ||
| 21 | rep[findrep(i)] = findrep(j) | ||
| 22 | |||
| 23 | for 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 | ||
| 28 | sizes = [[0, i] for i in r] | ||
| 29 | for i in r: | ||
| 30 | sizes[findrep(i)][0] += 1 | ||
| 31 | sizes.sort() | ||
| 32 | print(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 @@ | |||
| 1 | import fileinput | ||
| 2 | |||
| 3 | with fileinput.input() as lines: | ||
| 4 | pts = [tuple(int(x) for x in line[:-1].split(',')) for line in lines] | ||
| 5 | |||
| 6 | r = range(len(pts)) | ||
| 7 | |||
| 8 | def dist(p, q): | ||
| 9 | return (p[0]-q[0])**2 + (p[1]-q[1])**2 + (p[2]-q[2])**2 | ||
| 10 | |||
| 11 | d = sorted([(dist(pts[i], pts[j]), i, j) for i in r for j in r if j > i]) | ||
| 12 | |||
| 13 | rep = [i for i in r] | ||
| 14 | |||
| 15 | def findrep(i): | ||
| 16 | return i if rep[i] == i else findrep(rep[i]) | ||
| 17 | |||
| 18 | def joinrep(i, j): | ||
| 19 | rep[findrep(i)] = findrep(j) | ||
| 20 | |||
| 21 | for _, j, k in d: | ||
| 22 | if findrep(j) != findrep(k): | ||
| 23 | joinrep(j, k) | ||
| 24 | sol = pts[j][0] * pts[k][0] | ||
| 25 | |||
| 26 | print(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 | ``` |
| 13 | Day -Part 1- -Part 2- | 13 | Day -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). | |||
| 100 | I added a second solution for part 2 that does not use a map, but only | 101 | I added a second solution for part 2 that does not use a map, but only |
| 101 | lists. This could be seen as a dynamic programming problem where the | 102 | lists. This could be seen as a dynamic programming problem where the |
| 102 | iterative implementation is more intuitive than the recursive one. | 103 | iterative implementation is more intuitive than the recursive one. |
| 104 | |||
| 105 | ### Day 8: Playground | ||
| 106 | |||
| 107 | This one required some optimization effort for part 2... unless one is | ||
| 108 | already familiar with [the algorithm | ||
| 109 | described](https://en.wikipedia.org/wiki/Kruskal%27s_algorithm) | ||
| 110 | in the problem statement, and I was from back in the days of competitive | ||
| 111 | programming. In the end it was mostly a matter of figuring out the correct | ||
| 112 | [data structure to represent the groups of joint | ||
| 113 | 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). | ||
