-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path22.zig
231 lines (200 loc) · 7.33 KB
/
22.zig
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
const std = @import("std");
pub fn main() !void {
var sim = try Simulation.parse(@embedFile("22.txt"), std.heap.page_allocator);
try sim.simulate();
var count: usize = 0;
for (0..sim.settled.items.len) |i| {
const sub_count = try sim.count_falling(i, std.heap.page_allocator);
// std.debug.print("{c} {d}\n", .{ @as(u8, @intCast(i)) + 'A', sub_count });
count += sub_count;
}
// for (sim.settled.items) |b| {
// b.print();
// }
std.debug.print("{d}\n", .{count});
// var it = sim.above.iterator();
// while (it.next()) |aboves| {
// for (aboves.value_ptr.*.items) |above| {
// std.debug.print("{d} is above {d}\n", .{ above, aboves.key_ptr.* });
// }
// }
// it = sim.below.iterator();
// while (it.next()) |belows| {
// for (belows.value_ptr.*.items) |below| {
// std.debug.print("{d} is above {d}\n", .{ belows.key_ptr.*, below });
// }
// }
}
const Simulation = struct {
falling: std.ArrayList(Block),
settled: std.ArrayList(Block),
above: std.AutoHashMap(usize, std.ArrayList(usize)),
below: std.AutoHashMap(usize, std.ArrayList(usize)),
pub fn parse(input: []const u8, allocator: std.mem.Allocator) !@This() {
var falling = std.ArrayList(Block).init(allocator);
var lines = std.mem.splitScalar(u8, input, '\n');
while (lines.next()) |line| {
if (line.len > 0) {
try falling.append(try Block.parse(line));
}
}
return @This(){
.falling = falling,
.settled = std.ArrayList(Block).init(allocator),
.above = std.AutoHashMap(usize, std.ArrayList(usize)).init(allocator),
.below = std.AutoHashMap(usize, std.ArrayList(usize)).init(allocator),
};
}
pub fn deinit(self: *@This()) void {
self.falling.deinit();
self.settled.deinit();
}
fn less_than_fn(_: void, lhs: Block, rhs: Block) bool {
return lhs.z.min > rhs.z.min;
}
fn sort_blocks(self: *@This()) void {
std.mem.sort(Block, self.falling.items, {}, less_than_fn);
}
fn pop_lowest(self: *@This()) ?Block {
return self.falling.popOrNull();
}
pub fn settle(self: *@This(), block: Block) !void {
const i = self.settled.items.len;
try self.below.put(i, std.ArrayList(usize).init(self.settled.allocator));
try self.above.put(i, std.ArrayList(usize).init(self.settled.allocator));
var height: usize = 0;
for (self.settled.items) |*stationary_block| {
if (block.xy_intersect(stationary_block.*)) {
height = @max(height, stationary_block.z.max);
}
}
for (self.settled.items, 0..) |*stationary_block, j| {
if (block.xy_intersect(stationary_block.*) and stationary_block.z.max == height) {
try self.below.getPtr(i).?.append(j);
try self.above.getPtr(j).?.append(i);
}
}
var block_ = block;
block_.fall(height + 1);
try self.settled.append(block_);
}
pub fn simulate(self: *@This()) !void {
self.sort_blocks();
while (self.pop_lowest()) |lowest| {
try self.settle(lowest);
}
std.debug.assert(self.falling.items.len == 0);
}
pub fn has_support(self: @This(), index: usize, ignore: []usize) bool {
for (self.below.get(index).?.items) |below| {
if (std.mem.count(usize, ignore, &.{below}) == 0) {
return true;
}
}
return false;
}
pub fn count_falling(self: @This(), block_to_disintegrate: usize, allocator: std.mem.Allocator) !usize {
var falling = std.ArrayList(usize).init(allocator);
try falling.append(block_to_disintegrate);
var count: usize = 0;
var queue = Queue.new(self.above.get(block_to_disintegrate).?.items, allocator);
while (queue.pop()) |i| {
if (!self.has_support(i, falling.items)) {
try falling.append(i);
count += 1;
try queue.delayed_push_slice(self.above.get(i).?.items);
}
try queue.apply_delayed();
}
return count;
}
};
const Queue = struct {
current: std.ArrayList(usize),
next: std.ArrayList(usize),
pub fn new(slice: []usize, allocator: std.mem.Allocator) @This() {
return @This(){
.current = std.ArrayList(usize).fromOwnedSlice(allocator, slice),
.next = std.ArrayList(usize).init(allocator),
};
}
pub fn pop(self: *@This()) ?usize {
return self.current.popOrNull();
}
pub fn delayed_push(self: *@This(), item: usize) !void {
try self.next.append(item);
}
pub fn delayed_push_slice(self: *@This(), slice: []usize) !void {
try self.next.appendSlice(slice);
}
pub fn apply_delayed(self: *@This()) !void {
try self.current.appendSlice(self.next.items);
self.next.clearAndFree();
}
};
const Block = struct {
x: Range,
y: Range,
z: Range,
pub fn parse(line: []const u8) !@This() {
var lr_split = std.mem.splitScalar(u8, line, '~');
var min_iter = std.mem.splitScalar(u8, lr_split.next().?, ',');
var max_iter = std.mem.splitScalar(u8, lr_split.next().?, ',');
const minx = try std.fmt.parseInt(usize, min_iter.next().?, 10);
const miny = try std.fmt.parseInt(usize, min_iter.next().?, 10);
const minz = try std.fmt.parseInt(usize, min_iter.next().?, 10);
const maxx = try std.fmt.parseInt(usize, max_iter.next().?, 10);
const maxy = try std.fmt.parseInt(usize, max_iter.next().?, 10);
const maxz = try std.fmt.parseInt(usize, max_iter.next().?, 10);
std.debug.assert(minx <= maxx);
std.debug.assert(miny <= maxy);
std.debug.assert(minz <= maxz);
return @This(){
.x = range(minx, maxx),
.y = range(miny, maxy),
.z = range(minz, maxz),
};
}
pub fn xy_intersect(self: @This(), other: @This()) bool {
return self.x.overlaping(other.x) and self.y.overlaping(other.y);
}
pub fn fall(self: *@This(), minz: usize) void {
const delta = self.z.min - minz;
self.z.sub(delta);
}
pub fn print(self: @This()) void {
std.debug.print("x:", .{});
self.x.print();
std.debug.print(" y:", .{});
self.y.print();
std.debug.print(" z:", .{});
self.z.print();
std.debug.print("\n", .{});
}
};
pub fn range(min: usize, max: usize) Range {
std.debug.assert(min <= max);
return Range{ .min = min, .max = max };
}
const Range = struct {
min: usize,
max: usize,
pub fn overlaping(self: @This(), other: @This()) bool {
return self.has(other.min) or self.has(other.max) or
other.has(self.min) or other.has(self.max);
}
pub fn has(self: @This(), num: usize) bool {
return num >= self.min and num <= self.max;
}
pub fn sub(self: *@This(), delta: usize) void {
self.max -= delta;
self.min -= delta;
}
pub fn print(self: @This()) void {
if (self.min == self.max) {
std.debug.print("{d}", .{self.min});
} else {
std.debug.print("{d}~{d}", .{ self.min, self.max });
}
}
};