Compare commits

...

2 Commits

Author SHA1 Message Date
Dendy 1ca55ed289 Day 7 2022-12-08 17:22:45 +01:00
Dendy 7a59369808 Folder and input renaming 2022-12-08 17:22:18 +01:00
16 changed files with 1228 additions and 22 deletions

View File

@ -1,7 +1,7 @@
const std = @import("std");
pub fn main() void {
const input = @embedFile("input.asc");
const input = @embedFile("input");
var iter = std.mem.split(u8, input, "\n");
var max: usize = 0;

View File

@ -4,7 +4,7 @@ const std = @import("std");
// to make it as efficient as possible.
pub fn main() !void {
const input = @embedFile("input.asc");
const input = @embedFile("input");
// Putting part 1's solution as an extra :P
std.debug.print("Part 1's solution: {}\n", .{sumTopNElves(input, 1)});

View File

@ -1,7 +1,7 @@
const std = @import("std");
pub fn main() void {
const input = @embedFile("input.asc");
const input = @embedFile("input");
var iter = std.mem.split(u8, input, "\n");
var totalPoints: usize = 0;

View File

@ -1,7 +1,7 @@
const std = @import("std");
pub fn main() void {
const input = @embedFile("input.asc");
const input = @embedFile("input");
var iter = std.mem.split(u8, input, "\n");
var totalPoints: usize = 0;

View File

@ -7,7 +7,7 @@ const Iterator = std.mem.SplitIterator(u8);
const stepFnType = *const fn (*Iterator, []string) ?[]string;
pub fn main() void {
const input = @embedFile("input.asc");
const input = @embedFile("input");
// The fundamental difference lies in the way of processing the file,
// so we abstract that into a function to be able to pass its pointer.

View File

@ -7,7 +7,7 @@ pub fn main() void {
var totalFit: usize = 0;
var totalAabb: usize = 0;
const input = @embedFile("input.asc");
const input = @embedFile("input");
var iter = std.mem.split(u8, input, "\n");
while (iter.next()) |line| {
@ -35,7 +35,6 @@ pub fn main() void {
if (fitTest(nums[0..2], nums[2..4])) totalFit += 1;
if (aabbTest(nums[0..2], nums[2..4])) totalAabb += 1;
}
std.debug.print("Part 1: {}\n", .{totalFit});

View File

@ -17,14 +17,13 @@ pub fn main() void {
defer arena.deinit();
const allocator = arena.allocator();
const input = @embedFile("input.asc");
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

View File

@ -1,7 +1,7 @@
const std = @import("std");
pub fn main() void {
const input = @embedFile("input.asc");
const input = @embedFile("input");
std.debug.print("Part 1: {}\n", .{findDisctinctSubstrIdx(input, 4)});
std.debug.print("Part 2: {}\n", .{findDisctinctSubstrIdx(input, 14)});

105
07/1-2.zig Normal file
View File

@ -0,0 +1,105 @@
const std = @import("std");
pub fn main() void {
const input = @embedFile("input");
var part1: usize = 0;
// Go through each command one by one
var iter = dirIterator.init(input);
const root = sumSizes(&iter, 100_000, &part1);
std.debug.print("Part 1: {}\n", .{part1});
var part2: usize = 0;
_ = findBiggest(iter.reset(), root - 40_000_000, &part2);
std.debug.print("Part 2: {}\n", .{part2});
}
fn eql(a: []const u8, b: []const u8) bool {
return std.mem.eql(u8, a, b);
}
fn findBiggest(iter: *dirIterator, minimum: usize, global: *usize) usize {
var size: usize = 0;
while (iter.next()) |cmd| {
size += switch (cmd) {
.file => iter.parseSize(),
.dirEnter => findBiggest(iter, minimum, global),
.dirExit => break,
};
}
// If the size doesn't exceed the limit, add it to the total
if (size > minimum and (size < global.* or global.* == 0)) global.* = size;
// Return the size witout filtering
return size;
}
fn sumSizes(iter: *dirIterator, limit: usize, total: *usize) usize {
var size: usize = 0;
while (iter.next()) |cmd| {
size += switch (cmd) {
.file => iter.parseSize(),
.dirEnter => sumSizes(iter, limit, total),
.dirExit => break,
};
}
// If the size doesn't exceed the limit, add it to the total
total.* += if (size <= limit) size else 0;
// Return the size witout filtering
return size;
}
// Helper to iterate through the commands
const dirIterator = struct {
iter: std.mem.SplitIterator(u8),
const cmdType = enum {
dirEnter,
dirExit,
file,
};
pub fn init(input: []const u8) dirIterator {
return .{ .iter = std.mem.split(u8, input, "\n") };
}
// Returns the file size of the current line or 0
pub fn parseSize(self: dirIterator) usize {
const numSlice = std.mem.sliceTo(self.iter.rest(), ' ');
return std.fmt.parseInt(usize, numSlice, 10) catch 0;
}
pub fn next(self: *dirIterator) ?cmdType {
// Go through each command one by one
while (self.iter.next()) |line| {
// Not a valid input
if (line.len <= 0) continue;
// Changing folder (quitting or recursing)
if (line.len > 5 and eql(line[0..5], "$ cd ")) {
// Reached the end of the folder
if (eql(line[5..], "..")) return cmdType.dirExit;
// "cd" command not to parent, then it's a child
return cmdType.dirEnter;
}
// Not a cd, maybe it's a size
return cmdType.file;
}
// Exhausted the input
return null;
}
pub fn reset(self: *dirIterator) *dirIterator {
self.iter.reset();
return self;
}
};

1103
07/input Normal file

File diff suppressed because it is too large Load Diff