This repository contains a C++ template for Advent of Code, supporting multiple years.
- Multi-year support - Organize solutions by year (2025, 2024, etc.)
- CMake-based build system - Easy compilation and dependency management
- Google Test integration - Unit tests for each day's solution
- Automatic input fetching - Download puzzle inputs directly from adventofcode.com
- Performance measurement - High-precision execution time tracking
- Modular structure - Each day is a separate module with its own tests
- Day generator script - Automatically scaffold new day solutions
- Google C++ style enforcement - clang-format integration
-
Root Files:
CMakeLists.txt- Root build configuration.clang-format- Google C++ style configuration.clang-format-ignore- Exclude directories from formattingmain.cpp- Main runner with performance measurement.env.example- Template for AOC session cookie
-
scripts/ - Build and generation scripts
generate_day.sh- Day scaffolding generatorcheck_format.sh- Format verification script- templates/day/ - Template files for day generation
solution.h.templatesolution.cpp.templatetest.cpp.templateCMakeLists.txt.templateinput.txt.templateinput_test.txt.template
-
src/ - Source code organized by year
- common/ - Shared utilities
utils.h/cpp- String parsing, file I/O helpersinput_fetcher.h/cpp- AOC input downloader
- YYYY/ (e.g., 2025, 2024) - Year-specific solutions
- day_N/ - Individual day solutions
CMakeLists.txt- Build configurationsolution.h/cpp- part1() and part2() implementationstest.cpp- Google Test unit tests- tests/ - Input files
input_test.txt- Example input from puzzleinput.txt- Real puzzle input
- day_N/ - Individual day solutions
- common/ - Shared utilities
-
build/ - Generated build directory (not in version control)
- C++17 compiler (GCC 7+, Clang 5+, MSVC 2017+)
- CMake 3.14+
- libcurl (for input fetching)
- clang-format (optional, for code formatting)
macOS:
brew install cmake curl clang-formatUbuntu/Debian:
sudo apt-get install cmake libcurl4-openssl-dev clang-formatArch Linux:
sudo pacman -S cmake curl clang-
Clone or navigate to this repository
-
Configure AOC session cookie (for automatic input fetching):
cp .env.example .env
Edit
.envand add your session cookie:-
Go to adventofcode.com and log in
-
Open browser DevTools (F12) β Application/Storage β Cookies
-
Copy the value of the
sessioncookie -
Paste it in
.env:AOC_SESSION=your_session_cookie_here
-
-
Build the project:
mkdir build cd build cmake .. make -
Setup LSP/clangd support (for editor integration):
The project is already configured to export compile commands. After building, create a symlink in the project root:
ln -sf build/compile_commands.json compile_commands.json
This enables clangd (used by VSCode, Neovim, etc.) to provide proper code completion and diagnostics.
./aoc [options]
Options:
-y, --year YEAR Specify year (default: 2025)
-d, --day DAY Specify day (1-25, default: all days)
-p, --part PART Specify part (1-2, default: both parts)
-h, --help Show help message# Run all days of 2025 (default year)
./aoc
# Run all days of 2024
./aoc --year 2024
# Run day 1 of 2025
./aoc -d 1
# Run day 5 of 2024
./aoc -y 2024 -d 5
# Run day 10, part 2 only
./aoc -d 10 -p 2
# Run 2024 day 5 part 2
./aoc --year 2024 --day 5 --part 2The runner will:
- Automatically fetch inputs if not present (requires session cookie in
.env) - Run the specified solutions
- Display results in a formatted table with execution times
# Run all tests
cd build
ctest --output-on-failure
# Run specific day tests
./2025_day_1_test
./2024_day_1_testUse the day generator script:
./scripts/generate_day.sh <year> <day>Example:
./scripts/generate_day.sh 2025 5This will:
- Create the directory structure:
src/2025/day_5/ - Generate all necessary files (solution.h, solution.cpp, test.cpp, CMakeLists.txt)
- Create placeholder input files
- Update
main.cppwith includes and runner calls - Ready to rebuild and implement!
Next steps after generation:
Note: the generation script is already doing this now.
cd build
cmake ..
make
./aoc -y 2025 -d 5 # Fetch input and runThis project uses Google C++ Style Guide enforced via clang-format.
# Auto-format all C++ files
cd build
make format# Verify formatting without modifying files (useful for CI)
cd build
make check-formatThe .clang-format file in the project root defines the formatting rules. All code should be formatted before committing.
The common/utils.h library provides helpful functions:
readFile(filepath)- Read entire file as stringreadLines(filepath)- Read file as vector of linessplit(str, delimiter)- Split string by charactersplitWhitespace(str)- Split by any whitespacetrim(str)- Remove leading/trailing whitespacetoInt(str),toLong(str),toLongLong(str)- String to number conversion
Advent of Code - C++ Solutions
================================
Running all available solutions for year 2025
+=======+======+======+==================+===============+
| Year | Day | Part | Answer | Time (ms) |
+=======+======+======+==================+===============+
| 2025 | 1 | 1 | 54321 | 0.145 |
| 2025 | 1 | 2 | 98765 | 0.203 |
| 2025 | 2 | 1 | 1337 | 1.256 |
| 2025 | 2 | 2 | 9001 | 2.103 |
+=======+======+======+==================+===============+
| Total time: 3.707 ms |
+=======+======+======+==================+===============+