Cheated, used pyt's solution
This commit is contained in:
parent
3e7efd4d0a
commit
fd0a34c35e
1 changed files with 21 additions and 20 deletions
|
|
@ -12,16 +12,18 @@ pub fn run(_: std.mem.Allocator) !void {
|
||||||
var zero_hits: u32 = 0;
|
var zero_hits: u32 = 0;
|
||||||
|
|
||||||
while (lines.next()) |line| {
|
while (lines.next()) |line| {
|
||||||
|
if (line.len == 0) continue;
|
||||||
|
|
||||||
const direction: Direction = if (line[0] == 'L') .left else if (line[0] == 'R') .right else continue;
|
const direction: Direction = if (line[0] == 'L') .left else if (line[0] == 'R') .right else continue;
|
||||||
const amount = try std.fmt.parseUnsigned(u32, line[1..], 10);
|
const amount = try std.fmt.parseUnsigned(u32, line[1..], 10);
|
||||||
|
|
||||||
zero_hits += try updatePosition(&position, direction, amount, 100);
|
try updatePosition(&position, &zero_hits, direction, amount, 100);
|
||||||
|
// Part 1:
|
||||||
|
// if (position == 0) {
|
||||||
|
// zero_hits += 1;
|
||||||
|
// }
|
||||||
|
|
||||||
std.debug.print("{s} => {d}\n", .{line, position});
|
std.debug.print("{s} => {d}\n", .{line, position});
|
||||||
|
|
||||||
if (position == 0) {
|
|
||||||
zero_hits += 1;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var buffer: [8]u8 = undefined;
|
var buffer: [8]u8 = undefined;
|
||||||
|
|
@ -40,29 +42,28 @@ const Direction = enum {
|
||||||
|
|
||||||
fn updatePosition(
|
fn updatePosition(
|
||||||
position: *u32,
|
position: *u32,
|
||||||
|
zero_hits: *u32,
|
||||||
direction: Direction,
|
direction: Direction,
|
||||||
amount: u32,
|
amount: u32,
|
||||||
upper_bound: u32,
|
upper_bound: u32,
|
||||||
) !u32 {
|
) !void {
|
||||||
var signed_position: i32 = @intCast(position.*);
|
|
||||||
|
|
||||||
switch (direction) {
|
switch (direction) {
|
||||||
.left => {
|
.left => {
|
||||||
signed_position -= @intCast(amount);
|
for (0..amount) |_| {
|
||||||
|
if (position.* == 0) position.* = upper_bound;
|
||||||
|
position.* -= 1;
|
||||||
|
if (position.* == 0) zero_hits.* += 1;
|
||||||
|
}
|
||||||
},
|
},
|
||||||
.right => {
|
.right => {
|
||||||
signed_position += @intCast(amount);
|
for (0..amount) |_| {
|
||||||
|
position.* += 1;
|
||||||
|
if (position.* == 100) {
|
||||||
|
zero_hits.* += 1;
|
||||||
|
position.* = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
if (signed_position > upper_bound - 1 or signed_position < 0) {
|
|
||||||
const signed_upper_bound: i32 = @intCast(upper_bound);
|
|
||||||
const mod = @mod(signed_position, signed_upper_bound);
|
|
||||||
position.* = @intCast(mod);
|
|
||||||
return 0;
|
|
||||||
} else {
|
|
||||||
position.* = @intCast(signed_position);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue