2025 Day 8 Complete
This commit is contained in:
1000
2025/day08/input
Normal file
1000
2025/day08/input
Normal file
File diff suppressed because it is too large
Load Diff
83
2025/day08/main.go
Normal file
83
2025/day08/main.go
Normal file
@@ -0,0 +1,83 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"cmp"
|
||||
"fmt"
|
||||
"maps"
|
||||
"slices"
|
||||
"strings"
|
||||
|
||||
h "git.bullercodeworks.com/brian/adventofcode/helpers"
|
||||
)
|
||||
|
||||
func main() {
|
||||
inp := h.StdinToStringSlice()
|
||||
solve(inp)
|
||||
}
|
||||
|
||||
func solve(inp []string) {
|
||||
var part1, part2 int
|
||||
pts := inputToMap(inp)
|
||||
circuits := []map[h.Coordinate3d]struct{}{}
|
||||
for _, b := range pts {
|
||||
circuits = append(circuits, map[h.Coordinate3d]struct{}{b: {}})
|
||||
}
|
||||
|
||||
pairs := [][2]h.Coordinate3d{}
|
||||
for i, b1 := range pts {
|
||||
for _, b2 := range pts[i+1:] {
|
||||
pairs = append(pairs, [2]h.Coordinate3d{b1, b2})
|
||||
}
|
||||
}
|
||||
slices.SortFunc(pairs, func(a, b [2]h.Coordinate3d) int {
|
||||
return cmp.Compare(a[0].Distance(a[1]), b[0].Distance(b[1]))
|
||||
})
|
||||
|
||||
for i, p := range pairs {
|
||||
var c1, c2 int
|
||||
for i, c := range circuits {
|
||||
if _, ok := c[p[0]]; ok {
|
||||
c1 = i
|
||||
}
|
||||
if _, ok := c[p[1]]; ok {
|
||||
c2 = i
|
||||
}
|
||||
}
|
||||
|
||||
if c1 != c2 {
|
||||
maps.Copy(circuits[c1], circuits[c2])
|
||||
circuits = slices.Delete(circuits, c2, c2+1)
|
||||
}
|
||||
|
||||
if i+1 == 1000 {
|
||||
slices.SortFunc(circuits, func(a, b map[h.Coordinate3d]struct{}) int {
|
||||
return -cmp.Compare(len(a), len(b))
|
||||
})
|
||||
part1 = len(circuits[0]) * len(circuits[1]) * len(circuits[2])
|
||||
}
|
||||
if len(circuits) == 1 {
|
||||
part2 = (p[0].X * p[1].X)
|
||||
break
|
||||
}
|
||||
}
|
||||
fmt.Println("# Part 1")
|
||||
fmt.Println(part1)
|
||||
fmt.Println()
|
||||
fmt.Println("# Part 2")
|
||||
fmt.Println(part2)
|
||||
}
|
||||
|
||||
func inputToMap(inp []string) []h.Coordinate3d {
|
||||
var ret []h.Coordinate3d
|
||||
for i := range inp {
|
||||
pts := strings.Split(inp[i], ",")
|
||||
ret = append(ret,
|
||||
h.Coordinate3d{
|
||||
X: h.Atoi(pts[0]),
|
||||
Y: h.Atoi(pts[1]),
|
||||
Z: h.Atoi(pts[2]),
|
||||
},
|
||||
)
|
||||
}
|
||||
return ret
|
||||
}
|
||||
140
2025/day08/problem
Normal file
140
2025/day08/problem
Normal file
@@ -0,0 +1,140 @@
|
||||
[1]Advent of Code
|
||||
|
||||
• [2][About]
|
||||
• [3][Events]
|
||||
• [4][Shop]
|
||||
• [5][Settings]
|
||||
• [6][Log Out]
|
||||
|
||||
br0xen [7](AoC++) 14*
|
||||
|
||||
<y>[8]2025</y>
|
||||
|
||||
• [9][Calendar]
|
||||
• [10][AoC++]
|
||||
• [11][Sponsors]
|
||||
• [12][Leaderboards]
|
||||
• [13][Stats]
|
||||
|
||||
Our [14]sponsors help make Advent of Code possible:
|
||||
[15]Kilo Code - The fastest growing coding agent because it is open. #1 on
|
||||
OpenRouter. 500k+ Kilo Coders. 6.1T tokens per month. Try it today!
|
||||
|
||||
--- Day 8: Playground ---
|
||||
|
||||
Equipped with a new understanding of teleporter maintenance, you
|
||||
confidently step onto the repaired teleporter pad.
|
||||
|
||||
You rematerialize on an unfamiliar teleporter pad and find yourself in a
|
||||
vast underground space which contains a giant playground!
|
||||
|
||||
Across the playground, a group of Elves are working on setting up an
|
||||
ambitious Christmas decoration project. Through careful rigging, they have
|
||||
suspended a large number of small electrical [16]junction boxes.
|
||||
|
||||
Their plan is to connect the junction boxes with long strings of lights.
|
||||
Most of the junction boxes don't provide electricity; however, when two
|
||||
junction boxes are connected by a string of lights, electricity can pass
|
||||
between those two junction boxes.
|
||||
|
||||
The Elves are trying to figure out which junction boxes to connect so that
|
||||
electricity can reach every junction box. They even have a list of all of
|
||||
the junction boxes' positions in 3D space (your puzzle input).
|
||||
|
||||
For example:
|
||||
|
||||
162,817,812
|
||||
57,618,57
|
||||
906,360,560
|
||||
592,479,940
|
||||
352,342,300
|
||||
466,668,158
|
||||
542,29,236
|
||||
431,825,988
|
||||
739,650,466
|
||||
52,470,668
|
||||
216,146,977
|
||||
819,987,18
|
||||
117,168,530
|
||||
805,96,715
|
||||
346,949,466
|
||||
970,615,88
|
||||
941,993,340
|
||||
862,61,35
|
||||
984,92,344
|
||||
425,690,689
|
||||
|
||||
This list describes the position of 20 junction boxes, one per line. Each
|
||||
position is given as X,Y,Z coordinates. So, the first junction box in the
|
||||
list is at X=162, Y=817, Z=812.
|
||||
|
||||
To save on string lights, the Elves would like to focus on connecting
|
||||
pairs of junction boxes that are as close together as possible according
|
||||
to [17]straight-line distance. In this example, the two junction boxes
|
||||
which are closest together are 162,817,812 and 425,690,689.
|
||||
|
||||
By connecting these two junction boxes together, because electricity can
|
||||
flow between them, they become part of the same circuit. After connecting
|
||||
them, there is a single circuit which contains two junction boxes, and the
|
||||
remaining 18 junction boxes remain in their own individual circuits.
|
||||
|
||||
Now, the two junction boxes which are closest together but aren't already
|
||||
directly connected are 162,817,812 and 431,825,988. After connecting them,
|
||||
since 162,817,812 is already connected to another junction box, there is
|
||||
now a single circuit which contains three junction boxes and an additional
|
||||
17 circuits which contain one junction box each.
|
||||
|
||||
The next two junction boxes to connect are 906,360,560 and 805,96,715.
|
||||
After connecting them, there is a circuit containing 3 junction boxes, a
|
||||
circuit containing 2 junction boxes, and 15 circuits which contain one
|
||||
junction box each.
|
||||
|
||||
The next two junction boxes are 431,825,988 and 425,690,689. Because these
|
||||
two junction boxes were already in the same circuit, nothing happens!
|
||||
|
||||
This process continues for a while, and the Elves are concerned that they
|
||||
don't have enough extension cables for all these circuits. They would like
|
||||
to know how big the circuits will be.
|
||||
|
||||
After making the ten shortest connections, there are 11 circuits: one
|
||||
circuit which contains 5 junction boxes, one circuit which contains 4
|
||||
junction boxes, two circuits which contain 2 junction boxes each, and
|
||||
seven circuits which each contain a single junction box. Multiplying
|
||||
together the sizes of the three largest circuits (5, 4, and one of the
|
||||
circuits of size 2) produces 40.
|
||||
|
||||
Your list contains many junction boxes; connect together the 1000 pairs of
|
||||
junction boxes which are closest together. Afterward, what do you get if
|
||||
you multiply together the sizes of the three largest circuits?
|
||||
|
||||
To begin, [18]get your puzzle input.
|
||||
|
||||
Answer: [19]_____________________ [20][ [Submit] ]
|
||||
|
||||
You can also [Shareon [21]Bluesky [22]Twitter [23]Mastodon] this puzzle.
|
||||
|
||||
References
|
||||
|
||||
Visible links
|
||||
1. https://adventofcode.com/
|
||||
2. https://adventofcode.com/2025/about
|
||||
3. https://adventofcode.com/2025/events
|
||||
4. https://adventofcode.com/2025/shop
|
||||
5. https://adventofcode.com/2025/settings
|
||||
6. https://adventofcode.com/2025/auth/logout
|
||||
7. Advent of Code Supporter
|
||||
https://adventofcode.com/2025/support
|
||||
8. https://adventofcode.com/2025
|
||||
9. https://adventofcode.com/2025
|
||||
10. https://adventofcode.com/2025/support
|
||||
11. https://adventofcode.com/2025/sponsors
|
||||
12. https://adventofcode.com/2025/leaderboard/private
|
||||
13. https://adventofcode.com/2025/stats
|
||||
14. https://adventofcode.com/2025/sponsors
|
||||
15. https://adventofcode.com/2025/sponsors/redirect?url=https%3A%2F%2Fkilo%2Eai%2F
|
||||
16. https://en.wikipedia.org/wiki/Junction_box
|
||||
17. https://en.wikipedia.org/wiki/Euclidean_distance
|
||||
18. https://adventofcode.com/2025/day/8/input
|
||||
21. https://bsky.app/intent/compose?text=%22Playground%22+%2D+Day+8+%2D+Advent+of+Code+2025+%23AdventOfCode+https%3A%2F%2Fadventofcode%2Ecom%2F2025%2Fday%2F8
|
||||
22. https://twitter.com/intent/tweet?text=%22Playground%22+%2D+Day+8+%2D+Advent+of+Code+2025&url=https%3A%2F%2Fadventofcode%2Ecom%2F2025%2Fday%2F8&related=ericwastl&hashtags=AdventOfCode
|
||||
23. javascript:void(0);
|
||||
20
2025/day08/testinput
Normal file
20
2025/day08/testinput
Normal file
@@ -0,0 +1,20 @@
|
||||
162,817,812
|
||||
57,618,57
|
||||
906,360,560
|
||||
592,479,940
|
||||
352,342,300
|
||||
466,668,158
|
||||
542,29,236
|
||||
431,825,988
|
||||
739,650,466
|
||||
52,470,668
|
||||
216,146,977
|
||||
819,987,18
|
||||
117,168,530
|
||||
805,96,715
|
||||
346,949,466
|
||||
970,615,88
|
||||
941,993,340
|
||||
862,61,35
|
||||
984,92,344
|
||||
425,690,689
|
||||
@@ -1,6 +1,9 @@
|
||||
package aoc
|
||||
|
||||
import "fmt"
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
)
|
||||
|
||||
type Coordinate3d struct {
|
||||
X, Y, Z int
|
||||
@@ -71,6 +74,13 @@ func (c *Coordinate3d) GetDownCoord() *Coordinate3d {
|
||||
Z: c.Z - 1,
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Coordinate3d) Distance(c2 Coordinate3d) float64 {
|
||||
return math.Sqrt(math.Pow(float64(c2.X-c.X), 2) +
|
||||
math.Pow(float64(c2.Y-c.Y), 2) +
|
||||
math.Pow(float64(c2.Z-c.Z), 2))
|
||||
}
|
||||
|
||||
func (c Coordinate3d) String() string {
|
||||
return fmt.Sprintf("[X:%d, Y:%d, Z:%d]", c.X, c.Y, c.Z)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user