Skip to content

Commit

Permalink
add solution for 2015/02/02
Browse files Browse the repository at this point in the history
  • Loading branch information
demiazz committed Nov 11, 2024
1 parent eb7af83 commit 8a99d41
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 0 deletions.
110 changes: 110 additions & 0 deletions native/solutions/2015/02/part_2.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
const panic = @import("panic");
const std = @import("std");

const Allocator = std.mem.Allocator;
const AnyReader = std.io.AnyReader;

const BytesList = std.ArrayList(u8);

const Parser = struct {
total: i32,

data: [3]i32,
index: usize,

buffer: BytesList,

fn init(allocator: Allocator) Parser {
return .{
.total = 0,

.data = .{ 0, 0, 0 },
.index = 0,

.buffer = BytesList.init(allocator),
};
}

fn write(self: *Parser, byte: u8) !void {
return switch (byte) {
'0'...'9' => try self.buffer.append(byte),
'x' => try self.flushDimension(),
'\n' => try self.flushTotal(),
else => error.InvalidInput,
};
}

fn flushDimension(self: *Parser) !void {
if (self.buffer.items.len > 0) {
self.data[self.index] = try std.fmt.parseInt(i32, self.buffer.items, 10);
}

self.index += 1;

self.buffer.clearAndFree();
}

fn flushTotal(self: *Parser) !void {
try self.flushDimension();

var ribbon: i32 = self.data[0] + self.data[1] + self.data[2];

const maxDs = blk: {
var max = self.data[0];

if (self.data[1] > max) {
max = self.data[1];
}

if (self.data[2] > max) {
max = self.data[2];
}

break :blk max;
};

ribbon -= maxDs;
ribbon *= 2;

ribbon += self.data[0] * self.data[1] * self.data[2];

self.total += ribbon;

self.data[0] = 0;
self.data[1] = 0;
self.data[2] = 0;

self.index = 0;
}

fn deinit(self: *Parser) void {
self.buffer.deinit();
}
};

pub fn solve(allocator: Allocator, reader: AnyReader) ![]const u8 {
var parser = Parser.init(allocator);
defer parser.deinit();

while (reader.readByte()) |byte| {
try parser.write(byte);
} else |err| {
if (err != error.EndOfStream) {
return err;
}
}

try parser.flushTotal();

return try std.fmt.allocPrint(allocator, "{d}", .{parser.total});
}

test "solution 2015/02/01" {
const input_file = @embedFile("./input.fixture");
var input = std.io.fixedBufferStream(input_file);

const answer = try solve(std.testing.allocator, input.reader().any());
defer std.testing.allocator.free(answer);

try std.testing.expectEqualStrings(answer, "3812909");
}
1 change: 1 addition & 0 deletions native/solutions/registry.zig
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ pub const items = [_]Entry{
entry(.year_2015, .day_01, .part_one, index.solver_2015_01_01),
entry(.year_2015, .day_01, .part_two, index.solver_2015_01_02),
entry(.year_2015, .day_02, .part_one, index.solver_2015_02_01),
entry(.year_2015, .day_02, .part_two, index.solver_2015_02_02),
};

pub fn lookup(year: Year, day: Day, part: Part) ?Solver {
Expand Down
1 change: 1 addition & 0 deletions native/solutions/solutions.zig
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub const solver_2015_01_01 = @import("./2015/01/part_1.zig").solve;
pub const solver_2015_01_02 = @import("./2015/01/part_2.zig").solve;
pub const solver_2015_02_01 = @import("./2015/02/part_1.zig").solve;
pub const solver_2015_02_02 = @import("./2015/02/part_2.zig").solve;

0 comments on commit 8a99d41

Please sign in to comment.