Solutions for Advent of Code challenges written in Go.
aoc/
├── utils/ # Common utility functions
│ └── utils.go # File reading, parsing helpers
├── q1/ # Day 1 solution
│ ├── q1.go # Solution code
│ ├── input.txt # Actual puzzle input
│ └── test_input.txt # Sample/test input
├── q2/ # Day 2 solution
│ └── ...
└── go.mod
From the project root:
go build -o aoc# Run question 1 with actual input
./aoc -q 1
# Run question 1 with test input
./aoc -q 1 -test
# Run question 2 with actual input
./aoc -q 2
# Run question 2 with test input
./aoc -q 2 -testYou can also use go run:
# Run with actual input
go run main.go -q 1
# Run with test input
go run main.go -q 1 -test- Create a new directory (e.g.,
q2/) - Create
q2/q2.gowith the following template:
package q2
// Solve returns the solutions for part 1 and part 2
func Solve(lines []string) (int, int) {
// Part 1 solution
part1 := 0
// Part 2 solution
part2 := 0
return part1, part2
}- Add your puzzle input to
q2/input.txt - Add sample input to
q2/test_input.txt - Update
main.goto import and call your new solution:
import (
// ... existing imports
"github.com/sidntrivedi012/aoc/q2"
)
// In the switch statement, add:
case 2:
part1, part2 = q2.Solve(lines)- Rebuild:
go build -o aoc - Run:
./aoc -q 2 -test
The utils package provides:
ReadLines(filename)- Read file as slice of strings (one per line)ReadFile(filename)- Read entire file as a single stringParseInts(lines)- Convert string slice to int sliceSplitByEmptyLines(lines)- Split input into groups separated by blank lines
- Add sample input from the problem to
test_input.txt - Run with
-testflag to verify logic with sample data - Add actual puzzle input to
input.txt - Run without flags to get the final answer
-
Build the binary:
go build -o aoc
-
Add your inputs:
- Put sample input in
q1/test_input.txt - Put actual puzzle input in
q1/input.txt
- Put sample input in
-
Test with sample input:
./aoc -q 1 -test
-
Run with actual input:
./aoc -q 1