Advent of Code 2026: Day One (Golang)
Another year, another chance to complete the Advent of Code. Lucky for me this year I have a fully staffed team at work, so I actually have time to enjoy the Advent of Code.
This year I am going to tackle the AoC in Golang, because I haven’t had a lot of time to really write code on my own, so here we go. You can find my solutions on GitHub: utahcon/adventofcode_2026. I have denied myself the help of agents (except the stupid Google AI responses in search).
You can read the problem yourself over at Day One of adventofcode.com. I will walk through my thought process, and present the code for each part of the problem. I will include code for part one and then the relevant changes for part two.
Part One
Boilerplate Code
I won’t be showing every import, unless it’s ourside of th standard library. Assume the usual imports, and everything is package main.
Inputs
I always copy the test inputs to test.input and the real data to real.input. We need to be able to swap between the two files easily. I use the flag package to parse the -real flag. Bonus flag here is count which lets us choose how many numbers to multiply.
count := flag.Int("count", 3, "Count of numbers to consider")
useReal := flag.Bool("real", false, "Use real data")
flag.Parse()
And then I figure out which file to open:
filename := "test.input"
if *useReal {
filename = "real.input"
}
Next up we need to read the file. Now in this case the file is rather small, otherwise we would do the right thing and implement reading this line-by-line with bufio.Scanner, but since it is small we can read it all into memory.
data, err := os.ReadFile(filename) // data is a []byte (a byte slice)
if err != nil {
log.Fatal(err) // prints the error and exits with status 1
}
We need somewhere to hold the counts:
counts := map[int]int{}
and then we flip through the fields (splitting on whitespace), converting the string to an integer, and then adding it to the counts map.
for _, field := range strings.Fields(string(data)) {
n, err := strconv.Atoi(field)
if err != nil {
log.Fatalf("invalid number %q: %v", field, err)
}
counts[n]++
}
Now we need to sort the counts. To do this we do a comparison and get the keys in a new slice:
keys := slices.SortedFunc(maps.Keys(counts), func(a, b int) int {
return cmp.Or(cmp.Compare(counts[b], counts[a]), cmp.Compare(a, b))
})
Just as a safety measure, we should check that -count is in range of our keys slice.
if *count < 1 || *count > len(keys) {
log.Fatalf("count must be between 1 and %d", len(keys))
}
Finally we can output our product of the top N keys:
fmt.Println("Part One:", product(keys[:*count]))
well… almost… the astute among you will realize we are missing a function… here it is:
func product(nums []int) int {
p := 1
for _, n := range nums {
p *= n
}
return p
}
Part Two
Part Two asks us to find the bottom three keys and output their product. Because we have done things right, we can simply pass those keys to the product function instead of the top three like we did before:
fmt.Println("Part Two:", product(keys[len(keys)-*count:]))
Final Block
Here is the final code for the simple Day One challenge:
package main
import (
"cmp"
"flag"
"fmt"
"log"
"maps"
"os"
"slices"
"strconv"
"strings"
)
func main() {
count := flag.Int("count", 3, "Count of numbers to consider")
useReal := flag.Bool("real", false, "Use real data")
flag.Parse()
filename := "test.input"
if *useReal {
filename = "real.input"
}
data, err := os.ReadFile(filename)
if err != nil {
log.Fatal(err)
}
// how many times each number appears
counts := map[int]int{}
for _, field := range strings.Fields(string(data)) {
n, err := strconv.Atoi(field)
if err != nil {
log.Fatalf("invalid number %q: %v", field, err)
}
counts[n]++
}
// most frequent first; ties broken by the smaller number
keys := slices.SortedFunc(maps.Keys(counts), func(a, b int) int {
return cmp.Or(cmp.Compare(counts[b], counts[a]), cmp.Compare(a, b))
})
if *count < 1 || *count > len(keys) {
log.Fatalf("count must be between 1 and %d", len(keys))
}
fmt.Println("Part One:", product(keys[:*count]))
fmt.Println("Part Two:", product(keys[len(keys)-*count:]))
}
func product(nums []int) int {
p := 1
for _, n := range nums {
p *= n
}
return p
}