advent-of-code-2023/README.md

28 lines
3.5 KiB
Markdown
Raw Permalink Normal View History

2020-11-26 22:46:03 +01:00
# Advent Of Code Zig Template
This repo provides a template for Advent of Code participants using Zig. It contains a main file for each day, a build.zig file set up with targets for each day, and Visual Studio Code files for debugging.
2023-11-29 08:17:54 +01:00
This template has been tested with Zig `0.12.0-dev.1754+2a3226453`. It may not work with other versions.
2021-12-01 01:09:36 +01:00
## How to use this template:
2020-11-26 22:46:03 +01:00
2021-12-07 19:56:26 +01:00
The src/ directory contains a main file for each day. Put your code there. The build command `zig build dayXX [target and mode options] -- [program args]` will build and run the specified day. You can also use `zig build install_dayXX [target and mode options]` to build the executable for a day and put it into `zig-out/bin` without executing it. By default this template does not link libc, but you can set `should_link_libc` to `true` in build.zig to change that. If you add new files with tests, add those files to the list of test files in test_all.zig. The command `zig build test` will run tests in all of these files. You can also use `zig build test_dayXX` to run tests in a specific day, or `zig build install_tests_dayXX` to create a debuggable test executable in `zig-out/bin`.
2020-11-26 22:46:03 +01:00
2021-11-24 06:28:27 +01:00
Each day contains a decl like this:
```zig
const data = @embedFile("data/day05.txt");
2021-11-24 06:28:27 +01:00
```
To use this system, save your input for a day in the src/data/ directory with the appropriate name. Reference this decl to load the contents of that file as a compile time constant. If a day has no input, or you prefer not to embed it in this form, simply don't reference this decl. If `data` is unused, the compiler will not try to load the file, and it won't error if the file does not exist.
2021-11-24 06:28:27 +01:00
2020-11-26 22:46:03 +01:00
This repo also contains Visual Studio Code project files for debugging. These are meant to work with the C/C++ plugin. There is a debug configuration for each day. By default all days are built in debug mode, but this can be changed by editing `.vscode/tasks.json` if you have a need for speed.
If you would like to contribute project files for other development environments, please send a PR.
2021-12-01 01:09:36 +01:00
2022-11-26 21:35:05 +01:00
## Modifying the template
2022-11-26 22:03:13 +01:00
You can modify the template to add your own changes across all days. To do so, modify template/template.zig and then run `zig build generate`. The `$` character in the template will be replaced by the two-digit day number (e.g. 04 or 17). This step will only overwrite files which have not been modified, so you will not lose work if you update the template after implementing several days. After updating the template and generating, you should commit the changes to template/hashes.bin in addition to the updated template and source files. This will ensure that the newly generated files are not considered modified if you update the template again.
2022-11-26 21:35:05 +01:00
2021-12-01 01:09:36 +01:00
## Setting up ZLS
2022-11-24 18:28:02 +01:00
Zig has a reasonably robust language server, which can provide autocomplete for VSCode and many other editors. It can help significantly with exploring the std lib and suggesting parameter completions. The VSCode extension (augusterame.zls-vscode) will automatically install the language server in the background. If you are using a different editor, follow their [install instructions](https://zigtools.github.io/install-zls/). If you want to install a specific version of the language server (for example for maximum compatibility with 0.10.0), [check their releases page](https://github.com/zigtools/zls/releases) or [follow their instructions to build from source](https://github.com/zigtools/zls#from-source). Note that ZLS tracks master, so if you are using Zig 0.10.0 you may need to download a newer version to build ZLS.