summaryrefslogtreecommitdiff
path: root/src/day01.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/day01.zig')
-rw-r--r--src/day01.zig72
1 files changed, 72 insertions, 0 deletions
diff --git a/src/day01.zig b/src/day01.zig
new file mode 100644
index 0000000..2010816
--- /dev/null
+++ b/src/day01.zig
@@ -0,0 +1,72 @@
+const std = @import("std");
+const print = std.debug.print;
+
+pub fn solve(part: []u8, reader: *std.Io.Reader, _: std.mem.Allocator) !void {
+ if (std.mem.eql(u8, part, "1")) {
+ try part1(reader);
+ } else {
+ try part2(reader);
+ }
+}
+
+fn part1(reader: *std.Io.Reader) !void {
+ var pos: i32 = 50;
+ var zero: usize = 0;
+ while (reader.takeDelimiterInclusive('\n')) |line| {
+ if (line.len == 0){
+ break;
+ }
+
+ const numSlice = std.mem.trimRight(u8, line[1..], &[_]u8{'\n'});
+ const rotations = try std.fmt.parseInt(i32, numSlice, 10);
+ if (line[0] == 'R') {
+ pos += rotations;
+ } else if (line[0] == 'L'){
+ pos -= rotations;
+ }
+
+ pos = @mod(pos, 100);
+
+ if (pos == 0) {
+ zero += 1;
+ }
+
+ } else |_| {
+ }
+
+ print("{d}\n", .{zero});
+}
+
+fn part2(reader: *std.Io.Reader) !void {
+ var pos: i32 = 50;
+ var zero: i32 = 0;
+ while (reader.takeDelimiterInclusive('\n')) |line| {
+ if (line.len == 0){
+ break;
+ }
+
+ const numSlice = std.mem.trimRight(u8, line[1..], &[_]u8{'\n'});
+ var rotations = try std.fmt.parseInt(i32, numSlice, 10);
+
+ zero += @divFloor(rotations, 100);
+ rotations = @rem(rotations, 100);
+
+ if (line[0] == 'L'){
+ rotations *= -1;
+ }
+
+ const with_rotations: i32 = pos + rotations;
+ const new: i32 = @mod(with_rotations, 100);
+
+
+ if ((new != with_rotations and pos != 0) or new == 0){
+ zero += 1;
+ }
+
+ pos = new;
+
+
+ } else |_| {}
+
+ print("{d}\n", .{zero});
+}