tangled
alpha
login
or
join now
eldridge.cam
/
advent-of-code
2
fork
atom
Advent of Code solutions
2
fork
atom
overview
issues
pulls
pipelines
2025 day 8 but i did it yesterday and forgot to commmit
eldridge.cam
3 months ago
b11e4413
755b734d
+56
-9
2 changed files
expand all
collapse all
unified
split
2025
8
p1.tri
p2.tri
+10
-9
2025/8/p1.tri
···
1
1
import "trilogy:debug" use dbg
2
2
import "trilogy:io" use readlines
3
3
import "trilogy:parsec" use parse, sep_by, char, integer, per_line
4
4
-
import "trilogy:array" use push, collect, sort_by, take, reduce
4
4
+
import "trilogy:array" use collect, sort_by, take, reduce
5
5
import "trilogy:core" use length
6
6
import "trilogy:iterator" as it
7
7
import "trilogy:set" use union
8
8
+
import "trilogy:heap" use heap
8
9
9
10
func dist [x1, y1, z1]:[x2, y2, z2] = (x1-x2)**2 + (y1-y2)**2 + (z1-z2)**2
10
10
-
func dist_asc a b = dist a < dist b
11
11
+
func dist_asc a b = dist a <= dist b
11
12
12
13
proc main!() {
13
14
let points = readlines
14
15
|> it::map (parse (sep_by (char ',') integer))
15
16
|> collect
16
17
18
18
+
let edge_heap = heap dist_asc
19
19
+
17
20
let mut edges = []
18
21
let mut i = 0
19
22
while i < length points {
20
23
let mut j = i + 1
21
24
while j < length points {
22
22
-
push!(edges, points.i:points.j)
25
25
+
edge_heap::push!(edges, points.i:points.j)
23
26
j += 1
24
27
}
25
28
i += 1
26
29
}
27
27
-
dbg!(length edges)
28
28
-
edges sort_by= dist_asc
29
29
-
dbg!("sorted")
30
30
-
edges take= 1000
31
31
-
dbg!("taken")
32
30
33
31
let mut sets = {| pos => [| pos |] for pos in points |}
34
34
-
for a:b in edges {
32
32
+
let mut i = 0
33
33
+
while i < 1000 {
34
34
+
i += 1
35
35
+
let a:b = edge_heap::pop!(edges)
35
36
sets.a union= sets.b
36
37
for el in sets.a {
37
38
sets.el = sets.a
+46
2025/8/p2.tri
···
1
1
+
import "trilogy:debug" use dbg
2
2
+
import "trilogy:io" use readlines
3
3
+
import "trilogy:parsec" use parse, sep_by, char, integer, per_line
4
4
+
import "trilogy:array" use collect
5
5
+
import "trilogy:core" use length
6
6
+
import "trilogy:iterator" as it
7
7
+
import "trilogy:set" use union
8
8
+
import "trilogy:heap" use heap
9
9
+
10
10
+
func dist [x1, y1, z1]:[x2, y2, z2] = (x1-x2)**2 + (y1-y2)**2 + (z1-z2)**2
11
11
+
func dist_asc a b = dist a <= dist b
12
12
+
13
13
+
proc main!() {
14
14
+
let points = readlines
15
15
+
|> it::map (parse (sep_by (char ',') integer))
16
16
+
|> collect
17
17
+
18
18
+
let edge_heap = heap dist_asc
19
19
+
20
20
+
let mut edges = []
21
21
+
let mut i = 0
22
22
+
while i < length points {
23
23
+
let mut j = i + 1
24
24
+
while j < length points {
25
25
+
edge_heap::push!(edges, points.i:points.j)
26
26
+
j += 1
27
27
+
}
28
28
+
i += 1
29
29
+
}
30
30
+
31
31
+
let mut sets = {| pos => [| pos |] for pos in points |}
32
32
+
let mut i = 0
33
33
+
while length edges > 0 {
34
34
+
i += 1
35
35
+
let a:b = edge_heap::pop!(edges)
36
36
+
if sets.a == sets.b { continue unit }
37
37
+
sets.a union= sets.b
38
38
+
if length (sets.a) == length points {
39
39
+
dbg!(a.0 * b.0)
40
40
+
return 0
41
41
+
}
42
42
+
for el in sets.a { # NOTE: this for loop is the slowest part...
43
43
+
sets.el = sets.a
44
44
+
}
45
45
+
}
46
46
+
}