2024 Day 4 Part 2
This commit is contained in:
parent
921b0c3d70
commit
ecabd6e1b1
1 changed files with 45 additions and 0 deletions
|
|
@ -21,6 +21,9 @@ pub fn main() !void {
|
||||||
|
|
||||||
try stdout.print("{d}\n", .{try part1(allocator, text)});
|
try stdout.print("{d}\n", .{try part1(allocator, text)});
|
||||||
try bw.flush();
|
try bw.flush();
|
||||||
|
|
||||||
|
try stdout.print("{d}\n", .{try part2(allocator, text)});
|
||||||
|
try bw.flush();
|
||||||
}
|
}
|
||||||
|
|
||||||
const Coord = struct {
|
const Coord = struct {
|
||||||
|
|
@ -100,3 +103,45 @@ fn part1(allocator: Allocator, text: []u8) !usize {
|
||||||
|
|
||||||
return count;
|
return count;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn part2(allocator: Allocator, text: []u8) !usize {
|
||||||
|
var grid = std.AutoHashMap(Coord, u8).init(allocator);
|
||||||
|
defer grid.deinit();
|
||||||
|
|
||||||
|
{
|
||||||
|
var it = std.mem.tokenizeScalar(u8, text, '\n');
|
||||||
|
var r: usize = 0;
|
||||||
|
while (it.next()) |row| : (r += 1) {
|
||||||
|
for (row, 0..) |char, c| {
|
||||||
|
try grid.put(.{ .r = @intCast(r), .c = @intCast(c) }, char);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var count: usize = 0;
|
||||||
|
var it = grid.iterator();
|
||||||
|
while (it.next()) |entry| {
|
||||||
|
const v = entry.value_ptr.*;
|
||||||
|
if (v != 'A') {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
var a = entry.key_ptr.*;
|
||||||
|
|
||||||
|
const nw = grid.get(a.move(Direction.NW)) orelse continue;
|
||||||
|
const se = grid.get(a.move(Direction.SE)) orelse continue;
|
||||||
|
|
||||||
|
const sw = grid.get(a.move(Direction.SW)) orelse continue;
|
||||||
|
const ne = grid.get(a.move(Direction.NE)) orelse continue;
|
||||||
|
|
||||||
|
if (is_mas(nw, se) and is_mas(sw, ne)) {
|
||||||
|
count += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn is_mas(x: u8, y: u8) bool {
|
||||||
|
return (x == 'M' and y == 'S') or (y == 'M' and x == 'S');
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue