Solve day 9
This commit is contained in:
parent
d03e2f28da
commit
2f485e9dbc
107
src/day09.zig
107
src/day09.zig
@ -2,69 +2,73 @@ const std = @import("std");
|
|||||||
|
|
||||||
pub const input = @embedFile("input/day09.txt");
|
pub const input = @embedFile("input/day09.txt");
|
||||||
const test_input1 = @embedFile("input/day09_test1.txt");
|
const test_input1 = @embedFile("input/day09_test1.txt");
|
||||||
|
const test_input2 = @embedFile("input/day09_test2.txt");
|
||||||
|
|
||||||
var gpa = std.heap.GeneralPurposeAllocator(.{ .safety = true }){};
|
var gpa = std.heap.GeneralPurposeAllocator(.{ .safety = true }){};
|
||||||
const allocator = gpa.allocator();
|
const allocator = gpa.allocator();
|
||||||
|
|
||||||
|
fn move_rope(comptime S: usize, knots: *[S][2]isize, line: []const u8, visited: *std.AutoHashMap([2]isize, void)) !void {
|
||||||
|
const direction = line[0];
|
||||||
|
const steps = try std.fmt.parseInt(u8, line[2..], 10);
|
||||||
|
var step: usize = 0;
|
||||||
|
while (step < steps) : (step += 1) {
|
||||||
|
switch (direction) {
|
||||||
|
'R' => {
|
||||||
|
knots[0][0] += 1;
|
||||||
|
},
|
||||||
|
'L' => {
|
||||||
|
knots[0][0] -= 1;
|
||||||
|
},
|
||||||
|
'U' => {
|
||||||
|
knots[0][1] -= 1;
|
||||||
|
},
|
||||||
|
'D' => {
|
||||||
|
knots[0][1] += 1;
|
||||||
|
},
|
||||||
|
else => unreachable,
|
||||||
|
}
|
||||||
|
|
||||||
|
var i: usize = 1;
|
||||||
|
while (i < knots.len) : (i += 1) {
|
||||||
|
var x_diff = knots[i][0] - knots[i - 1][0];
|
||||||
|
var y_diff = knots[i][1] - knots[i - 1][1];
|
||||||
|
if (x_diff < -1 or x_diff > 1 or y_diff < -1 or y_diff > 1) {
|
||||||
|
if (x_diff < 0) {
|
||||||
|
knots[i][0] += 1;
|
||||||
|
} else if (x_diff > 0) {
|
||||||
|
knots[i][0] -= 1;
|
||||||
|
}
|
||||||
|
if (y_diff < 0) {
|
||||||
|
knots[i][1] += 1;
|
||||||
|
} else if (y_diff > 0) {
|
||||||
|
knots[i][1] -= 1;
|
||||||
|
}
|
||||||
|
if (i == knots.len - 1) try visited.put(knots[i], {});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn solve_part1(data: []const u8) !usize {
|
pub fn solve_part1(data: []const u8) !usize {
|
||||||
var lines = std.mem.tokenize(u8, data, "\n");
|
var lines = std.mem.tokenize(u8, data, "\n");
|
||||||
var visited = std.AutoHashMap([2]isize, void).init(allocator);
|
var visited = std.AutoHashMap([2]isize, void).init(allocator);
|
||||||
var head = [2]isize{ 0, 0 };
|
var knots = [2][2]isize{ [2]isize{ 0, 0 }, [2]isize{ 0, 0 } };
|
||||||
var tail = [2]isize{ 0, 0 };
|
try visited.put(knots[0], {});
|
||||||
try visited.put(tail, {});
|
|
||||||
while (lines.next()) |line| {
|
while (lines.next()) |line| {
|
||||||
const direction = line[0];
|
try move_rope(2, &knots, line, &visited);
|
||||||
const steps = try std.fmt.parseInt(u8, line[2..], 10);
|
|
||||||
// std.debug.print("{s} {d}\n", .{ [_]u8{direction}, steps });
|
|
||||||
var step: usize = 0;
|
|
||||||
while (step < steps) : (step += 1) {
|
|
||||||
// std.debug.print("move head: {s}\n", .{[_]u8{direction}});
|
|
||||||
switch (direction) {
|
|
||||||
'R' => {
|
|
||||||
head[0] += 1;
|
|
||||||
},
|
|
||||||
'L' => {
|
|
||||||
head[0] -= 1;
|
|
||||||
},
|
|
||||||
'U' => {
|
|
||||||
head[1] -= 1;
|
|
||||||
},
|
|
||||||
'D' => {
|
|
||||||
head[1] += 1;
|
|
||||||
},
|
|
||||||
else => unreachable,
|
|
||||||
}
|
|
||||||
var x_diff = tail[0] - head[0];
|
|
||||||
var y_diff = tail[1] - head[1];
|
|
||||||
// std.debug.print("x_diff: {d}\n", .{x_diff});
|
|
||||||
// std.debug.print("y_diff: {d}\n", .{y_diff});
|
|
||||||
if (x_diff < -1 or x_diff > 1 or y_diff < -1 or y_diff > 1) {
|
|
||||||
if (x_diff < 0) {
|
|
||||||
// std.debug.print("move tail x: {d}\n", .{-1});
|
|
||||||
tail[0] += 1;
|
|
||||||
} else if (x_diff > 0) {
|
|
||||||
// std.debug.print("move tail x: {d}\n", .{1});
|
|
||||||
tail[0] -= 1;
|
|
||||||
}
|
|
||||||
if (y_diff < 0) {
|
|
||||||
// std.debug.print("move tail y: {d}\n", .{-1});
|
|
||||||
tail[1] += 1;
|
|
||||||
} else if (y_diff > 0) {
|
|
||||||
// std.debug.print("move tail y: {d}\n", .{1});
|
|
||||||
tail[1] -= 1;
|
|
||||||
}
|
|
||||||
try visited.put(tail, {});
|
|
||||||
}
|
|
||||||
// std.debug.print("head: {d}\n", .{head});
|
|
||||||
// std.debug.print("tail: {d}\n", .{tail});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return visited.count();
|
return visited.count();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn solve_part2(data: []const u8) !usize {
|
pub fn solve_part2(data: []const u8) !usize {
|
||||||
_ = data;
|
var lines = std.mem.tokenize(u8, data, "\n");
|
||||||
return 0;
|
var visited = std.AutoHashMap([2]isize, void).init(allocator);
|
||||||
|
var knots = [_][2]isize{[_]isize{ 0, 0 }} ** 10;
|
||||||
|
try visited.put(knots[0], {});
|
||||||
|
while (lines.next()) |line| {
|
||||||
|
try move_rope(10, &knots, line, &visited);
|
||||||
|
}
|
||||||
|
return visited.count();
|
||||||
}
|
}
|
||||||
|
|
||||||
test "solves part1" {
|
test "solves part1" {
|
||||||
@ -72,5 +76,6 @@ test "solves part1" {
|
|||||||
}
|
}
|
||||||
|
|
||||||
test "solves part2" {
|
test "solves part2" {
|
||||||
try std.testing.expectEqual(solve_part2(test_input1), 0);
|
try std.testing.expectEqual(solve_part2(test_input1), 1);
|
||||||
|
try std.testing.expectEqual(solve_part2(test_input2), 36);
|
||||||
}
|
}
|
||||||
|
8
src/input/day09_test2.txt
Normal file
8
src/input/day09_test2.txt
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
R 5
|
||||||
|
U 8
|
||||||
|
L 8
|
||||||
|
D 3
|
||||||
|
R 17
|
||||||
|
D 10
|
||||||
|
L 25
|
||||||
|
U 20
|
Loading…
Reference in New Issue
Block a user