Compare commits
No commits in common. "1ca55ed289a92c6f86e7b736b063a9e2cee7fb13" and "076ca288f1b52a2b8045f9e1c0808542aca771cf" have entirely different histories.
1ca55ed289
...
076ca288f1
105
07/1-2.zig
105
07/1-2.zig
|
@ -1,105 +0,0 @@
|
|||
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;
|
||||
}
|
||||
};
|
|
@ -1,7 +1,7 @@
|
|||
const std = @import("std");
|
||||
|
||||
pub fn main() void {
|
||||
const input = @embedFile("input");
|
||||
const input = @embedFile("input.asc");
|
||||
var iter = std.mem.split(u8, input, "\n");
|
||||
|
||||
var max: usize = 0;
|
|
@ -4,7 +4,7 @@ const std = @import("std");
|
|||
// to make it as efficient as possible.
|
||||
|
||||
pub fn main() !void {
|
||||
const input = @embedFile("input");
|
||||
const input = @embedFile("input.asc");
|
||||
|
||||
// Putting part 1's solution as an extra :P
|
||||
std.debug.print("Part 1's solution: {}\n", .{sumTopNElves(input, 1)});
|
|
@ -1,7 +1,7 @@
|
|||
const std = @import("std");
|
||||
|
||||
pub fn main() void {
|
||||
const input = @embedFile("input");
|
||||
const input = @embedFile("input.asc");
|
||||
var iter = std.mem.split(u8, input, "\n");
|
||||
|
||||
var totalPoints: usize = 0;
|
|
@ -1,7 +1,7 @@
|
|||
const std = @import("std");
|
||||
|
||||
pub fn main() void {
|
||||
const input = @embedFile("input");
|
||||
const input = @embedFile("input.asc");
|
||||
var iter = std.mem.split(u8, input, "\n");
|
||||
|
||||
var totalPoints: usize = 0;
|
|
@ -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");
|
||||
const input = @embedFile("input.asc");
|
||||
|
||||
// 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.
|
|
@ -7,7 +7,7 @@ pub fn main() void {
|
|||
var totalFit: usize = 0;
|
||||
var totalAabb: usize = 0;
|
||||
|
||||
const input = @embedFile("input");
|
||||
const input = @embedFile("input.asc");
|
||||
var iter = std.mem.split(u8, input, "\n");
|
||||
|
||||
while(iter.next()) |line| {
|
||||
|
@ -35,6 +35,7 @@ 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});
|
|
@ -17,13 +17,14 @@ pub fn main() void {
|
|||
defer arena.deinit();
|
||||
const allocator = arena.allocator();
|
||||
|
||||
const input = @embedFile("input");
|
||||
const input = @embedFile("input.asc");
|
||||
|
||||
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
|
|
@ -1,7 +1,7 @@
|
|||
const std = @import("std");
|
||||
|
||||
pub fn main() void {
|
||||
const input = @embedFile("input");
|
||||
const input = @embedFile("input.asc");
|
||||
|
||||
std.debug.print("Part 1: {}\n", .{findDisctinctSubstrIdx(input, 4)});
|
||||
std.debug.print("Part 2: {}\n", .{findDisctinctSubstrIdx(input, 14)});
|
Reference in New Issue