summaryrefslogtreecommitdiff
path: root/src/day04.zig
blob: ba6aaf8f5390723228f162df9392127ffa4739ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
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});
}