From ce18267ed6720503bb8e5da787aedaf7c0434e8c Mon Sep 17 00:00:00 2001 From: ktkk Date: Mon, 1 Dec 2025 16:26:45 +0000 Subject: [PATCH 1/4] 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); + } } From 1a0e6cd0b408acae39407ff2608949a1f99870c8 Mon Sep 17 00:00:00 2001 From: ktkk Date: Mon, 1 Dec 2025 16:29:28 +0000 Subject: [PATCH 2/4] WIP: add missing default case --- src/days/day01.zig | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/days/day01.zig b/src/days/day01.zig index c0c05ed..afc14bf 100644 --- a/src/days/day01.zig +++ b/src/days/day01.zig @@ -60,6 +60,8 @@ fn updatePosition( var mod = @mod(signed_position, signed_upper_bound); if (signed_position < 0) mod += 1; position.* = @intCast(mod); + } else { + position.* = @intCast(signed_position); } } From 3e7efd4d0a7f23433a3253b479f1d219e85433ad Mon Sep 17 00:00:00 2001 From: ktkk Date: Mon, 1 Dec 2025 22:18:44 +0000 Subject: [PATCH 3/4] Finish day01, part 1 --- src/days/day01.zig | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/days/day01.zig b/src/days/day01.zig index afc14bf..df9fec0 100644 --- a/src/days/day01.zig +++ b/src/days/day01.zig @@ -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; } } From fd0a34c35e589ce267723b14f814826b5f90e3ec Mon Sep 17 00:00:00 2001 From: ktkk Date: Mon, 1 Dec 2025 22:47:28 +0000 Subject: [PATCH 4/4] Cheated, used pyt's solution --- src/days/day01.zig | 41 +++++++++++++++++++++-------------------- 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/src/days/day01.zig b/src/days/day01.zig index df9fec0..7a77996 100644 --- a/src/days/day01.zig +++ b/src/days/day01.zig @@ -12,16 +12,18 @@ pub fn run(_: std.mem.Allocator) !void { var zero_hits: u32 = 0; 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 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}); - - if (position == 0) { - zero_hits += 1; - } } var buffer: [8]u8 = undefined; @@ -40,29 +42,28 @@ const Direction = enum { fn updatePosition( position: *u32, + zero_hits: *u32, direction: Direction, amount: u32, upper_bound: u32, -) !u32 { - var signed_position: i32 = @intCast(position.*); - +) !void { switch (direction) { .left => { - signed_position -= @intCast(amount); + for (0..amount) |_| { + if (position.* == 0) position.* = upper_bound; + position.* -= 1; + if (position.* == 0) zero_hits.* += 1; + } }, .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; - } }