Compare commits

..

No commits in common. "1e3f01aef549887bbd959e0d10600d67a74d2d14" and "ebaffe961f02a78edba53de9880aa27e5610d4b7" have entirely different histories.

6 changed files with 2 additions and 1100 deletions

2
.gitignore vendored
View file

@ -1,2 +0,0 @@
.zig-cache/
zig-out/

View file

@ -1,33 +0,0 @@
const std = @import("std");
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const install_all = b.step("install-all", "Install all days");
const run_all = b.step("run-all", "Run all days");
const num_days = b.option(u32, "num-days", "The number of days to process") orelse 25;
var day: u8 = 1;
while (day <= num_days) : (day += 1) {
const str = b.fmt("day{d}", .{day});
const file = b.fmt("{s}.zig", .{str});
const exe = b.addExecutable(.{
.name = str,
.root_source_file = b.path(file),
.target = target,
.optimize = optimize,
});
const install_exe = b.addInstallArtifact(exe, .{});
const install_step = b.step(b.fmt("install_{s}", .{str}), b.fmt("Install {s}", .{str}));
install_step.dependOn(&install_exe.step);
install_all.dependOn(&install_exe.step);
const run_exe = b.addRunArtifact(exe);
const run_step = b.step(b.fmt("run_{s}", .{str}), b.fmt("Run {s}", .{str}));
run_step.dependOn(&run_exe.step);
run_all.dependOn(&run_exe.step);
}
}

File diff suppressed because it is too large Load diff

View file

@ -1,49 +0,0 @@
const std = @import("std");
const print = std.debug.print;
pub fn main() !void {
const file = try std.fs.cwd().openFile("day1.txt", .{});
defer file.close();
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer std.debug.assert(gpa.deinit() == .ok);
const allocator = gpa.allocator();
const stat = try file.stat();
const input = try file.reader().readAllAlloc(allocator, stat.size);
defer allocator.free(input);
var l1 = std.ArrayList(i32).init(allocator);
var l2 = std.ArrayList(i32).init(allocator);
defer l1.deinit();
defer l2.deinit();
var lines = std.mem.tokenizeScalar(u8, input, '\n');
while (lines.next()) |line| {
var split = std.mem.tokenizeScalar(u8, line, ' ');
try l1.append(try std.fmt.parseInt(i32, split.next().?, 10));
try l2.append(try std.fmt.parseInt(i32, split.next().?, 10));
}
// PART 1
std.mem.sort(i32, l1.items, {}, std.sort.asc(i32));
std.mem.sort(i32, l2.items, {}, std.sort.asc(i32));
var distance: u32 = 0;
for (l1.items, l2.items) |item1, item2| {
distance += @abs(item1 - item2);
}
// PART 2
var simScore: i32 = 0;
for (l1.items) |item1| {
var count: i32 = 0;
for (l2.items) |item2| {
if (item1 == item2) count += 1;
}
simScore += item1 * count;
}
print("PART 1 - DISTANCE: {d}\n", .{distance});
print("PART 2 - SIMILARITY: {d}\n", .{simScore});
}

View file

@ -1,11 +0,0 @@
const std = @import("std");
const print = std.debug.print;
pub fn readAllInput(day: u8, allocator: std.mem.Allocator) ![]const u8 {
var fileName: [16]u8 = undefined;
const inputFile = try std.fmt.bufPrint(&fileName, "day{d}.txt", .{day});
const file = try std.fs.cwd().openFile(inputFile, .{});
defer file.close();
const stat = try file.stat();
return try file.reader().readAllAlloc(allocator, stat.size);
}

View file

@ -1,6 +1,3 @@
# Advent of Code
# advent-of-code
> Advent of Code is an Advent calendar of small programming puzzles for a variety of skill levels that can be solved in any programming language you like. People use them as interview prep, company training, university coursework, practice problems, a speed contest, or to challenge each other.
> -- [Eric Wastl](https://was.tl/) (Advent of Code creator)
My (casual) attempts at [Advent of Code](https://adventofcode.com). The solutions are released under the [*unlicense*](LICENSE). As such, they are public domain and you may ~~steal~~ use them in pretty much any manner you see fit.
Casual attempts at Advent of Code