Advent of Code 2025 Day 3— D
Marketing Pitch and Impressions#
D is a general-purpose programming language with static typing, systems-level access, and C-like syntax. With the D Programming Language, write fast, read fast, and run fast.
Unfortunately most of the systems programming languages I’ve used in AoC 2025 appear to have lost the systems-programming race to Rust and maybe Go. That’s something of a shame, as D seems like a worthy modern alternative to C and C++.
One feature that I found appealing, and largely the reason I looked at D in the first place, is the distinction D makes between const (a read-only view of data that might be changed by someone else) and immutable (data that can never change). Unfortunately this distinction doesn’t really come into play in this particular solution.
Another thing that is quite nice is the ability to mark functions as pure and
@safe. This gives the compiler more information to optimize and check code
for safety and purity.
Visual Studio Code support for D is excellent.
The Code#
The standout feature demonstrated here is Compile Time Function Execution
(CTFE). Because D’s compiler front-end is so powerful, I was able to compute
the entire solution during compilation. The runtime executable simply prints
the pre-calculated constants. I verified this by inspecting the generated
assembly— the main() function just calls writeln(...) twice with constants.
The underlying algorithm is again pretty straightfoward. Just take the biggest digit from the current string that leaves enough digits to fill in the remainder of the n-digit number. The exact same algorithm is used for part 1 and part 2, just with differing lengths.
| |
Install D and run#
D has three major compilers: DMD (reference), LDC (LLVM-based), and GDC
(GCC-based). DMD does not, as yet, have an aarch64 backend, so I used LDC on
my Mac.
# I installed via Homebrew
brew install ldc
# From the website: install DMD
curl -fsS https://dlang.org/install.sh | bash -s dmd
# Compile and Run
ldc2 -J. -run d03.d