From ce18267ed6720503bb8e5da787aedaf7c0434e8c Mon Sep 17 00:00:00 2001 From: ktkk Date: Mon, 1 Dec 2025 16:26:45 +0000 Subject: [PATCH] WIP: implement day 1 --- src/days/day01.zig | 57 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/src/days/day01.zig b/src/days/day01.zig index 9da9d61..c0c05ed 100644 --- a/src/days/day01.zig +++ b/src/days/day01.zig @@ -4,5 +4,62 @@ pub const title = "Day 01: Secret Entrance"; pub fn run(_: std.mem.Allocator) !void { const input = @embedFile("./input/day01.txt"); + var lines = std.mem.tokenizeScalar(u8, input, '\n'); + + const starting_position = 50; + var position: u32 = starting_position; + + var zero_hits: u32 = 0; + + 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); + if (position == 0) { + zero_hits += 1; + } + } + + var buffer: [8]u8 = undefined; + var stdout_writer = std.fs.File.stdout().writer(&buffer); + const stdout = &stdout_writer.interface; + + try stdout.print("{d}\n", .{zero_hits}); + + try stdout.flush(); +} + +const Direction = enum { + left, + right, +}; + +fn updatePosition( + position: *u32, + direction: Direction, + amount: u32, + upper_bound: u32, +) !void { + var signed_position: i32 = @intCast(position.*); + + switch (direction) { + .left => { + signed_position -= @intCast(amount); + }, + .right => { + signed_position += @intCast(amount); + }, + } + + 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) { + const signed_upper_bound: i32 = @intCast(upper_bound); + var mod = @mod(signed_position, signed_upper_bound); + if (signed_position < 0) mod += 1; + position.* = @intCast(mod); + } }