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
|
const std = @import("std");
const Pos = struct { x: usize, y: usize };
const Dir = enum { left, right, up, down };
fn step(pos: Pos, dir: Dir) Pos {
return switch (dir) {
.left => .{ .x = pos.x -% 1, .y = pos.y },
.right => .{ .x = pos.x + 1, .y = pos.y },
.up => .{ .x = pos.x, .y = pos.y -% 1 },
.down => .{ .x = pos.x, .y = pos.y + 1 },
};
}
fn invalid(pos: Pos, input: Input) bool {
return pos.x >= input.width or pos.y >= input.height;
}
const Input = struct {
plants: []u8,
width: usize,
height: usize,
};
t:
\\ -
\\ RRRR|
\\|RRRR
\\ RRR|
\\ R -
\\ -
,
const test_input =
\\RRRRIICCFF
\\RRRRIICCCF
\\VVRRRCCFFF
\\VVRCCCJFFF
\\VVVVCJJCFE
\\VVIVCCJJEE
\\VVIIICJJEE
\\MIIIIIJJEE
\\MIIISIJEEE
\\MMMISSJEEE
\\
;
pub fn parse(allocator: std.mem.Allocator, data: []const u8) !Input {
var it = std.mem.split(u8, data, "\n");
var plants = std.ArrayList(u8).init(allocator);
var width: usize = undefined;
var height: usize = 0;
while (it.next()) |line| {
if (line.len == 0) break;
width = line.len;
height += 1;
try plants.appendSlice(line);
}
return .{ .plants = plants.items, .width = width, .height = height };
}
test "part1" {
var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
defer arena.deinit();
const output = try part1(arena.allocator(), try parse(arena.allocator(), test_input));
try std.testing.expectEqual(1930, output);
}
pub fn part1(allocator: std.mem.Allocator, input: Input) !u32 {
var visited = try allocator.alloc(bool, input.plants.len);
var stack = std.ArrayList(Pos).init(allocator);
var cost: u32 = 0;
for (0..input.height) |start_y| for (0..input.width) |start_x| {
if (visited[start_y * input.width + start_x]) continue;
stack.clearRetainingCapacity();
try stack.append(.{ .x = start_x, .y = start_y });
visited[start_y * input.width + start_x] = true;
const plant = input.plants[start_y * input.width + start_x];
var area: u32 = 1;
var perimeter: u32 = 0;
while (stack.popOrNull()) |pos| {
// std.debug.print("considering {}, {}\n", .{ pos.x, pos.y });
for ([4]Pos{
.{ .x = pos.x -% 1, .y = pos.y },
.{ .x = pos.x + 1, .y = pos.y },
.{ .x = pos.x, .y = pos.y -% 1 },
.{ .x = pos.x, .y = pos.y + 1 },
}) |n| {
if (n.x >= input.width or n.y >= input.height or input.plants[n.y * input.width + n.x] != plant) {
// std.debug.print("1 perimeter from {}, {} towards {}, {}\n", .{ pos.x, pos.y, @as(i64, @bitCast(n.x)), @as(i64, @bitCast(n.y)) });
perimeter += 1;
continue;
}
if (visited[n.y * input.width + n.x]) continue;
try stack.append(n);
visited[n.y * input.width + n.x] = true;
area += 1;
}
}
// std.debug.print("A region of {c} plants with price {} * {}\n", .{ plant, area, perimeter });
cost += area * perimeter;
};
return cost;
}
test "part2" {
var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
defer arena.deinit();
const output = try part2(arena.allocator(), try parse(arena.allocator(), test_input));
std.debug.print("got {}\n", .{output});
try std.testing.expectEqual(1206, output);
}
pub fn part2(allocator: std.mem.Allocator, input: Input) !u32 {
var visited = try allocator.alloc(bool, input.plants.len);
var stack = std.ArrayList(Pos).init(allocator);
var cost: u32 = 0;
for (0..input.height) |start_y| for (0..input.width) |start_x| {
if (visited[start_y * input.width + start_x]) continue;
stack.clearRetainingCapacity();
try stack.append(.{ .x = start_x, .y = start_y });
visited[start_y * input.width + start_x] = true;
const plant = input.plants[start_y * input.width + start_x];
var area: u32 = 1;
var sides: u32 = 0;
while (stack.popOrNull()) |pos| {
// std.debug.print("considering {}, {}\n", .{ pos.x, pos.y });
for (std.meta.tags(Dir)) |dir| {
const n = step(pos, dir);
if (invalid(n, input) or input.plants[n.y * input.width + n.x] != plant) {
const side: Dir = switch (dir) {
.left => .down,
.down => .right,
.right => .up,
.up => .left,
};
const s = step(pos, side);
const is_leftmost = invalid(s, input) or
input.plants[s.y * input.width + s.x] != plant or
blk: {
const x = step(s, dir);
break :blk !invalid(x, input) and input.plants[x.y * input.width + x.x] == plant;
};
if (is_leftmost) {
// std.debug.print("1 side from {}, {} towards {}, {}\n", .{ pos.x, pos.y, @as(i64, @bitCast(n.x)), @as(i64, @bitCast(n.y)) });
sides += 1;
}
continue;
}
if (visited[n.y * input.width + n.x]) continue;
try stack.append(n);
visited[n.y * input.width + n.x] = true;
area += 1;
}
}
// std.debug.print("A region of {c} plants with price {} * {}\n", .{ plant, area, sides });
cost += area * sides;
};
return cost;
}
|