summaryrefslogtreecommitdiff
path: root/src/day01.zig
diff options
context:
space:
mode:
authorXander Bil <xander@biltopia.org>2024-12-02 23:38:23 +0100
committerXander Bil <xander@biltopia.org>2024-12-02 23:38:23 +0100
commit1c2490f5f2c9419ddb5035fc3a8967beef691e6c (patch)
tree0dd2cb0a588e5fff69d7002585ace9bc81099fb4 /src/day01.zig
downloadaoc2024-1c2490f5f2c9419ddb5035fc3a8967beef691e6c.tar.xz
aoc2024-1c2490f5f2c9419ddb5035fc3a8967beef691e6c.zip
solutions day1 and day2
Diffstat (limited to 'src/day01.zig')
-rw-r--r--src/day01.zig81
1 files changed, 81 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});
+}