Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
milanaleksic committed Dec 20, 2023
1 parent 863ff91 commit 0c5fe7e
Showing 1 changed file with 83 additions and 37 deletions.
120 changes: 83 additions & 37 deletions src/day14.zig
Original file line number Diff line number Diff line change
Expand Up @@ -72,41 +72,77 @@ const Data = struct {
print("\n", .{});
}
}

pub fn moveToNorth(self: *Self) void {
for (0..self.rows.items.len) |x| {
var foundation: usize = 0;
for (0..self.rows.items.len) |y| {
var cell = self.rows.items[y].items[x];
switch (cell.nodeType) {
NodeType.CubeRock => foundation = y + 1,
NodeType.RoundRock => {
if (foundation < y) {
// print("Moving item from {d} to {d}\n", .{ y, foundation });
self.rows.items[foundation].items[x].nodeType = NodeType.RoundRock;
self.rows.items[y].items[x].nodeType = NodeType.Empty;
}
foundation += 1;
},
else => {},
}
}
}
}

// pub fn moveToSouth(self: *Self) void {
// for (0..self.rows.items.len) |x| {
// var foundation: usize = self.rows.items.len - 1;
// for (self.rows.items, 0..) |*row, y| {
// var cell = row.items[x];
// switch (cell.nodeType) {
// NodeType.CubeRock => foundation = y + 1,
// NodeType.RoundRock => {
// if (foundation < y) {
// // print("Moving item from {d} to {d}\n", .{ y, foundation });
// self.rows.items[foundation].items[x].nodeType = NodeType.RoundRock;
// self.rows.items[y].items[x].nodeType = NodeType.Empty;
// }
// foundation += 1;
// },
// else => {},
// }
// }
// }
// }

pub fn calculate(self: *Self) usize {
var sum: usize = 0;
const colSize = self.rows.items.len;
for (self.rows.items, 0..) |row, y| {
for (row.items) |node| {
switch (node.nodeType) {
NodeType.RoundRock => {
sum += colSize - y;
},
else => {},
}
}
}
return sum;
}
};

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

var data = try Data.init(allocator, list);
defer data.deinit();

// data.printMap();

for (0..data.rows.items.len) |x| {
var foundation: usize = 0;
for (data.rows.items, 0..) |*row, y| {
var cell = row.items[x];
switch (cell.nodeType) {
NodeType.CubeRock => foundation = y + 1,
NodeType.RoundRock => {
if (foundation < y) {
// print("Moving item from {d} to {d}\n", .{ y, foundation });
data.rows.items[foundation].items[x].nodeType = NodeType.RoundRock;
data.rows.items[y].items[x].nodeType = NodeType.Empty;
sum += data.rows.items.len - foundation;
} else {
sum += data.rows.items.len - y;
}
foundation += 1;
},
else => {},
}
}
}
data.moveToNorth();

// data.printMap();

return sum;
return data.calculate();
}

test "part 1 test 1" {
Expand Down Expand Up @@ -136,34 +172,44 @@ test "part 1 full" {
try std.testing.expectEqual(testValue, 113456);
}

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

pub fn part2(allocator: std.mem.Allocator, list: std.ArrayList([]const u8)) !usize {
var data = try Data.init(allocator, list);
defer data.deinit();

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

// }
for (0..100) |_| {
data.moveToNorth();
// data.moveToWest();
// data.moveToSouth();
// data.moveToEast();
print("{d}\n", .{data.calculate()});
}

return sum;
return data.calculate();
}

test "part 2 test 1" {
var list = try util.parseToListOfStrings([]const u8,
\\...
\\O....#....
\\O.OO#....#
\\.....##...
\\OO.#O....O
\\.O.....O#.
\\O.#..O.#.#
\\..O..#O..O
\\.......O..
\\#....###..
\\#OO..#....
);
defer list.deinit();

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

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

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

0 comments on commit 0c5fe7e

Please sign in to comment.