Compare commits
5 commits
2c69ef4b78
...
ba09492857
Author | SHA1 | Date | |
---|---|---|---|
ba09492857 | |||
26f694c449 | |||
9c22642be4 | |||
46582ed853 | |||
39c28b5897 |
123 changed files with 1394 additions and 0 deletions
16
2020/zig/.gitattributes
vendored
Normal file
16
2020/zig/.gitattributes
vendored
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
# See https://git-scm.com/docs/gitattributes
|
||||||
|
# See https://help.github.com/articles/dealing-with-line-endings/
|
||||||
|
|
||||||
|
# Default behavior, if core.autocrlf is unset.
|
||||||
|
* text=auto
|
||||||
|
|
||||||
|
# Files to be converted to native line endings on checkout.
|
||||||
|
*.cpp text
|
||||||
|
*.h text
|
||||||
|
|
||||||
|
# Text files to always have CRLF (dos) line endings on checkout.
|
||||||
|
*.bat text eol=crlf
|
||||||
|
|
||||||
|
# Text files to always have LF (unix) line endings on checkout.
|
||||||
|
*.sh text eol=lf
|
||||||
|
*.zig text eol=lf
|
4
2020/zig/.gitignore
vendored
Normal file
4
2020/zig/.gitignore
vendored
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
zig-cache/
|
||||||
|
zig-out/
|
||||||
|
data/
|
||||||
|
build_runner.zig
|
20
2020/zig/LICENSE
Normal file
20
2020/zig/LICENSE
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
Copyright (c) 2020-2021 Martin Wickham
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
||||||
|
|
32
2020/zig/README.md
Normal file
32
2020/zig/README.md
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
# 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.
|
||||||
|
|
||||||
|
This template tracks the master branch of Zig, *not* 0.8.1. It may not work with older versions.
|
||||||
|
|
||||||
|
## How to use this template:
|
||||||
|
|
||||||
|
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`.
|
||||||
|
|
||||||
|
Each day contains a decl like this:
|
||||||
|
```zig
|
||||||
|
const data = @embedFile("../data/day05.txt");
|
||||||
|
```
|
||||||
|
To use this system, save your input for a day in the 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 it's unused, it will not try to load the file, and it won't error if the file does not exist.
|
||||||
|
|
||||||
|
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.
|
||||||
|
|
||||||
|
## Setting up ZLS
|
||||||
|
|
||||||
|
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. To set it up, make sure you have an up-to-date master build of Zig (which you can [download here](https://ziglang.org/download/)), and then run the following commands:
|
||||||
|
|
||||||
|
```
|
||||||
|
git clone --recurse-submodules https://github.com/zigtools/zls
|
||||||
|
cd zls
|
||||||
|
zig build -Drelease-fast
|
||||||
|
zig-out/bin/zls configure
|
||||||
|
```
|
||||||
|
|
||||||
|
The last command will direct you to documentation for connecting it to your preferred editor. If you are using VSCode, the documentation [can be found here](https://github.com/zigtools/zls/wiki/Installing-for-Visual-Studio-Code).
|
146
2020/zig/build.zig
Normal file
146
2020/zig/build.zig
Normal file
|
@ -0,0 +1,146 @@
|
||||||
|
const std = @import("std");
|
||||||
|
const Builder = std.build.Builder;
|
||||||
|
const LibExeObjStep = std.build.LibExeObjStep;
|
||||||
|
|
||||||
|
// set this to true to link libc
|
||||||
|
const should_link_libc = false;
|
||||||
|
|
||||||
|
fn linkObject(b: *Builder, obj: *LibExeObjStep) void {
|
||||||
|
if (should_link_libc) obj.linkLibC();
|
||||||
|
_ = b;
|
||||||
|
|
||||||
|
// Add linking for packages or third party libraries here
|
||||||
|
}
|
||||||
|
|
||||||
|
const required_zig_version = std.SemanticVersion.parse("0.9.0-dev.1920+de81c504b") catch unreachable;
|
||||||
|
|
||||||
|
pub fn build(b: *Builder) void {
|
||||||
|
if (comptime @import("builtin").zig_version.order(required_zig_version) == .lt) {
|
||||||
|
std.debug.print(
|
||||||
|
\\Error: Your version of Zig is missing features that are needed for this template.
|
||||||
|
\\You will need to download a newer build.
|
||||||
|
\\
|
||||||
|
\\ https://ziglang.org/download/
|
||||||
|
\\
|
||||||
|
\\
|
||||||
|
, .{});
|
||||||
|
std.os.exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Standard target options allows the person running `zig build` to choose
|
||||||
|
// what target to build for. Here we do not override the defaults, which
|
||||||
|
// means any target is allowed, and the default is native. Other options
|
||||||
|
// for restricting supported target set are available.
|
||||||
|
const target = b.standardTargetOptions(.{});
|
||||||
|
|
||||||
|
// Standard release options allow the person running `zig build` to select
|
||||||
|
// between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall.
|
||||||
|
const mode = b.standardReleaseOptions();
|
||||||
|
|
||||||
|
const install_all = b.step("install_all", "Install all days");
|
||||||
|
const install_all_tests = b.step("install_tests_all", "Install tests for all days");
|
||||||
|
const run_all = b.step("run_all", "Run all days");
|
||||||
|
|
||||||
|
// Set up an exe for each day
|
||||||
|
var day: u32 = 1;
|
||||||
|
while (day <= 25) : (day += 1) {
|
||||||
|
const dayString = b.fmt("day{:0>2}", .{day});
|
||||||
|
const zigFile = b.fmt("src/{s}.zig", .{dayString});
|
||||||
|
|
||||||
|
const exe = b.addExecutable(dayString, zigFile);
|
||||||
|
exe.setTarget(target);
|
||||||
|
exe.setBuildMode(mode);
|
||||||
|
linkObject(b, exe);
|
||||||
|
|
||||||
|
exe.install();
|
||||||
|
|
||||||
|
const install_cmd = b.addInstallArtifact(exe);
|
||||||
|
|
||||||
|
const run_test = b.addTest(zigFile);
|
||||||
|
run_test.setTarget(target);
|
||||||
|
run_test.setBuildMode(mode);
|
||||||
|
linkObject(b, exe);
|
||||||
|
|
||||||
|
const build_test = b.addTestExe(b.fmt("test_{s}", .{dayString}), zigFile);
|
||||||
|
build_test.setTarget(target);
|
||||||
|
build_test.setBuildMode(mode);
|
||||||
|
linkObject(b, exe);
|
||||||
|
const install_test = b.addInstallArtifact(build_test);
|
||||||
|
|
||||||
|
{
|
||||||
|
const step_key = b.fmt("install_{s}", .{dayString});
|
||||||
|
const step_desc = b.fmt("Install {s}.exe", .{dayString});
|
||||||
|
const install_step = b.step(step_key, step_desc);
|
||||||
|
install_step.dependOn(&install_cmd.step);
|
||||||
|
install_all.dependOn(&install_cmd.step);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
const step_key = b.fmt("test_{s}", .{dayString});
|
||||||
|
const step_desc = b.fmt("Run tests in {s}", .{zigFile});
|
||||||
|
const step = b.step(step_key, step_desc);
|
||||||
|
step.dependOn(&run_test.step);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
const step_key = b.fmt("install_tests_{s}", .{dayString});
|
||||||
|
const step_desc = b.fmt("Install test_{s}.exe", .{dayString});
|
||||||
|
const step = b.step(step_key, step_desc);
|
||||||
|
step.dependOn(&install_test.step);
|
||||||
|
install_all_tests.dependOn(&install_test.step);
|
||||||
|
}
|
||||||
|
|
||||||
|
const run_cmd = exe.run();
|
||||||
|
run_cmd.step.dependOn(&install_cmd.step);
|
||||||
|
if (b.args) |args| {
|
||||||
|
run_cmd.addArgs(args);
|
||||||
|
}
|
||||||
|
|
||||||
|
const run_desc = b.fmt("Run {s}", .{dayString});
|
||||||
|
const run_step = b.step(dayString, run_desc);
|
||||||
|
run_step.dependOn(&run_cmd.step);
|
||||||
|
run_all.dependOn(&run_cmd.step);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set up tests for util.zig
|
||||||
|
{
|
||||||
|
const test_util = b.step("test_util", "Run tests in util.zig");
|
||||||
|
const test_cmd = b.addTest("src/util.zig");
|
||||||
|
test_cmd.setTarget(target);
|
||||||
|
test_cmd.setBuildMode(mode);
|
||||||
|
linkObject(b, test_cmd);
|
||||||
|
test_util.dependOn(&test_cmd.step);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set up test executable for util.zig
|
||||||
|
{
|
||||||
|
const test_util = b.step("install_tests_util", "Run tests in util.zig");
|
||||||
|
const test_exe = b.addTestExe("test_util", "src/util.zig");
|
||||||
|
test_exe.setTarget(target);
|
||||||
|
test_exe.setBuildMode(mode);
|
||||||
|
linkObject(b, test_exe);
|
||||||
|
const install = b.addInstallArtifact(test_exe);
|
||||||
|
test_util.dependOn(&install.step);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set up a step to run all tests
|
||||||
|
{
|
||||||
|
const test_step = b.step("test", "Run all tests");
|
||||||
|
const test_cmd = b.addTest("src/test_all.zig");
|
||||||
|
test_cmd.setTarget(target);
|
||||||
|
test_cmd.setBuildMode(mode);
|
||||||
|
linkObject(b, test_cmd);
|
||||||
|
test_step.dependOn(&test_cmd.step);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set up a step to build tests (but not run them)
|
||||||
|
{
|
||||||
|
const test_build = b.step("install_tests", "Install test_all.exe");
|
||||||
|
const test_exe = b.addTestExe("test_all", "src/test_all.zig");
|
||||||
|
test_exe.setTarget(target);
|
||||||
|
test_exe.setBuildMode(mode);
|
||||||
|
linkObject(b, test_exe);
|
||||||
|
const test_exe_install = b.addInstallArtifact(test_exe);
|
||||||
|
test_build.dependOn(&test_exe_install.step);
|
||||||
|
}
|
||||||
|
}
|
50
2020/zig/src/day01.zig
Normal file
50
2020/zig/src/day01.zig
Normal file
|
@ -0,0 +1,50 @@
|
||||||
|
const std = @import("std");
|
||||||
|
const tokenize = std.mem.tokenize;
|
||||||
|
const parseInt = std.fmt.parseInt;
|
||||||
|
const print = std.debug.print;
|
||||||
|
|
||||||
|
const data = @embedFile("../data/day01.txt");
|
||||||
|
|
||||||
|
pub fn main() !void {
|
||||||
|
var it = tokenize(u8, data, "\n");
|
||||||
|
|
||||||
|
// Not sure if allocating a massive buffer and tracking the size is the right approach?
|
||||||
|
var buffer: [1000]u16 = undefined;
|
||||||
|
var i: usize = 0;
|
||||||
|
while (it.next()) |line| {
|
||||||
|
const int = try parseInt(u16, line, 10);
|
||||||
|
buffer[i] = int;
|
||||||
|
// Can we use continue expression?
|
||||||
|
i = i + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
try part1(buffer[0..i]);
|
||||||
|
try part2(buffer[0..i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
fn part1(input: []u16) !void {
|
||||||
|
for (input) |a, i| {
|
||||||
|
for (input[(i+1)..]) |b| {
|
||||||
|
if (a + b == 2020) {
|
||||||
|
const answer = @as(u32, a) * b;
|
||||||
|
print("Part1: {d}\n", .{answer});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn part2(input: []u16) !void {
|
||||||
|
for (input) |a, i| {
|
||||||
|
for (input[(i+1)..]) |b, j| {
|
||||||
|
for (input[(j+1)..]) |c| {
|
||||||
|
if (a + b + c == 2020) {
|
||||||
|
const answer = @as(u32, a) * b * c;
|
||||||
|
print("Part2: {d}\n", .{answer});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
44
2020/zig/src/day02.zig
Normal file
44
2020/zig/src/day02.zig
Normal file
|
@ -0,0 +1,44 @@
|
||||||
|
const std = @import("std");
|
||||||
|
const Allocator = std.mem.Allocator;
|
||||||
|
const List = std.ArrayList;
|
||||||
|
const Map = std.AutoHashMap;
|
||||||
|
const StrMap = std.StringHashMap;
|
||||||
|
const BitSet = std.DynamicBitSet;
|
||||||
|
const Str = []const u8;
|
||||||
|
|
||||||
|
const util = @import("util.zig");
|
||||||
|
const gpa = util.gpa;
|
||||||
|
|
||||||
|
const data = @embedFile("../data/day02.txt");
|
||||||
|
|
||||||
|
pub fn main() !void {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// Useful stdlib functions
|
||||||
|
const tokenize = std.mem.tokenize;
|
||||||
|
const split = std.mem.split;
|
||||||
|
const indexOf = std.mem.indexOfScalar;
|
||||||
|
const indexOfAny = std.mem.indexOfAny;
|
||||||
|
const indexOfStr = std.mem.indexOfPosLinear;
|
||||||
|
const lastIndexOf = std.mem.lastIndexOfScalar;
|
||||||
|
const lastIndexOfAny = std.mem.lastIndexOfAny;
|
||||||
|
const lastIndexOfStr = std.mem.lastIndexOfLinear;
|
||||||
|
const trim = std.mem.trim;
|
||||||
|
const sliceMin = std.mem.min;
|
||||||
|
const sliceMax = std.mem.max;
|
||||||
|
|
||||||
|
const parseInt = std.fmt.parseInt;
|
||||||
|
const parseFloat = std.fmt.parseFloat;
|
||||||
|
|
||||||
|
const min = std.math.min;
|
||||||
|
const min3 = std.math.min3;
|
||||||
|
const max = std.math.max;
|
||||||
|
const max3 = std.math.max3;
|
||||||
|
|
||||||
|
const print = std.debug.print;
|
||||||
|
const assert = std.debug.assert;
|
||||||
|
|
||||||
|
const sort = std.sort.sort;
|
||||||
|
const asc = std.sort.asc;
|
||||||
|
const desc = std.sort.desc;
|
44
2020/zig/src/day03.zig
Normal file
44
2020/zig/src/day03.zig
Normal file
|
@ -0,0 +1,44 @@
|
||||||
|
const std = @import("std");
|
||||||
|
const Allocator = std.mem.Allocator;
|
||||||
|
const List = std.ArrayList;
|
||||||
|
const Map = std.AutoHashMap;
|
||||||
|
const StrMap = std.StringHashMap;
|
||||||
|
const BitSet = std.DynamicBitSet;
|
||||||
|
const Str = []const u8;
|
||||||
|
|
||||||
|
const util = @import("util.zig");
|
||||||
|
const gpa = util.gpa;
|
||||||
|
|
||||||
|
const data = @embedFile("../data/day03.txt");
|
||||||
|
|
||||||
|
pub fn main() !void {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// Useful stdlib functions
|
||||||
|
const tokenize = std.mem.tokenize;
|
||||||
|
const split = std.mem.split;
|
||||||
|
const indexOf = std.mem.indexOfScalar;
|
||||||
|
const indexOfAny = std.mem.indexOfAny;
|
||||||
|
const indexOfStr = std.mem.indexOfPosLinear;
|
||||||
|
const lastIndexOf = std.mem.lastIndexOfScalar;
|
||||||
|
const lastIndexOfAny = std.mem.lastIndexOfAny;
|
||||||
|
const lastIndexOfStr = std.mem.lastIndexOfLinear;
|
||||||
|
const trim = std.mem.trim;
|
||||||
|
const sliceMin = std.mem.min;
|
||||||
|
const sliceMax = std.mem.max;
|
||||||
|
|
||||||
|
const parseInt = std.fmt.parseInt;
|
||||||
|
const parseFloat = std.fmt.parseFloat;
|
||||||
|
|
||||||
|
const min = std.math.min;
|
||||||
|
const min3 = std.math.min3;
|
||||||
|
const max = std.math.max;
|
||||||
|
const max3 = std.math.max3;
|
||||||
|
|
||||||
|
const print = std.debug.print;
|
||||||
|
const assert = std.debug.assert;
|
||||||
|
|
||||||
|
const sort = std.sort.sort;
|
||||||
|
const asc = std.sort.asc;
|
||||||
|
const desc = std.sort.desc;
|
44
2020/zig/src/day04.zig
Normal file
44
2020/zig/src/day04.zig
Normal file
|
@ -0,0 +1,44 @@
|
||||||
|
const std = @import("std");
|
||||||
|
const Allocator = std.mem.Allocator;
|
||||||
|
const List = std.ArrayList;
|
||||||
|
const Map = std.AutoHashMap;
|
||||||
|
const StrMap = std.StringHashMap;
|
||||||
|
const BitSet = std.DynamicBitSet;
|
||||||
|
const Str = []const u8;
|
||||||
|
|
||||||
|
const util = @import("util.zig");
|
||||||
|
const gpa = util.gpa;
|
||||||
|
|
||||||
|
const data = @embedFile("../data/day04.txt");
|
||||||
|
|
||||||
|
pub fn main() !void {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// Useful stdlib functions
|
||||||
|
const tokenize = std.mem.tokenize;
|
||||||
|
const split = std.mem.split;
|
||||||
|
const indexOf = std.mem.indexOfScalar;
|
||||||
|
const indexOfAny = std.mem.indexOfAny;
|
||||||
|
const indexOfStr = std.mem.indexOfPosLinear;
|
||||||
|
const lastIndexOf = std.mem.lastIndexOfScalar;
|
||||||
|
const lastIndexOfAny = std.mem.lastIndexOfAny;
|
||||||
|
const lastIndexOfStr = std.mem.lastIndexOfLinear;
|
||||||
|
const trim = std.mem.trim;
|
||||||
|
const sliceMin = std.mem.min;
|
||||||
|
const sliceMax = std.mem.max;
|
||||||
|
|
||||||
|
const parseInt = std.fmt.parseInt;
|
||||||
|
const parseFloat = std.fmt.parseFloat;
|
||||||
|
|
||||||
|
const min = std.math.min;
|
||||||
|
const min3 = std.math.min3;
|
||||||
|
const max = std.math.max;
|
||||||
|
const max3 = std.math.max3;
|
||||||
|
|
||||||
|
const print = std.debug.print;
|
||||||
|
const assert = std.debug.assert;
|
||||||
|
|
||||||
|
const sort = std.sort.sort;
|
||||||
|
const asc = std.sort.asc;
|
||||||
|
const desc = std.sort.desc;
|
Some files were not shown because too many files have changed in this diff Show more
Loading…
Reference in a new issue