Compare commits
No commits in common. "fd0a34c35e589ce267723b14f814826b5f90e3ec" and "d64050a402539c9548631c1ad10499243107574d" have entirely different histories.
fd0a34c35e
...
d64050a402
1 changed files with 0 additions and 61 deletions
|
|
@ -4,66 +4,5 @@ pub const title = "Day 01: Secret Entrance";
|
||||||
|
|
||||||
pub fn run(_: std.mem.Allocator) !void {
|
pub fn run(_: std.mem.Allocator) !void {
|
||||||
const input = @embedFile("./input/day01.txt");
|
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| {
|
|
||||||
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);
|
|
||||||
|
|
||||||
try updatePosition(&position, &zero_hits, direction, amount, 100);
|
|
||||||
// Part 1:
|
|
||||||
// if (position == 0) {
|
|
||||||
// zero_hits += 1;
|
|
||||||
// }
|
|
||||||
|
|
||||||
std.debug.print("{s} => {d}\n", .{line, position});
|
|
||||||
}
|
|
||||||
|
|
||||||
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,
|
|
||||||
zero_hits: *u32,
|
|
||||||
direction: Direction,
|
|
||||||
amount: u32,
|
|
||||||
upper_bound: u32,
|
|
||||||
) !void {
|
|
||||||
switch (direction) {
|
|
||||||
.left => {
|
|
||||||
for (0..amount) |_| {
|
|
||||||
if (position.* == 0) position.* = upper_bound;
|
|
||||||
position.* -= 1;
|
|
||||||
if (position.* == 0) zero_hits.* += 1;
|
|
||||||
}
|
|
||||||
},
|
|
||||||
.right => {
|
|
||||||
for (0..amount) |_| {
|
|
||||||
position.* += 1;
|
|
||||||
if (position.* == 100) {
|
|
||||||
zero_hits.* += 1;
|
|
||||||
position.* = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue