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");
|
const std = @import("std");
|
||||||
|
|
||||||
pub fn main() void {
|
pub fn main() void {
|
||||||
const input = @embedFile("input");
|
const input = @embedFile("input.asc");
|
||||||
var iter = std.mem.split(u8, input, "\n");
|
var iter = std.mem.split(u8, input, "\n");
|
||||||
|
|
||||||
var max: usize = 0;
|
var max: usize = 0;
|
|
@ -4,7 +4,7 @@ const std = @import("std");
|
||||||
// to make it as efficient as possible.
|
// to make it as efficient as possible.
|
||||||
|
|
||||||
pub fn main() !void {
|
pub fn main() !void {
|
||||||
const input = @embedFile("input");
|
const input = @embedFile("input.asc");
|
||||||
|
|
||||||
// Putting part 1's solution as an extra :P
|
// Putting part 1's solution as an extra :P
|
||||||
std.debug.print("Part 1's solution: {}\n", .{sumTopNElves(input, 1)});
|
std.debug.print("Part 1's solution: {}\n", .{sumTopNElves(input, 1)});
|
||||||
|
@ -30,7 +30,7 @@ pub fn sumTopNElves(input: []const u8, comptime cant: usize) usize {
|
||||||
var advance: usize = 1;
|
var advance: usize = 1;
|
||||||
while (advance < cant) : (advance += 1) {
|
while (advance < cant) : (advance += 1) {
|
||||||
// Advance and wrap around
|
// Advance and wrap around
|
||||||
const i = (min_i + advance) % cant;
|
const i = (min_i + advance)%cant;
|
||||||
|
|
||||||
if (maxArr[i] < maxArr[min_i]) min_i = i;
|
if (maxArr[i] < maxArr[min_i]) min_i = i;
|
||||||
}
|
}
|
||||||
|
@ -59,7 +59,7 @@ const ElfIterator = struct {
|
||||||
pub fn next(self: *ElfIterator) ?usize {
|
pub fn next(self: *ElfIterator) ?usize {
|
||||||
var res: usize = 0;
|
var res: usize = 0;
|
||||||
|
|
||||||
while (self.iter.next()) |calories| {
|
while(self.iter.next()) |calories| {
|
||||||
// If non numeric assume EOF (End Of elF)
|
// If non numeric assume EOF (End Of elF)
|
||||||
res += std.fmt.parseInt(usize, calories, 10) catch return res;
|
res += std.fmt.parseInt(usize, calories, 10) catch return res;
|
||||||
}
|
}
|
|
@ -1,7 +1,7 @@
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
|
|
||||||
pub fn main() void {
|
pub fn main() void {
|
||||||
const input = @embedFile("input");
|
const input = @embedFile("input.asc");
|
||||||
var iter = std.mem.split(u8, input, "\n");
|
var iter = std.mem.split(u8, input, "\n");
|
||||||
|
|
||||||
var totalPoints: usize = 0;
|
var totalPoints: usize = 0;
|
||||||
|
@ -19,7 +19,7 @@ pub fn main() void {
|
||||||
// Points per shape chosen is easy.
|
// Points per shape chosen is easy.
|
||||||
// {0,1,2} + 1 == {1,2,3}
|
// {0,1,2} + 1 == {1,2,3}
|
||||||
// which is what we want.
|
// which is what we want.
|
||||||
const shapePoints = me + 1;
|
const shapePoints = me+1;
|
||||||
|
|
||||||
// Now this is a bit more complex.
|
// Now this is a bit more complex.
|
||||||
// We notice that 'them - me' gives:
|
// We notice that 'them - me' gives:
|
||||||
|
@ -52,7 +52,7 @@ pub fn main() void {
|
||||||
//
|
//
|
||||||
// Now we multiply by three so '(2-(result+4)%3)*3'
|
// Now we multiply by three so '(2-(result+4)%3)*3'
|
||||||
// and boom, points per result per match.
|
// and boom, points per result per match.
|
||||||
const resultPoints = (2 - ((4 + them) - me) % 3) * 3;
|
const resultPoints = (2-((4 + them) - me)%3)*3;
|
||||||
|
|
||||||
totalPoints += resultPoints + shapePoints;
|
totalPoints += resultPoints + shapePoints;
|
||||||
}
|
}
|
|
@ -1,7 +1,7 @@
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
|
|
||||||
pub fn main() void {
|
pub fn main() void {
|
||||||
const input = @embedFile("input");
|
const input = @embedFile("input.asc");
|
||||||
var iter = std.mem.split(u8, input, "\n");
|
var iter = std.mem.split(u8, input, "\n");
|
||||||
|
|
||||||
var totalPoints: usize = 0;
|
var totalPoints: usize = 0;
|
||||||
|
@ -52,7 +52,7 @@ pub fn main() void {
|
||||||
// win(2) {2,0,1} {1,2,0}
|
// win(2) {2,0,1} {1,2,0}
|
||||||
//
|
//
|
||||||
// Now it's just a matter of adding one to that
|
// Now it's just a matter of adding one to that
|
||||||
const shapePoints = ((them + result + 2) % 3) + 1;
|
const shapePoints = ((them + result + 2)%3) + 1;
|
||||||
|
|
||||||
totalPoints += resultPoints + shapePoints;
|
totalPoints += resultPoints + shapePoints;
|
||||||
}
|
}
|
|
@ -7,7 +7,7 @@ const Iterator = std.mem.SplitIterator(u8);
|
||||||
const stepFnType = *const fn (*Iterator, []string) ?[]string;
|
const stepFnType = *const fn (*Iterator, []string) ?[]string;
|
||||||
|
|
||||||
pub fn main() void {
|
pub fn main() void {
|
||||||
const input = @embedFile("input");
|
const input = @embedFile("input.asc");
|
||||||
|
|
||||||
// The fundamental difference lies in the way of processing the file,
|
// 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.
|
// so we abstract that into a function to be able to pass its pointer.
|
|
@ -7,10 +7,10 @@ pub fn main() void {
|
||||||
var totalFit: usize = 0;
|
var totalFit: usize = 0;
|
||||||
var totalAabb: usize = 0;
|
var totalAabb: usize = 0;
|
||||||
|
|
||||||
const input = @embedFile("input");
|
const input = @embedFile("input.asc");
|
||||||
var iter = std.mem.split(u8, input, "\n");
|
var iter = std.mem.split(u8, input, "\n");
|
||||||
|
|
||||||
while (iter.next()) |line| {
|
while(iter.next()) |line| {
|
||||||
if (line.len <= 0) continue;
|
if (line.len <= 0) continue;
|
||||||
|
|
||||||
//////////////////
|
//////////////////
|
||||||
|
@ -18,11 +18,11 @@ pub fn main() void {
|
||||||
////////////////
|
////////////////
|
||||||
|
|
||||||
const delim = "-,-";
|
const delim = "-,-";
|
||||||
if ((delim.len + 1) & 1 != 0) @panic("Amount of nums should always be even");
|
if ((delim.len+1)&1 != 0) @panic("Amount of nums should always be even");
|
||||||
var nums = [_]u8{undefined} ** (delim.len + 1);
|
var nums = [_]u8{undefined} ** (delim.len+1);
|
||||||
var startIdx: usize = 0;
|
var startIdx: usize = 0;
|
||||||
|
|
||||||
inline for (nums) |_, i| {
|
inline for(nums) |_,i| {
|
||||||
// Get the number
|
// Get the number
|
||||||
const numstr = if (i < delim.len)
|
const numstr = if (i < delim.len)
|
||||||
std.mem.sliceTo(line[startIdx..], delim[i])
|
std.mem.sliceTo(line[startIdx..], delim[i])
|
||||||
|
@ -35,6 +35,7 @@ pub fn main() void {
|
||||||
|
|
||||||
if (fitTest(nums[0..2], nums[2..4])) totalFit += 1;
|
if (fitTest(nums[0..2], nums[2..4])) totalFit += 1;
|
||||||
if (aabbTest(nums[0..2], nums[2..4])) totalAabb += 1;
|
if (aabbTest(nums[0..2], nums[2..4])) totalAabb += 1;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
std.debug.print("Part 1: {}\n", .{totalFit});
|
std.debug.print("Part 1: {}\n", .{totalFit});
|
|
@ -17,13 +17,14 @@ pub fn main() void {
|
||||||
defer arena.deinit();
|
defer arena.deinit();
|
||||||
const allocator = arena.allocator();
|
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 1: {s}\n", .{crane900X(input,moveAppend, allocator)});
|
||||||
std.debug.print("Part 2: {s}\n", .{crane900X(input, moveAppendSlice, 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 {
|
fn crane900X(input: []const u8, moveFn: moveFnType, allocator: std.mem.Allocator) []const u8 {
|
||||||
|
|
||||||
var iter = std.mem.split(u8, input, "\n");
|
var iter = std.mem.split(u8, input, "\n");
|
||||||
|
|
||||||
// Parses the header with the states and advances the counter
|
// Parses the header with the states and advances the counter
|
|
@ -1,7 +1,7 @@
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
|
|
||||||
pub fn main() void {
|
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 1: {}\n", .{findDisctinctSubstrIdx(input, 4)});
|
||||||
std.debug.print("Part 2: {}\n", .{findDisctinctSubstrIdx(input, 14)});
|
std.debug.print("Part 2: {}\n", .{findDisctinctSubstrIdx(input, 14)});
|
Reference in New Issue