Created
November 19, 2025 02:09
-
-
Save bozdoz/b19a56159a901eafb6205d071c7b6f34 to your computer and use it in GitHub Desktop.
Day 06 - 2019
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "io" | |
| "log" | |
| "github.com/bozdoz/advent-of-code-2025/utils" | |
| ) | |
| type d = []string | |
| func partOne(data d) (ans any) { | |
| orbits := newOrbits(data) | |
| count := 0 | |
| for _, v := range orbits.depth { | |
| count += v | |
| } | |
| return count | |
| } | |
| func partTwo(data d) (ans any) { | |
| seen := map[string]bool{} | |
| orbits := newOrbits(data) | |
| cur := "YOU" | |
| for cur != "COM" { | |
| next := orbits.objects[cur] | |
| seen[next] = true | |
| cur = next | |
| } | |
| // find orbit in common | |
| cur = "SAN" | |
| for !seen[cur] { | |
| cur = orbits.objects[cur] | |
| } | |
| common := orbits.depth[cur] | |
| // diff of YOU vs common | |
| // diff of SAN vs common | |
| // minus 2 because YOU and SAN don't count | |
| transfers := orbits.depth["YOU"] - common + orbits.depth["SAN"] - common - 2 | |
| return transfers | |
| } | |
| func init() { | |
| log.SetOutput(io.Discard) | |
| } | |
| var day = utils.NewDay[d]() | |
| func main() { | |
| day.Read("./input.txt") | |
| day.Run(partOne) | |
| day.Run(partTwo) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "strings" | |
| ) | |
| type Orbits struct { | |
| objects map[string]string | |
| // distance from center | |
| depth map[string]int | |
| } | |
| func newOrbits(data d) *Orbits { | |
| orbits := Orbits{ | |
| objects: map[string]string{}, | |
| depth: map[string]int{}, | |
| } | |
| for _, val := range data { | |
| arr := strings.Split(val, ")") | |
| orbits.objects[arr[1]] = arr[0] | |
| } | |
| for k, v := range orbits.objects { | |
| if v == "COM" { | |
| orbits.depth[k] = 1 | |
| } else { | |
| // iterate orbits until we get COM | |
| deps := []string{k, v} | |
| // get next | |
| for { | |
| k = v | |
| v = orbits.objects[k] | |
| if v == "COM" { | |
| // first time getting to the core | |
| // imagine c:b b:COM | |
| for i := range deps { | |
| orbits.depth[deps[i]] = len(deps) - i | |
| } | |
| break | |
| } | |
| if orbits.depth[v] > 0 { | |
| // imagine d:c c:b | |
| established := orbits.depth[v] | |
| for i := range deps { | |
| orbits.depth[deps[i]] = established + len(deps) - i | |
| } | |
| break | |
| } | |
| // keep going | |
| deps = append(deps, v) | |
| } | |
| } | |
| } | |
| return &orbits | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment