Finish day01, part 1
This commit is contained in:
parent
1a0e6cd0b4
commit
3e7efd4d0a
1 changed files with 10 additions and 9 deletions
|
|
@ -14,7 +14,11 @@ pub fn run(_: std.mem.Allocator) !void {
|
|||
while (lines.next()) |line| {
|
||||
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);
|
||||
try updatePosition(&position, direction, amount, 99);
|
||||
|
||||
zero_hits += try updatePosition(&position, direction, amount, 100);
|
||||
|
||||
std.debug.print("{s} => {d}\n", .{line, position});
|
||||
|
||||
if (position == 0) {
|
||||
zero_hits += 1;
|
||||
}
|
||||
|
|
@ -39,7 +43,7 @@ fn updatePosition(
|
|||
direction: Direction,
|
||||
amount: u32,
|
||||
upper_bound: u32,
|
||||
) !void {
|
||||
) !u32 {
|
||||
var signed_position: i32 = @intCast(position.*);
|
||||
|
||||
switch (direction) {
|
||||
|
|
@ -51,17 +55,14 @@ fn updatePosition(
|
|||
},
|
||||
}
|
||||
|
||||
if (signed_position == upper_bound) {
|
||||
position.* = @intCast(signed_position);
|
||||
} else if (signed_position == 0) {
|
||||
position.* = 0;
|
||||
} else if (signed_position > upper_bound or signed_position < 0) {
|
||||
if (signed_position > upper_bound - 1 or signed_position < 0) {
|
||||
const signed_upper_bound: i32 = @intCast(upper_bound);
|
||||
var mod = @mod(signed_position, signed_upper_bound);
|
||||
if (signed_position < 0) mod += 1;
|
||||
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