-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday06pt2.zig
160 lines (135 loc) · 5.21 KB
/
day06pt2.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
const std = @import("std");
const input = @embedFile("input06.txt");
const w = std.mem.indexOfScalar(u8, input, '\n').?;
const h = input.len / (w + 1);
const Vec2 = struct {
x: isize,
y: isize,
const Self = @This();
pub inline fn eql(self: Self, other: Self) bool {
return self.x == other.x and self.y == other.y;
}
pub inline fn add(self: Self, other: Self) Self {
return .{
.x = self.x + other.x,
.y = self.y + other.y,
};
}
pub inline fn rotate_right(self: Self) Self {
return .{ .x = -self.y, .y = self.x };
}
pub inline fn is_in_bounds(self: Self) bool {
return self.x >= 0 and self.x < w and self.y >= 0 and self.y < h;
}
pub inline fn is_blocked(self: Self) bool {
if (!self.is_in_bounds()) return false;
const idx = @as(usize, @intCast(self.y)) * (w + 1) + @as(usize, @intCast(self.x));
return input[idx] == '#';
}
};
const StepsTaken = struct {
up: std.DynamicBitSetUnmanaged,
down: std.DynamicBitSetUnmanaged,
left: std.DynamicBitSetUnmanaged,
right: std.DynamicBitSetUnmanaged,
const Self = @This();
pub fn init(alloc: *const std.mem.Allocator) !Self {
var up = try std.DynamicBitSetUnmanaged.initEmpty(alloc.*, w * h);
errdefer up.deinit(alloc.*);
var down = try std.DynamicBitSetUnmanaged.initEmpty(alloc.*, w * h);
errdefer down.deinit(alloc.*);
var left = try std.DynamicBitSetUnmanaged.initEmpty(alloc.*, w * h);
errdefer left.deinit(alloc.*);
const right = try std.DynamicBitSetUnmanaged.initEmpty(alloc.*, w * h);
errdefer up.deinit(alloc.*);
return .{ .up = up, .down = down, .left = left, .right = right };
}
pub fn deinit(self: *Self, alloc: *const std.mem.Allocator) void {
self.up.deinit(alloc.*);
self.down.deinit(alloc.*);
self.left.deinit(alloc.*);
self.right.deinit(alloc.*);
}
pub fn clear(self: *Self) void {
self.up.unsetAll();
self.down.unsetAll();
self.left.unsetAll();
self.right.unsetAll();
}
inline fn index(pos: Vec2) !usize {
if (!pos.is_in_bounds()) return error.OutOfBounds;
return @as(usize, @intCast(pos.y)) * w + @as(usize, @intCast(pos.x));
}
pub fn is_in_loop(self: Self, pos: Vec2, delta: Vec2) !bool {
const dir_steps = if (delta.y == -1) self.up
else if (delta.y == 1) self.down
else if (delta.x == -1) self.left
else if (delta.x == 1) self.right
else return error.BadDelta;
return dir_steps.isSet(try Self.index(pos));
}
pub fn mark_step(self: *Self, pos: Vec2, delta: Vec2) !void {
var dir_steps = if (delta.y == -1) self.up
else if (delta.y == 1) self.down
else if (delta.x == -1) self.left
else if (delta.x == 1) self.right
else return error.BadDelta;
dir_steps.set(try Self.index(pos));
}
};
fn guard_walk_loops(steps_taken: *StepsTaken, pos: Vec2, dir: Vec2, obs: Vec2) !bool {
var gpos = pos;
var gdelta = dir;
steps_taken.clear();
while (gpos.is_in_bounds()) {
if (try steps_taken.is_in_loop(gpos, gdelta)) return true;
try steps_taken.mark_step(gpos, gdelta);
var forward = gpos.add(gdelta);
while (forward.is_blocked() or forward.eql(obs)) {
gdelta = gdelta.rotate_right();
forward = gpos.add(gdelta);
}
gpos = gpos.add(gdelta);
}
return false;
}
pub fn main() !void {
std.debug.assert(input.len % (w + 1) == 0);
var gpa = std.heap.GeneralPurposeAllocator(.{}).init;
defer std.debug.assert(gpa.deinit() == .ok);
const alloc = gpa.allocator();
var obs_places = std.AutoHashMapUnmanaged(Vec2, struct { pos: Vec2, dir: Vec2 }).empty;
defer obs_places.deinit(alloc);
const gpos_raw = std.mem.indexOfScalar(u8, input, '^') orelse return error.GuardNotFound;
const gpos_init: Vec2 = .{
.x = @intCast(gpos_raw % (w + 1)),
.y = @intCast(@divFloor(gpos_raw, w + 1)),
};
var gpos = gpos_init;
var gdelta: Vec2 = .{ .x = 0, .y = -1 };
// Find potential places to put an obstruction.
while (gpos.is_in_bounds()) {
const front = gpos.add(gdelta);
const right = gpos.add(gdelta.rotate_right());
if (!front.is_blocked()) {
_ = try obs_places.getOrPutValue(alloc, front, .{ .pos = gpos, .dir = gdelta });
} else if (!right.is_blocked()) {
_ = try obs_places.getOrPutValue(alloc, right, .{ .pos = gpos, .dir = gdelta });
}
while (gpos.add(gdelta).is_blocked()) {
gdelta = gdelta.rotate_right();
}
gpos = gpos.add(gdelta);
}
var steps_taken = try StepsTaken.init(&alloc);
defer steps_taken.deinit(&alloc);
var good_obs_places: usize = 0;
var obs_places_iter = obs_places.iterator();
while (obs_places_iter.next()) |entry| {
const obs = entry.key_ptr.*;
const pos = entry.value_ptr.pos;
const dir = entry.value_ptr.dir;
good_obs_places += @intFromBool(try guard_walk_loops(&steps_taken, pos, dir, obs));
}
std.debug.print("{d}\n", .{ good_obs_places });
}