summaryrefslogtreecommitdiff
path: root/day_11.zig
blob: dc6819090a68737c94e189eb6954e9407fcec0f6 (plain) (blame)
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
const std = @import("std");
const Result = @import("util/aoc.zig").Result;

const Operation = struct {
    symbol: u8,
    fixed: bool = false,
    fixed_number: u8 = 0,

    fn exe(self: Operation, b: u64) u64 {
        if (self.symbol == '*') {
            switch (self.fixed) {
                true => return self.fixed_number * b,
                false => return b * b,
            }
        }

        switch (self.fixed) {
            true => return self.fixed_number + b,
            false => return b + b,
        }
    }
};

const Monkey = struct {
    const Throw_rules = struct {
        multiple_of: u8,
        true: u8,
        false: u8,
    };

    id: u8,
    inspect_count: usize = 0,
    items: std.ArrayList(u64),
    operation: Operation,
    throw_rules: Throw_rules,

    fn throws_to(self: Monkey, worry_level: u64) usize {
        if (worry_level % self.throw_rules.multiple_of == 0) return self.throw_rules.true;
        return self.throw_rules.false;
    }

    fn inspect(self: *Monkey) u64 {
        self.inspect_count += 1;
        return self.operation.exe(self.items.orderedRemove(0));
    }
};

fn answer(monkeys: []Monkey) u64 {
    var max: [2]u64 = std.mem.zeroes([2]u64);
    for (monkeys) |*monkey| {
        if (monkey.inspect_count > max[0] and monkey.inspect_count > max[1]) {
            max[1] = max[0];
            max[0] = monkey.inspect_count;
        } else if (monkey.inspect_count > max[1]) {
            max[1] = monkey.inspect_count;
        }

        monkey.items.deinit();
    }
    return max[0] * max[1];
}

fn parse(allocator: std.mem.Allocator, input: []const u8) []Monkey {
    var monkeys = std.ArrayList(Monkey).init(allocator);

    var monkey_iter = std.mem.split(u8, input, "\n\n");
    while (monkey_iter.next()) |monkey_data| {
        var monkey = std.mem.split(u8, monkey_data, "\n");

        var line = monkey.next().?;
        var id = std.fmt.parseInt(u8, line[7 .. line.len - 1], 0) catch unreachable;

        var items = std.ArrayList(u64).init(allocator);
        line = monkey.next().?;
        var items_iter = std.mem.split(u8, line[18..], ", ");
        while (items_iter.next()) |item| {
            items.append(std.fmt.parseInt(u64, item, 0) catch unreachable) catch unreachable;
        }

        const operation = blk: {
            line = monkey.next().?;
            var op = Operation{ .symbol = line[23] };

            if (line[25] != 'o') {
                op.fixed = true;
                op.fixed_number = std.fmt.parseInt(u8, line[25..], 0) catch unreachable;
            }

            break :blk op;
        };

        const throw_rules = blk: {
            line = monkey.next().?;
            const multiple_of = std.fmt.parseInt(u8, line[21..], 0) catch unreachable;

            line = monkey.next().?;
            const iftrue = std.fmt.parseInt(u8, line[29..], 0) catch unreachable;

            line = monkey.next().?;
            const iffalse = std.fmt.parseInt(u8, line[30..], 0) catch unreachable;

            break :blk Monkey.Throw_rules{
                .multiple_of = multiple_of,
                .true = iftrue,
                .false = iffalse,
            };
        };

        monkeys.append(Monkey{
            .id = id,
            .items = items,
            .operation = operation,
            .throw_rules = throw_rules,
        }) catch unreachable;
    }

    return monkeys.toOwnedSlice() catch unreachable;
}

pub fn puzzle_1(allocator: std.mem.Allocator, input: []const u8) Result {
    var monkeys = parse(allocator, input);
    defer allocator.free(monkeys);

    var i: usize = 0;
    while (i < 20) : (i += 1) {
        for (monkeys) |*monkey| {
            while (monkey.items.items.len > 0) {
                const new_worry_level = monkey.inspect();
                const throw_to = monkey.throws_to(new_worry_level / 3);
                monkeys[throw_to].items.append(new_worry_level / 3) catch unreachable;
            }
        }
    }

    return .{ .int = @intCast(i32, answer(monkeys)) };
}

pub fn puzzle_2(allocator: std.mem.Allocator, input: []const u8) Result {
    var monkeys = parse(allocator, input);
    defer allocator.free(monkeys);

    const mod = blk: {
        var r: u64 = 1;
        for (monkeys) |monkey| r *= monkey.throw_rules.multiple_of;
        break :blk r;
    };

    var i: usize = 0;
    while (i < 10000) : (i += 1) {
        for (monkeys) |*monkey| {
            while (monkey.items.items.len > 0) {
                const new_worry_level = monkey.inspect();
                const throw_to = monkey.throws_to(new_worry_level % mod);
                monkeys[throw_to].items.append(new_worry_level % mod) catch unreachable;
            }
        }
    }

    return .{ .int = @intCast(i64, answer(monkeys)) };
}