Skip to content

Commit

Permalink
18-2
Browse files Browse the repository at this point in the history
  • Loading branch information
milanaleksic committed Jan 2, 2024
1 parent 8083c56 commit 7d99044
Showing 1 changed file with 75 additions and 15 deletions.
90 changes: 75 additions & 15 deletions src/day18.zig
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const Data = struct {
coords: std.ArrayList(Coord),
numberOfSteps: i64,

pub fn init(allocator: std.mem.Allocator, list: std.ArrayList([]const u8)) !Self {
pub fn initPart1(allocator: std.mem.Allocator, list: std.ArrayList([]const u8)) !Self {
var coords = std.ArrayList(Coord).init(allocator);
var coord = Coord{ .x = 0, .y = 0 };
var numberOfSteps: i64 = 0;
Expand Down Expand Up @@ -76,6 +76,61 @@ const Data = struct {
};
}

pub fn initPart2(allocator: std.mem.Allocator, list: std.ArrayList([]const u8)) !Self {
var coords = std.ArrayList(Coord).init(allocator);
var coord = Coord{ .x = 0, .y = 0 };
var numberOfSteps: i64 = 0;
for (list.items) |line| {
// print("line={s}\n", .{line});
var sliceIter = std.mem.split(u8, line, " ");
// ignore first
_ = sliceIter.next().?;
// ignore second
_ = sliceIter.next().?;
const encodedInstruction = sliceIter.next().?;
// std.debug.print("Decoding {s}\n", .{encodedInstruction[2..7]});
const amount = try std.fmt.parseInt(i64, encodedInstruction[2..7], 16);
switch (encodedInstruction[7]) {
'3' => coord.y -= amount,
'1' => coord.y += amount,
'2' => coord.x -= amount,
'0' => coord.x += amount,
else => undefined,
}
numberOfSteps += amount;
try coords.append(coord);
}

// shift all nodes to absolute values for simplicity
var minX: i64 = 0;
var minY: i64 = 0;
for (coords.items) |coordIter| {
if (coordIter.x < minX) {
minX = coordIter.x;
}
if (coordIter.y < minY) {
minY = coordIter.y;
}
}

if (minX < 0 or minY < 0) {
for (coords.items) |*coordFinal| {
if (minX < 0) {
coordFinal.x -= minX;
}
if (minY < 0) {
coordFinal.y -= minY;
}
}
}

return Self{
.allocator = allocator,
.numberOfSteps = numberOfSteps,
.coords = coords,
};
}

pub fn deinit(self: *Self) void {
self.coords.deinit();
}
Expand All @@ -102,7 +157,7 @@ const Data = struct {
};

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

return data.countHoles();
Expand Down Expand Up @@ -135,39 +190,44 @@ test "part 1 full" {
var data = try util.openFile(std.testing.allocator, "data/input-18.txt");
defer data.deinit();

// 55507 is too high
const testValue: i64 = try part1(std.testing.allocator, data.lines);
try std.testing.expectEqual(testValue, 52231);
}

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

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

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

// }

return sum;
return data.countHoles();
}

test "part 2 test 1" {
var list = try util.parseToListOfStrings([]const u8,
\\...
\\R 6 (#70c710)
\\D 5 (#0dc571)
\\L 2 (#5713f0)
\\D 2 (#d2c081)
\\R 2 (#59c680)
\\D 2 (#411b91)
\\L 5 (#8ceee2)
\\U 2 (#caa173)
\\L 1 (#1b58a2)
\\U 2 (#caa171)
\\R 2 (#7807d2)
\\U 3 (#a77fa3)
\\L 2 (#015232)
\\U 2 (#7a21e3)
);
defer list.deinit();

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

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

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

0 comments on commit 7d99044

Please sign in to comment.