Gave up on zig 2020, restore just the elixir version

This commit is contained in:
Adam Millerchip 2022-11-27 15:57:52 +09:00
parent 1f7c32b758
commit 4955687b53
124 changed files with 1 additions and 1440 deletions

View File

@ -1,16 +0,0 @@
# 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

5
2020/zig/.gitignore vendored
View File

@ -1,5 +0,0 @@
zig-cache/
zig-out/
src/sample
src/input
build_runner.zig

View File

@ -1,20 +0,0 @@
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.

View File

@ -1,32 +0,0 @@
# 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).

View File

@ -1,146 +0,0 @@
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);
}
}

View File

@ -1,48 +0,0 @@
const std = @import("std");
const tokenize = std.mem.tokenize;
const parseInt = std.fmt.parseInt;
const print = std.debug.print;
const data = @embedFile("input");
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;
}
print("Part 1: {d}\n", .{part1(buffer[0..i])});
print("Part 2: {d}\n", .{part2(buffer[0..i])});
}
fn part1(input: []u16) u32 {
for (input) |a, i| {
for (input[(i+1)..]) |b| {
if (a + b == 2020) {
return @as(u32, a) * b;
}
}
}
return 0;
}
fn part2(input: []u16) u32 {
for (input) |a, i| {
for (input[(i+1)..]) |b, j| {
for (input[(j+1)..]) |c| {
if (a + b + c == 2020) {
return @as(u32, a) * b * c;
}
}
}
}
return 0;
}

View File

@ -1,62 +0,0 @@
const std = @import("std");
const gpa = @import("util.zig").gpa;
const Allocator = std.mem.Allocator;
const ArrayList = std.ArrayList;
const tokenize = std.mem.tokenize;
const parseInt = std.fmt.parseInt;
const print = std.debug.print;
const data = @embedFile("input");
const Rule = struct {
a: u8, b: u8, char: u8, password: []const u8
};
pub fn main() !void {
const rules = try parse();
defer Allocator.free(gpa, rules);
print("Part 1: {d}\n", .{part1(rules)});
print("Part 2: {d}\n", .{part2(rules)});
}
fn parse() ![]Rule {
var it = tokenize(u8, data, "\n");
var rules = ArrayList(Rule).init(gpa);
while (it.next()) |line| {
var line_it = tokenize(u8, line, ": -");
const a = try parseInt(u8, line_it.next().?, 10);
const b = try parseInt(u8, line_it.next().?, 10);
const char = line_it.next().?[0];
const password = line_it.next().?;
try rules.append(Rule{.a = a, .b = b, .char = char, .password = password});
}
return rules.toOwnedSlice();
}
fn part1(rules: []Rule) u16 {
var valid_pw_count: u16 = 0;
for (rules) |rule| {
var count: u8 = 0;
for (rule.password) |password_char| {
if (rule.char == password_char)
count += 1;
}
if (rule.a <= count and count <= rule.b)
valid_pw_count += 1;
}
return valid_pw_count;
}
fn part2(rules: []Rule) u16 {
var valid_pw_count: u16 = 0;
for (rules) |rule| {
const a_match = rule.password[rule.a-1] == rule.char;
const b_match = rule.password[rule.b-1] == rule.char;
// No xor in zig?
if (a_match and !b_match or !a_match and b_match)
valid_pw_count += 1;
}
return valid_pw_count;
}

View File

@ -1,46 +0,0 @@
const std = @import("std");
const util = @import("util.zig");
const Allocator = std.mem.Allocator;
const ArrayList = std.ArrayList;
const tokenize = std.mem.tokenize;
const print = std.debug.print;
const gpa = util.gpa;
const data = @embedFile("input");
pub fn main() !void {
// Still have no idea how to clean up the ArrayLists
var it = tokenize(u8, data, "\n");
var rows_al = ArrayList([]bool).init(gpa);
while (it.next()) |line| {
var row = ArrayList(bool).init(gpa);
for (line) |char| {
try row.append(char == '#');
}
try rows_al.append(row.toOwnedSlice());
}
var rows = rows_al.toOwnedSlice();
var part_1 = count_trees(rows, 3, 1);
print("Part 1: {}\n", .{part_1});
var part_2 = count_trees(rows, 1, 1) * part_1 * count_trees(rows, 5, 1) * count_trees(rows, 7, 1) * count_trees(rows, 1, 2);
print("Part 2: {}\n", .{part_2});
}
fn count_trees(slope: [][]bool, delta_x: usize, delta_y: usize) usize {
var x: usize = 0;
var y: usize = 0;
var count: usize = 0;
while (y < slope.len) {
var square = slope[y][x];
if (square) {
count += 1;
}
x = (x + delta_x) % slope[y].len;
y += delta_y;
}
return count;
}

View File

@ -1,69 +0,0 @@
const std = @import("std");
const Allocator = std.mem.Allocator;
const ArrayList = std.ArrayList;
const Map = std.AutoHashMap;
const StringHashMap = std.StringHashMap;
const BitSet = std.DynamicBitSet;
const Str = []const u8;
const util = @import("util.zig");
const gpa = util.gpa;
const input = @embedFile("input");
pub fn main() !void {
var line_it = split(u8, input, "\n");
var passports = ArrayList(StringHashMap([]const u8)).init(gpa);
var fields = StringHashMap([]const u8).init(gpa);
while (line_it.next()) |line| {
if (line.len == 0) {
try passports.append(try fields.clone());
fields.clearAndFree();
} else {
var token_it = tokenize(u8, line, " :");
while (token_it.next()) |token| {
try fields.put(token, token_it.next().?);
}
}
}
try passports.append(fields);
const required_fields = [_][]const u8{ "byr", "iyr", "eyr", "hgt", "hcl", "ecl", "pid" };
var valid_count: usize = 0;
for (passports.items) |passport| {
var valid: bool = true;
for (required_fields) |required_field| {
valid = valid and passport.contains(required_field);
}
if (valid) valid_count += 1;
}
print("{}\n", .{valid_count});
}
// 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