summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/day01.zig81
-rw-r--r--src/day02.zig91
2 files changed, 172 insertions, 0 deletions
diff --git a/src/day01.zig b/src/day01.zig
new file mode 100644
index 0000000..33b0a67
--- /dev/null
+++ b/src/day01.zig
@@ -0,0 +1,81 @@
+const std = @import("std");
+const print = std.debug.print;
+
+pub fn solve(part: []u8, buffer: []u8, allocator: std.mem.Allocator) !void {
+ var lines = std.mem.splitScalar(u8, buffer, '\n');
+ var left = std.ArrayList(usize).init(allocator);
+ var right = std.ArrayList(usize).init(allocator);
+ defer left.deinit();
+ defer right.deinit();
+
+ if (std.mem.eql(u8, part, "part1")) {
+ try part1(&lines, &left, &right, allocator);
+ } else {
+ try part2(&lines, &left, &right, allocator);
+ }
+}
+
+fn part1(lines: *std.mem.SplitIterator(u8, .scalar), left: *std.ArrayList(usize), right: *std.ArrayList(usize), allocator: std.mem.Allocator) !void {
+ while (lines.next()) |line| {
+ if (line.len > 0) {
+ var components = std.mem.split(u8, line, " ");
+ try left.append(try std.fmt.parseInt(usize, components.next().?, 10));
+ try right.append(try std.fmt.parseInt(usize, components.next().?, 10));
+ }
+ }
+
+ const left_s = try left.toOwnedSlice();
+ const right_s = try right.toOwnedSlice();
+ defer allocator.free(left_s);
+ defer allocator.free(right_s);
+
+ std.mem.sort(usize, left_s, {}, std.sort.asc(usize));
+ std.mem.sort(usize, right_s, {}, std.sort.asc(usize));
+
+ var sum: usize = 0;
+ for (left_s, right_s) |item_l, item_r| {
+ if (item_l > item_r) {
+ sum += item_l - item_r;
+ } else {
+ sum += item_r - item_l;
+ }
+ }
+
+ print("Sum is {d}\n", .{sum});
+}
+
+fn part2(lines: *std.mem.SplitIterator(u8, .scalar), left: *std.ArrayList(usize), right: *std.ArrayList(usize), allocator: std.mem.Allocator) !void {
+ while (lines.next()) |line| {
+ if (line.len > 0) {
+ var components = std.mem.split(u8, line, " ");
+ try left.append(try std.fmt.parseInt(usize, components.next().?, 10));
+ try right.append(try std.fmt.parseInt(usize, components.next().?, 10));
+ }
+ }
+
+ const left_s = try left.toOwnedSlice();
+ const right_s = try right.toOwnedSlice();
+ defer allocator.free(left_s);
+ defer allocator.free(right_s);
+
+ std.mem.sort(usize, right_s, {}, std.sort.asc(usize));
+
+ var map = std.AutoHashMap(usize, usize).init(allocator);
+ defer map.deinit();
+
+ var sum: usize = 0;
+ for (left_s) |item_l| {
+ sum += map.get(item_l) orelse blk: {
+ var amount: usize = 0;
+ for (right_s) |item_r| {
+ if (item_l == item_r) {
+ amount += item_l;
+ }
+ }
+ try map.put(item_l, amount);
+ break :blk amount;
+ };
+ }
+
+ print("Sum is {d}\n", .{sum});
+}
diff --git a/src/day02.zig b/src/day02.zig
new file mode 100644
index 0000000..cad2d9d
--- /dev/null
+++ b/src/day02.zig
@@ -0,0 +1,91 @@
+const std = @import("std");
+const stdin = std.io.getStdIn();
+const print = std.debug.print;
+
+pub fn solve(part: []u8, buffer: []u8) !void {
+ var lines = std.mem.splitScalar(u8, buffer, '\n');
+ if (std.mem.eql(u8, part, "part1")) {
+ try part1(&lines);
+ } else {
+ try part2(&lines);
+ }
+}
+
+fn part1(lines: *std.mem.SplitIterator(u8, .scalar)) !void {
+ var count: usize = 0;
+ lines: while (lines.next()) |line| {
+ if (line.len > 0) {
+ var components = std.mem.splitScalar(u8, line, ' ');
+ var previous: i32 = try std.fmt.parseInt(i32, components.next().?, 10);
+ var increasing: i32 = 0;
+ while (components.next()) |number_s| {
+ const number = try std.fmt.parseInt(i32, number_s, 10);
+ if (increasing == 0) {
+ if (previous > number) {
+ increasing = -1;
+ } else {
+ increasing = 1;
+ }
+ }
+
+ const difference = (number - previous) * increasing;
+ if (difference < 1 or difference > 3) {
+ continue :lines;
+ }
+ previous = number;
+ }
+ count += 1;
+ }
+ }
+
+ print("Safe count: {d}\n", .{count});
+}
+
+fn part2(lines: *std.mem.SplitIterator(u8, .scalar)) !void {
+ var count: usize = 0;
+ while (lines.next()) |line| {
+ if (line.len > 0) {
+ var components = std.mem.splitScalar(u8, line, ' ');
+
+ var i: usize = 0;
+ var j: usize = 0;
+
+ counter: while (i <= j) {
+ j = 0;
+ components.reset();
+
+ if (i == 0) {
+ _ = components.next();
+ }
+
+ var previous: i32 = try std.fmt.parseInt(i32, components.next().?, 10);
+ var increasing: i32 = 0;
+ while (components.next()) |number_s| {
+ if (i != j) {
+ const number = try std.fmt.parseInt(i32, number_s, 10);
+ if (increasing == 0) {
+ if (previous > number) {
+ increasing = -1;
+ } else {
+ increasing = 1;
+ }
+ }
+
+ const difference = (number - previous) * increasing;
+ if (difference < 1 or difference > 3) {
+ i += 1;
+ continue :counter;
+ }
+ previous = number;
+ }
+
+ j += 1;
+ }
+
+ count += 1;
+ break;
+ }
+ }
+ }
+ print("Safe count: {d}\n", .{count});
+}