Skip to content

Tomcat-42/aoc.zig-template

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

aoc.zig-template

This is a zig template for Advent of Code. It has a very opinionated structure and automatic input fetching/source code template generation for a current year/day. All of this is powered by the zig build system.

Usage

Clone this template repo:

gh repo create aoc.zig --template Tomcat-42/aoc.zig-template --public --clone

Then Login to your Advent of Code account to get your session token. You can do that by opening the browser devtools, selecting the networks tab, reloading the page, selecting any request and looking for the Cookie: session=<TOKEN> header:

image

Then, make it available as an env var:

AOC_SESSION_TOKEN="<TOKEN>"

You can pass the command line flags -Dyear=<year> and -Dday=<day> to specify the year and day you want to generate the template for (if you don't pass them, it will default to the current year and day of the month).

zig build --build-runner build_runner.zig -Dyear=2023 -Dday=1 --watch run

You can pass test instead of run to run the unit tests instead:

zig build --build-runner build_runner.zig -Dyear=2023 -Dday=1 --watch test

Details

When you run the build command for a year and day for the first time, it generates the source code template (./src/<year>/<day>.zig) and input (./input/<year>/<day>.txt) for that year/day.

The ./src/<year>/<day>.zig file will have the following format:

const std = @import("std");
const mem = std.mem;

input: []const u8,
allocator: mem.Allocator,

pub fn part1(this: *const @This()) !?i64 {
    _ = this;
    return null;
}

pub fn part2(this: *const @This()) !?i64 {
    _ = this;
    return null;
}

test "it should do nothing" {
    const allocator = std.testing.allocator;
    const input = "";

    const problem: @This() = .{
        .input = input,
        .allocator = allocator,
    };

    try std.testing.expectEqual(null, try problem.part1());
    try std.testing.expectEqual(null, try problem.part2());
}

You "solve" the problem by returning the solution from part1 and part2 function. The input field is the input data for the problem (see build.zig and src/main.zig for details on how this is achieved). Note that you can return anything as the solution, for instance, if the solution is a string, you can return a []const u8 from any part.

Add tests for small examples and edge cases in tests blocks at the end of the file.

About

Advent of Code Zig Template

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 3

  •  
  •  
  •  

Languages