Skip to content

Commit

Permalink
standardize input data reading
Browse files Browse the repository at this point in the history
  • Loading branch information
milanaleksic committed Dec 9, 2023
1 parent 628f485 commit 1aba228
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 40 deletions.
52 changes: 40 additions & 12 deletions src/_template.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,38 @@ const util = @import("util.zig");
const mem = std.mem;
const print = std.debug.print;

const Data = struct {
const Self = @This();
allocator: std.mem.Allocator,

rows: std.ArrayList(std.ArrayList(i64)),

pub fn init(allocator: std.mem.Allocator, list: std.ArrayList([]const u8)) !Self {
for (list.items) |line| {
print("line={s}\n", .{line});
}
return Self{
// store data
.allocator = allocator,
};
}

pub fn deinit(self: *Self) void {
_ = self;
// deinit data
}
};

pub fn part1(allocator: std.mem.Allocator, list: std.ArrayList([]const u8)) !i64 {
_ = allocator;
var sum: i64 = 0;

for (list.items) |line| {
print("line={s}\n", .{line});
}
var data = try Data.init(allocator, list);
defer data.deinit();

// access input data...
// for (data.rows.items) |rowData| {

// }

return sum;
}
Expand All @@ -21,24 +46,27 @@ test "part 1 test 1" {
defer list.deinit();

const testValue: i64 = try part1(std.testing.allocator, list);
try std.testing.expectEqual(testValue, 13);
try std.testing.expectEqual(testValue, -1);
}

test "part 1 full" {
var data = try util.openFile(std.testing.allocator, "data/input-N-1.txt");
defer data.deinit();

const testValue: i64 = try part1(std.testing.allocator, data.lines);
try std.testing.expectEqual(testValue, 27059);
try std.testing.expectEqual(testValue, -1);
}

pub fn part2(allocator: std.mem.Allocator, list: std.ArrayList([]const u8)) !i64 {
_ = allocator;
var sum: i64 = 0;

for (list.items) |line| {
print("line={s}\n", .{line});
}
var data = try Data.init(allocator, list);
defer data.deinit();

// access input data...
// for (data.rows.items) |rowData| {

// }

return sum;
}
Expand All @@ -50,13 +78,13 @@ test "part 2 test 1" {
defer list.deinit();

const testValue: i64 = try part2(std.testing.allocator, list);
try std.testing.expectEqual(testValue, 0);
try std.testing.expectEqual(testValue, -1);
}

test "part 2 full" {
var data = try util.openFile(std.testing.allocator, "data/input-N-1.txt");
defer data.deinit();

const testValue: i64 = try part2(std.testing.allocator, data.lines);
try std.testing.expectEqual(testValue, 0);
try std.testing.expectEqual(testValue, -1);
}
69 changes: 41 additions & 28 deletions src/day9.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,46 @@ const util = @import("util.zig");
const mem = std.mem;
const print = std.debug.print;

const Data = struct {
const Self = @This();
allocator: std.mem.Allocator,

rows: std.ArrayList(std.ArrayList(i64)),

pub fn init(allocator: std.mem.Allocator, list: std.ArrayList([]const u8)) !Self {
var rows: std.ArrayList(std.ArrayList(i64)) = std.ArrayList(std.ArrayList(i64)).init(allocator);
for (list.items) |line| {
var row: std.ArrayList(i64) = std.ArrayList(i64).init(allocator);
var lineIter = std.mem.split(u8, line, " ");
while (lineIter.next()) |number| {
if (number.len == 0) {
continue;
}
try row.append(try util.toI64(number));
}
try rows.append(row);
}
return Self{
.rows = rows,
.allocator = allocator,
};
}

pub fn deinit(self: *Self) void {
for (self.rows.items) |row| {
row.deinit();
}
self.rows.deinit();
}
};

pub fn part1(allocator: std.mem.Allocator, list: std.ArrayList([]const u8)) !i64 {
var sum: i64 = 0;

for (list.items) |line| {
// print("line={s}\n", .{line});
var data = try Data.init(allocator, list);
defer data.deinit();

for (data.rows.items) |rowData| {
var pyramid: std.ArrayList(std.ArrayList(i64)) = std.ArrayList(std.ArrayList(i64)).init(allocator);
defer {
for (pyramid.items) |row| {
Expand All @@ -17,17 +51,7 @@ pub fn part1(allocator: std.mem.Allocator, list: std.ArrayList([]const u8)) !i64
pyramid.deinit();
}

var row: std.ArrayList(i64) = std.ArrayList(i64).init(allocator);

// convert to pyramid
var lineIter = std.mem.split(u8, line, " ");
while (lineIter.next()) |number| {
if (number.len == 0) {
continue;
}
try row.append(try util.toI64(number));
}
try pyramid.append(row);
try pyramid.append(try rowData.clone());

// printPyramid(pyramid);

Expand Down Expand Up @@ -103,9 +127,10 @@ test "part 1 full" {
pub fn part2(allocator: std.mem.Allocator, list: std.ArrayList([]const u8)) !i64 {
var sum: i64 = 0;

for (list.items) |line| {
// print("line={s}\n", .{line});
var data = try Data.init(allocator, list);
defer data.deinit();

for (data.rows.items) |rowData| {
var pyramid: std.ArrayList(std.ArrayList(i64)) = std.ArrayList(std.ArrayList(i64)).init(allocator);
defer {
for (pyramid.items) |row| {
Expand All @@ -114,19 +139,7 @@ pub fn part2(allocator: std.mem.Allocator, list: std.ArrayList([]const u8)) !i64
pyramid.deinit();
}

var row: std.ArrayList(i64) = std.ArrayList(i64).init(allocator);

// convert to pyramid
var lineIter = std.mem.split(u8, line, " ");
while (lineIter.next()) |number| {
if (number.len == 0) {
continue;
}
try row.append(try util.toI64(number));
}
try pyramid.append(row);

// printPyramid(pyramid);
try pyramid.append(try rowData.clone());

// calculate lower rows
while (true) {
Expand Down

0 comments on commit 1aba228

Please sign in to comment.