Solve day 9 part 1
This commit is contained in:
parent
1afee9d007
commit
d03e2f28da
76
src/day09.zig
Normal file
76
src/day09.zig
Normal file
@ -0,0 +1,76 @@
|
|||||||
|
const std = @import("std");
|
||||||
|
|
||||||
|
pub const input = @embedFile("input/day09.txt");
|
||||||
|
const test_input1 = @embedFile("input/day09_test1.txt");
|
||||||
|
|
||||||
|
var gpa = std.heap.GeneralPurposeAllocator(.{ .safety = true }){};
|
||||||
|
const allocator = gpa.allocator();
|
||||||
|
|
||||||
|
pub fn solve_part1(data: []const u8) !usize {
|
||||||
|
var lines = std.mem.tokenize(u8, data, "\n");
|
||||||
|
var visited = std.AutoHashMap([2]isize, void).init(allocator);
|
||||||
|
var head = [2]isize{ 0, 0 };
|
||||||
|
var tail = [2]isize{ 0, 0 };
|
||||||
|
try visited.put(tail, {});
|
||||||
|
while (lines.next()) |line| {
|
||||||
|
const direction = line[0];
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn solve_part2(data: []const u8) !usize {
|
||||||
|
_ = data;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
test "solves part1" {
|
||||||
|
try std.testing.expectEqual(solve_part1(test_input1), 13);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "solves part2" {
|
||||||
|
try std.testing.expectEqual(solve_part2(test_input1), 0);
|
||||||
|
}
|
2000
src/input/day09.txt
Executable file
2000
src/input/day09.txt
Executable file
File diff suppressed because it is too large
Load Diff
8
src/input/day09_test1.txt
Normal file
8
src/input/day09_test1.txt
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
R 4
|
||||||
|
U 4
|
||||||
|
L 3
|
||||||
|
D 1
|
||||||
|
R 4
|
||||||
|
D 1
|
||||||
|
L 5
|
||||||
|
R 2
|
@ -8,6 +8,7 @@ const day05 = @import("day05.zig");
|
|||||||
const day06 = @import("day06.zig");
|
const day06 = @import("day06.zig");
|
||||||
const day07 = @import("day07.zig");
|
const day07 = @import("day07.zig");
|
||||||
const day08 = @import("day08.zig");
|
const day08 = @import("day08.zig");
|
||||||
|
const day09 = @import("day09.zig");
|
||||||
const utils = @import("utils.zig");
|
const utils = @import("utils.zig");
|
||||||
|
|
||||||
fn solve_day(comptime day_num: u8, day: anytype, stdout: anytype, timer: *std.time.Timer) !void {
|
fn solve_day(comptime day_num: u8, day: anytype, stdout: anytype, timer: *std.time.Timer) !void {
|
||||||
@ -48,6 +49,7 @@ pub fn main() !void {
|
|||||||
try solve_day(6, day06, &stdout, &timer);
|
try solve_day(6, day06, &stdout, &timer);
|
||||||
try solve_day(7, day07, &stdout, &timer);
|
try solve_day(7, day07, &stdout, &timer);
|
||||||
try solve_day(8, day08, &stdout, &timer);
|
try solve_day(8, day08, &stdout, &timer);
|
||||||
|
try solve_day(9, day09, &stdout, &timer);
|
||||||
|
|
||||||
try bw.flush();
|
try bw.flush();
|
||||||
}
|
}
|
||||||
@ -63,5 +65,6 @@ test {
|
|||||||
_ = day06;
|
_ = day06;
|
||||||
_ = day07;
|
_ = day07;
|
||||||
_ = day08;
|
_ = day08;
|
||||||
|
_ = day09;
|
||||||
_ = utils;
|
_ = utils;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user