aboutsummaryrefslogtreecommitdiffstats
path: root/src/11/main.zig
blob: 857c4576ab434660ebbfc67100b974124399d3e7 (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
161
162
163
164
165
166
167
168
169
const std = @import("std");
const aoc = @import("aoc");

const SeatState = enum { EMPTY, FLOOR, TAKEN };
const MapDims = struct { width: usize, height: usize };
const Dir = struct { x: i8, y: i8 };
const Pos = struct { x: i128, y: i128 };

const adjacent = [_]Dir{
    Dir{ .x = -1, .y = -1 },
    Dir{ .x = -0, .y = -1 },
    Dir{ .x = 1, .y = -1 },
    Dir{ .x = -1, .y = 0 },
    Dir{ .x = 1, .y = 0 },
    Dir{ .x = -1, .y = 1 },
    Dir{ .x = 0, .y = 1 },
    Dir{ .x = 1, .y = 1 },
};

fn parseMap(mapitems: *[]SeatState, dims: *MapDims, input: []const u8, allocator: std.mem.Allocator) !void {
    var lineit = std.mem.tokenize(u8, input, "\n");
    var map = std.ArrayList(SeatState).init(allocator);
    errdefer map.deinit();
    while (lineit.next()) |line| {
        if (dims.width == 0) {
            dims.width = line.len;
        } else if (dims.width != line.len) {
            return aoc.Error.InvalidInput;
        }
        for (line) |c| {
            try map.append(switch (c) {
                '#' => SeatState.TAKEN,
                'L' => SeatState.EMPTY,
                '.' => SeatState.FLOOR,
                else => {
                    return aoc.Error.InvalidInput;
                },
            });
        }
        dims.height += 1;
    }
    mapitems.* = map.toOwnedSlice();
}

fn printMap(mapitems: []SeatState, dims: MapDims) void {
    for (mapitems) |state, i| {
        const c: u8 = switch (state) {
            SeatState.EMPTY => 'L',
            SeatState.TAKEN => '#',
            SeatState.FLOOR => '.',
        };
        if (i % dims.width == 0) {
            aoc.debugfmt("\n", .{});
        }
        aoc.debugfmt("{c}", .{c});
    }
}

fn simulateStrategyOne(before: []SeatState, after: []SeatState, dims: MapDims) u32 {
    var seat_changes: u32 = 0;
    for (before) |state, i| {
        const p = Pos{ .x = i % dims.width, .y = i / dims.width };
        var count: u32 = 0;
        for (adjacent) |ap| {
            if (p.x + ap.x >= dims.width or p.x + ap.x < 0 or p.y + ap.y >= dims.height or p.y + ap.y < 0) continue;
            const ni = @intCast(u64, @intCast(i64, i) + ap.x + ap.y * @intCast(i64, dims.width));
            count += @boolToInt(before[ni] == SeatState.TAKEN);
        }
        if (state == SeatState.EMPTY and count == 0) {
            after[i] = SeatState.TAKEN;
            seat_changes += 1;
        } else if (state == SeatState.TAKEN and count >= 4) {
            after[i] = SeatState.EMPTY;
            seat_changes += 1;
        } else {
            after[i] = before[i];
        }
    }
    return seat_changes;
}

fn simulateStrategyTwo(before: []SeatState, after: []SeatState, dims: MapDims) u32 {
    var seat_changes: u32 = 0;
    for (before) |state, i| {
        const p = Pos{ .x = i % dims.width, .y = i / dims.width };
        var count: u32 = 0;
        for (adjacent) |ap| {
            var iterp = Pos{ .x = p.x + ap.x, .y = p.y + ap.y };
            while (iterp.x < dims.width and iterp.x >= 0 and iterp.y < dims.height and iterp.y >= 0) {
                const ni = @intCast(u64, iterp.x + iterp.y * @intCast(i64, dims.width));
                if (before[ni] == SeatState.TAKEN) {
                    count += 1;
                    break;
                } else if (before[ni] == SeatState.EMPTY) {
                    break;
                } else {
                    iterp.x += ap.x;
                    iterp.y += ap.y;
                }
            }
        }
        if (state == SeatState.EMPTY and count == 0) {
            after[i] = SeatState.TAKEN;
            seat_changes += 1;
        } else if (state == SeatState.TAKEN and count >= 5) {
            after[i] = SeatState.EMPTY;
            seat_changes += 1;
        } else {
            after[i] = before[i];
        }
    }
    return seat_changes;
}

fn part1(allocator: std.mem.Allocator, input: []u8, args: [][]u8) !?[]u8 {
    _ = args;

    var mapdims = MapDims{ .width = 0, .height = 0 };
    var mapitems: []SeatState = undefined;
    try parseMap(&mapitems, &mapdims, input, allocator);
    defer allocator.free(mapitems);

    var mapresult = try allocator.alloc(SeatState, mapitems.len);
    defer allocator.free(mapresult);

    var round: u32 = 0;
    while (simulateStrategyOne(mapitems, mapresult, mapdims) > 0) : (round += 1) {
        aoc.debugfmt("\rRound: {}", .{round});
        std.mem.copy(SeatState, mapitems, mapresult);
    }

    var taken_count: u64 = 0;
    for (mapresult) |state| {
        taken_count += @boolToInt(state == SeatState.TAKEN);
    }

    aoc.debugfmt("\nSeats Occupied: {}\n", .{taken_count});

    return try std.fmt.allocPrint(allocator, "{d}", .{taken_count});
}

fn part2(allocator: std.mem.Allocator, input: []u8, args: [][]u8) !?[]u8 {
    _ = args;

    var mapdims = MapDims{ .width = 0, .height = 0 };
    var mapitems: []SeatState = undefined;
    try parseMap(&mapitems, &mapdims, input, allocator);
    defer allocator.free(mapitems);

    var mapresult = try allocator.alloc(SeatState, mapitems.len);
    defer allocator.free(mapresult);

    var round: u32 = 0;
    while (simulateStrategyTwo(mapitems, mapresult, mapdims) > 0) : (round += 1) {
        aoc.debugfmt("\rRound: {}", .{round});
        std.mem.copy(SeatState, mapitems, mapresult);
    }

    var taken_count: u64 = 0;
    for (mapresult) |state| {
        taken_count += @boolToInt(state == SeatState.TAKEN);
    }

    aoc.debugfmt("\nSeats Occupied: {}\n", .{taken_count});

    return try std.fmt.allocPrint(allocator, "{d}", .{taken_count});
}

pub const main = aoc.main(part1, part2, .{ "2126", "1914" });