Advent of Code 2026: Day Two (Golang)
Welcome back to Day Two! Since we are going to be using the same flags and input code for each day of our challenge, the first step today is abstracting that part into a reusable library.
Abstract Input Control
Previously our file tree looked like this:
.
├── day1
│ ├── main.go
│ ├── main_test.go
│ ├── real.input
│ └── test.input
└── README.md
So we will be creating a new top level directory called internal to hold our reusable code.
.
├── day1
│ ├── main.go
│ ├── main_test.go
│ ├── real.input
│ └── test.input
├── go.mod
├── internal
│ └── input
│ └── input.go
└── README.md
And here is what that looks like
package input
import (
"flag"
"os"
)
const (
TestFile = "test.input"
RealFile = "real.input"
)
var useReal = flag.Bool("real", false, "use "+RealFile+" instead of "+TestFile)
// Filename returns the input file chosen by the -real flag.
func Filename() string {
// in case main hasn't called flag.Parse, we check and run if needed
if !flag.Parsed() {
flag.Parse()
}
if *useReal {
return RealFile
}
return TestFile
}
// Read returns the contents of the input file chosen by the -real flag.
func Read() ([]byte, error) {
return os.ReadFile(Filename())
}
Funny enough they decided to give us multiple tests this time, so YMMV
Part One
Now that we have a fancy new shared internal package, we will add it to our main.go
package main
import (
"log"
"github.com/utahcon/adventofcode_2026/internal/input"
)
func main() {
data, err := input.Read()
if err != nil {
log.Fatal(err)
}
_ = data //placeholder for unused variable
}
Very nice and clean, I like it.
The path
The robot in the problem follows a continuous path, of unknown length. We do know it is a pattern of roygbv. We don’t
want to construct an insanely long string just to track things, so we need to be creative a bit here. So we will create a
constant, and as needed, we will add it to the end of our string.
const (
PATHPATTERN = "roygbv"
)
And let’s set the first part of our path in main(), and we will create a pathIdx to track how far down the path we
are.
path := PATHPATTERN
pathIdx := 0
So now we will read the next step in our program (which is a single character in our input), and then find the next instance of it in our path. Since data is still a byte slice, we need to convert it to a string.
program := strings.TrimSpace(string(data)) //drops trailing whitespace
for i, ch := range program {
// i is the byte index, ch is the rune (one character)
fmt.Printf("%d: %c\n", i, ch)
}
Now we need to find the next iteration of the rune in our current path
// find the next instance of the rune in our path, or -1
offset := strings.IndexRune(path[pathIdx:], ch)
for offset == -1 {
// extend the path
path += PATHPATTERN
// look again
offset = strings.IndexRune(path[pathIdx:], ch)
}
// update our pathIdx
// the +1 move the search to the next space
pathIdx = pathIdx + offset + 1
Now if you take our pathIdx, and subtract one, we know where the robot is!
fmt.Printf("Distance Traveled: %d\n", pathIdx-1)
Part Two
For Part Two they want to trick us into sorting through the string, but Golang offers us some cheats here. Since we know the pattern to the longest path is rvbgyo, we can simply count the instances of each rune in our string, and build a new string with that many copies of the rune.
First we will add to our constants the LONGORDER:
const (
PATHPATTERN = "roygbv"
LONGORDER = "rvbgyo"
)
Now we will create a string builder to efficiently build a string. Then loop through the LONGORDER to count the number of times our rune shows up using strings.Count, and simply write that many copies using strings.Repeat in our WriteString method.
var longProgram string
var sb strings.Builder
for _, c := range LONGORDER {
n := strings.Count(program, string(c))
sb.WriteString(strings.Repeat(string(c), n))
longProgram = sb.String()
}
Now since we are running the loop for counting the pathIdx twice, let’s move it to a stand-alone function.
func distance(program string) int {
path := PATHPATTERN
pathIdx := 1
for _, ch := range program {
offset := strings.IndexRune(path[pathIdx:], ch)
for offset == -1 {
path += PATHPATTERN
offset = strings.IndexRune(path[pathIdx:], ch)
}
pathIdx = pathIdx + offset + 1
}
return pathIdx - 1
}
And let’s give our outputs a little change-a-roo to call that function:
fmt.Printf("Part One | Distance Traveled: %d\n", distance(program))
fmt.Printf("Part Two | Distance Traveled: %d\n", distance(longProgram))
Final
Alright, that wraps up this Advent of Code Day Two, with two shiny new stars! I am really enjoying this, and can’t wait for tomorrow’s puzzle!
For the full final code, please see utahcon/adventofcode_2026