This repository has been archived on 2022-12-11. You can view files and clone it, but cannot push or open issues or pull requests.
aoc2022/05/1-2.zig

114 lines
3.2 KiB
Zig

const std = @import("std");
const moveFnType = *const fn ([]Vector(u8), usize, usize, usize) void;
pub fn range(len: usize) []const void {
return @as([*]void, undefined)[0..len];
}
const lineIter = std.mem.SplitIterator(u8);
const Vector = std.ArrayList;
// I don't have much time for this, it ain't gonna
// be as pretty as the other ones
pub fn main() void {
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
defer arena.deinit();
const allocator = arena.allocator();
const input = @embedFile("input");
std.debug.print("Part 1: {s}\n", .{crane900X(input, moveAppend, allocator)});
std.debug.print("Part 2: {s}\n", .{crane900X(input, moveAppendSlice, allocator)});
}
fn crane900X(input: []const u8, moveFn: moveFnType, allocator: std.mem.Allocator) []const u8 {
var iter = std.mem.split(u8, input, "\n");
// Parses the header with the states and advances the counter
const containers = parseContainers(&iter, allocator);
while (iter.next()) |line| {
// Not valid
if (line.len <= 0) continue;
const nums = parseLine(line);
moveFn(containers, nums[0], nums[1] - 1, nums[2] - 1);
}
var res = allocator.alloc(u8, containers.len) catch @panic("Allocation error");
for (containers) |cont, i| {
res[i] = cont.items[cont.items.len - 1];
}
return res;
}
fn isNumber(char: u8) bool {
return char >= '0' and char <= '9';
}
fn isLetter(char: u8) bool {
return char >= 'A' and char <= 'Z';
}
fn moveAppend(list: []Vector(u8), amount: usize, from: usize, to: usize) void {
var i: usize = 0;
while (i < amount) : (i += 1) {
list[to].append(list[from].pop()) catch @panic("Allocation error");
}
}
fn moveAppendSlice(list: []Vector(u8), amount: usize, from: usize, to: usize) void {
var fromLen = list[from].items.len;
list[to].appendSlice(list[from].items[(fromLen - amount)..]) catch @panic("Allocation error");
var i: usize = 0;
while (i < amount) : (i += 1) {
_ = list[from].pop();
}
}
fn parseLine(line: []const u8) [3]u8 {
var nums = [3]u8{ 0, 0, 0 };
var iter = std.mem.split(u8, line, " ");
var i: usize = 0;
while (iter.next()) |word| {
nums[i] = (std.fmt.parseInt(u8, word, 10) catch continue);
i += 1;
}
return nums;
}
fn parseContainers(iter: *std.mem.SplitIterator(u8), allocator: std.mem.Allocator) []Vector(u8) {
const len = (iter.first().len + 1) / 4;
iter.reset();
var containers = Vector(Vector(u8)).init(allocator);
var contIdx: usize = 0;
while (contIdx < len) : (contIdx += 1) {
containers.append(Vector(u8).init(allocator)) catch @panic("Allocation error");
}
while (iter.next()) |line| {
if (line.len <= 0) continue;
// We reached the end
if (isNumber(line[1])) {
// Consume the next line
_ = iter.next();
break;
}
var i: usize = 0;
while (i < len) : (i += 1) {
const char = line[i * 4 + 1];
if (!isLetter(char)) continue;
containers.items[i].insert(0, char) catch @panic("Allocation error");
}
}
return containers.items;
}