From 7b47337e40672c4c5f12a2773d3888c17237f36a Mon Sep 17 00:00:00 2001 From: Xander Bil Date: Wed, 4 Dec 2024 23:58:20 +0100 Subject: solutions day3 and 4 --- src/day01.zig | 6 +-- src/day02.zig | 6 +-- src/day03.zig | 63 +++++++++++++++++++++++++++ src/day04.zig | 136 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 205 insertions(+), 6 deletions(-) create mode 100644 src/day03.zig create mode 100644 src/day04.zig (limited to 'src') diff --git a/src/day01.zig b/src/day01.zig index 33b0a67..d92e756 100644 --- a/src/day01.zig +++ b/src/day01.zig @@ -8,7 +8,7 @@ pub fn solve(part: []u8, buffer: []u8, allocator: std.mem.Allocator) !void { defer left.deinit(); defer right.deinit(); - if (std.mem.eql(u8, part, "part1")) { + if (std.mem.eql(u8, part, "1")) { try part1(&lines, &left, &right, allocator); } else { try part2(&lines, &left, &right, allocator); @@ -41,7 +41,7 @@ fn part1(lines: *std.mem.SplitIterator(u8, .scalar), left: *std.ArrayList(usize) } } - print("Sum is {d}\n", .{sum}); + print("{d}\n", .{sum}); } fn part2(lines: *std.mem.SplitIterator(u8, .scalar), left: *std.ArrayList(usize), right: *std.ArrayList(usize), allocator: std.mem.Allocator) !void { @@ -77,5 +77,5 @@ fn part2(lines: *std.mem.SplitIterator(u8, .scalar), left: *std.ArrayList(usize) }; } - print("Sum is {d}\n", .{sum}); + print("{d}\n", .{sum}); } diff --git a/src/day02.zig b/src/day02.zig index cad2d9d..e87b234 100644 --- a/src/day02.zig +++ b/src/day02.zig @@ -4,7 +4,7 @@ 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")) { + if (std.mem.eql(u8, part, "1")) { try part1(&lines); } else { try part2(&lines); @@ -38,7 +38,7 @@ fn part1(lines: *std.mem.SplitIterator(u8, .scalar)) !void { } } - print("Safe count: {d}\n", .{count}); + print("{d}\n", .{count}); } fn part2(lines: *std.mem.SplitIterator(u8, .scalar)) !void { @@ -87,5 +87,5 @@ fn part2(lines: *std.mem.SplitIterator(u8, .scalar)) !void { } } } - print("Safe count: {d}\n", .{count}); + print("{d}\n", .{count}); } diff --git a/src/day03.zig b/src/day03.zig new file mode 100644 index 0000000..0a5e27e --- /dev/null +++ b/src/day03.zig @@ -0,0 +1,63 @@ +const std = @import("std"); +const mecha = @import("mecha"); +const print = std.debug.print; + +pub fn solve(part: []u8, buffer: []u8, allocator: std.mem.Allocator) !void { + if (std.mem.eql(u8, part, "1")) { + try part1(buffer, allocator); + } else { + try part2(buffer, allocator); + } +} + +fn part1(buffer: []const u8, allocator: std.mem.Allocator) !void { + const mul = mecha.combine(.{ mecha.string("mul(").discard(), mecha.int(usize, .{}), mecha.ascii.char(',').discard(), mecha.int(usize, .{}), mecha.ascii.char(')').discard() }); + + var sum: usize = 0; + var rest = buffer; + while (rest.len > 0) { + const result = mul.parse(allocator, rest) catch { + rest = rest[1..]; + continue; + }; + sum += result.value[0] * result.value[1]; + rest = result.rest; + } + + print("{d}", .{sum}); +} + +// UGLY +fn part2(buffer: []const u8, allocator: std.mem.Allocator) !void { + const mul = mecha.combine(.{ mecha.string("mul(").discard(), mecha.int(usize, .{}), mecha.ascii.char(',').discard(), mecha.int(usize, .{}), mecha.ascii.char(')').discard() }); + + const dont = mecha.string("don't()"); + const do = mecha.string("do()"); + + var sum: usize = 0; + var rest = buffer; + var disabled = false; + while (rest.len > 0) { + if (disabled) { + _ = do.parse(allocator, rest) catch { + rest = rest[1..]; + continue; + }; + disabled = false; + } else { + const result = mul.parse(allocator, rest) catch { + const result = dont.parse(allocator, rest) catch { + rest = rest[1..]; + continue; + }; + rest = result.rest; + disabled = true; + continue; + }; + sum += result.value[0] * result.value[1]; + rest = result.rest; + } + } + + print("{d}", .{sum}); +} diff --git a/src/day04.zig b/src/day04.zig new file mode 100644 index 0000000..ba6aaf8 --- /dev/null +++ b/src/day04.zig @@ -0,0 +1,136 @@ +const std = @import("std"); +const mecha = @import("mecha"); +const print = std.debug.print; + +pub fn solve(part: []u8, buffer: []u8, allocator: std.mem.Allocator) !void { + if (std.mem.eql(u8, part, "1")) { + try part1(buffer, allocator); + } else { + try part2(buffer, allocator); + } +} + +fn amount(line: []const u8, allocator: std.mem.Allocator) usize { + const match = mecha.oneOf(.{ mecha.string("XMAS"), mecha.string("SAMX") }); + + var rest = line; + var output: usize = 0; + while (rest.len > 0) { + _ = match.parse(allocator, rest) catch { + rest = rest[1..]; + continue; + }; + rest = rest[3..]; + output += 1; + } + return output; +} + +fn diagonal(list: []const []const u8, startr: usize, startc: usize, dirr: i32, dirc: i32, allocator: std.mem.Allocator) !usize { + var line = std.ArrayList(u8).init(allocator); + defer line.deinit(); + + const rows: i32 = @intCast(list.len); + const width: i32 = @intCast(list[0].len); + + var r: i32 = @intCast(startr); + var c: i32 = @intCast(startc); + while (r >= 0 and c >= 0 and r < rows and c < width) { + try line.append(list[@intCast(r)][@intCast(c)]); + + r += dirr; + c += dirc; + } + return amount(line.items, allocator); +} + +fn part1(buffer: []const u8, allocator: std.mem.Allocator) !void { + var lines = std.mem.splitScalar(u8, buffer, '\n'); + + var list = std.ArrayList([]const u8).init(allocator); + defer list.deinit(); + + var sum: usize = 0; + + while (lines.next()) |line| { + if (line.len > 0) { + try list.append(line); + sum += amount(line, allocator); // HORIZONTAL + } + } + + for (0..list.items.len) |r| { + sum += try diagonal(list.items, r, 0, -1, 1, allocator); // UP-RIGHT + sum += try diagonal(list.items, r, 0, 1, 1, allocator); // DOWN-RIGHT + } + + for (1..list.items[0].len) |c| { + sum += try diagonal(list.items, 0, c, 1, 1, allocator); // DOWN-RIGHT + sum += try diagonal(list.items, list.items.len - 1, c, -1, 1, allocator); // UP-RIGHT + } + + for (0..list.items[0].len) |c| { // VERTICAL + sum += try diagonal(list.items, 0, c, 1, 0, allocator); + } + + print("{d}\n", .{sum}); +} + +//------------------------------PART 2--------------------------------------- + +fn diagonal2(list: []const []const u8, startr: usize, startc: usize, dirr: i32, dirc: i32, allocator: std.mem.Allocator, hashmap: anytype) !usize { + var line = std.ArrayList(u8).init(allocator); + defer line.deinit(); + + const rows: i32 = @intCast(list.len); + const width: i32 = @intCast(list[0].len); + var output: usize = 0; + + var r: i32 = @intCast(startr); + var c: i32 = @intCast(startc); + while (r >= 0 and c >= 0 and r < rows and c < width) { + try line.append(list[@intCast(r)][@intCast(c)]); + + if (line.items.len >= 3 and (std.mem.eql(u8, line.items[line.items.len - 3 ..], "MAS") or std.mem.eql(u8, line.items[line.items.len - 3 ..], "SAM"))) { + if (hashmap.contains((r - dirr) * rows + (c - dirc))) { + output += 1; + } else { + try hashmap.put((r - dirr) * rows + (c - dirc), 1); + } + } + + r += dirr; + c += dirc; + } + return output; +} + +fn part2(buffer: []const u8, allocator: std.mem.Allocator) !void { + var map = std.AutoHashMap(i32, usize).init(allocator); + defer map.deinit(); + + var lines = std.mem.splitScalar(u8, buffer, '\n'); + + var list = std.ArrayList([]const u8).init(allocator); + defer list.deinit(); + + var sum: usize = 0; + + while (lines.next()) |line| { + if (line.len > 0) { + try list.append(line); + } + } + + for (0..list.items.len) |r| { + sum += try diagonal2(list.items, r, 0, -1, 1, allocator, &map); // UP-RIGHT + sum += try diagonal2(list.items, r, 0, 1, 1, allocator, &map); // DOWN-RIGHT + } + + for (1..list.items[0].len) |c| { + sum += try diagonal2(list.items, 0, c, 1, 1, allocator, &map); // DOWN-RIGHT + sum += try diagonal2(list.items, list.items.len - 1, c, -1, 1, allocator, &map); // UP-RIGHT + } + + print("{d}\n", .{sum}); +} -- cgit v1.2.3