···
const State = struct { turn: i32, zeroes: u32 };
6
-
const turns: []i32 = blk: {
7
-
const allocator = std.heap.page_allocator;
6
+
const allocator = std.heap.page_allocator;
9
-
const input_raw = @embedFile("./input.txt");
10
-
const input_str = std.mem.trim(u8, input_raw, "\t\r\n");
11
-
var turns_str = std.mem.splitScalar(u8, input_str, '\n');
8
+
const input_raw = @embedFile("./input.txt");
9
+
const input_str = std.mem.trim(u8, input_raw, "\t\r\n");
10
+
var turns_str = std.mem.splitScalar(u8, input_str, '\n');
14
-
std.mem.count(u8, input_str, "\n") + 1;
15
-
const turns = try allocator.alloc(i32, turns_len);
13
+
std.mem.count(u8, input_str, "\n") + 1;
14
+
const turns = try allocator.alloc(i32, turns_len);
15
+
defer allocator.free(turns);
18
-
while (turns_str.next()) |turn_str| {
19
-
const mult: i32 = if (turn_str[0] == 'L') -1 else 1;
20
-
turns[i] = try std.fmt.parseInt(i32, turn_str[1..], 10) * mult;
18
+
while (turns_str.next()) |turn_str| {
19
+
const mult: i32 = if (turn_str[0] == 'L') -1 else 1;
20
+
turns[i] = try std.fmt.parseInt(i32, turn_str[1..], 10) * mult;
var part_1 = State{ .turn = 50, .zeroes = 0 };
28
-
const iturn: i32 = @intCast(part_1.turn);
29
-
part_1.turn = @mod(iturn + turn, 100);
26
+
part_1.turn = @mod(part_1.turn + turn, 100);
part_1.zeroes += @intFromBool(part_1.turn == 0);
std.debug.print("Part 1: {}\n", .{part_1.zeroes});
var part_2 = State{ .turn = 50, .zeroes = 0 };
36
-
const iturn: i32 = @intCast(part_2.turn);
37
-
const raw_turn: i32 = iturn + turn;
33
+
const raw_turn: i32 = part_2.turn + turn;
// if it is below zero before being moduloed and the original number itself wasn't zero it means that it did touch zero but the division thing wouldn't count it, so we give this extra support.
// of course, there is no need to deal with a negative to positive situation because the acc.turn will never be negative!!!
part_2.zeroes += @abs(raw_turn) / 100 + @intFromBool(part_2.turn != 0 and raw_turn <= 0);