-
Notifications
You must be signed in to change notification settings - Fork 0
/
day6.zig
171 lines (147 loc) · 5.15 KB
/
day6.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
const std = @import("std");
const data = @embedFile("input6.txt");
const split = std.mem.split;
const allocator = std.heap.page_allocator;
const maxLines: usize = 130;
const maxLineLength: usize = 130;
const stdout = std.io.getStdOut().writer();
fn contains(list: std.ArrayList(Index), value: Index) bool {
for (list.items) |item| {
if (item.x == value.x and item.y == value.y) {
return true;
}
}
return false;
}
const Vec2 = struct {
x: isize,
y: isize,
fn rotate(self: *Vec2) void {
const tmp = self.x;
self.x = -self.y;
self.y = tmp;
}
};
const Index = struct {
x: usize,
y: usize,
fn step(self: *Index, dir: Vec2) !void {
const x_curr: isize = @intCast(self.x);
const y_curr: isize = @intCast(self.y);
const x: isize = x_curr + dir.x;
const y: isize = y_curr + dir.y;
if (x < 0 or y < 0 or x >= maxLineLength or y >= maxLines) {
return error.NegativeOrZeroResult;
}
self.x = @intCast(x);
self.y = @intCast(y);
}
fn next(self: *Index, dir: Vec2) !Index {
const x_curr: isize = @intCast(self.x);
const y_curr: isize = @intCast(self.y);
const x: isize = x_curr + dir.x;
const y: isize = y_curr + dir.y;
if (x < 0 or y < 0 or x >= maxLineLength or y >= maxLines) {
return error.NegativeOrZeroResult;
}
return Index{
.x = @intCast(x),
.y = @intCast(y),
};
}
};
pub fn main() !void {
var splits = split(u8, data, "\n");
var charGrid: [maxLines][maxLineLength]u8 = undefined;
var lineIndex: usize = 0;
while (splits.next()) |line| {
if (lineIndex >= maxLines) {
break;
}
// Copy the line into the 2D array
var charIndex: usize = 0;
while (charIndex < line.len) : (charIndex += 1) {
charGrid[lineIndex][charIndex] = line[charIndex];
}
// Fill the remaining part of the row with zeros
while (charIndex < maxLineLength) : (charIndex += 1) {
charGrid[lineIndex][charIndex] = 0;
}
lineIndex += 1;
}
var starting_direction = Vec2{ .x = 0, .y = 0 };
var starting_position = Index{ .x = 0, .y = 0 };
outer: for (charGrid, 0..) |line, index_y| {
for (line, 0..) |c, index_x| {
if (c == '^') {
starting_direction.y = -1;
starting_position.x = index_x;
starting_position.y = index_y;
break :outer;
}
}
}
var count: i64 = 0;
var visited = std.ArrayList(Index).init(allocator);
var visitedDirection = std.ArrayList(Vec2).init(allocator);
var direction = starting_direction;
var position = starting_position;
const unchangedGrid = charGrid;
while (position.x > 0 and position.y > 0 and position.x < maxLineLength and position.y < maxLines) {
if (charGrid[position.y][position.x] != 'X') {
try visited.append(position);
try visitedDirection.append(direction);
count = count + 1;
charGrid[position.y][position.x] = 'X';
}
const new_pos = position.next(direction) catch {
break;
};
if (charGrid[new_pos.y][new_pos.x] == '#') {
direction.rotate();
} else {
try position.step(direction);
}
}
std.debug.print("{d}", .{count});
var visitedWhileWalking = std.ArrayList(Index).init(allocator);
var visitedDirectionWhileWalking = std.ArrayList(Vec2).init(allocator);
var endless_loop: u32 = 0;
charGrid = unchangedGrid;
for (visited.items) |value| {
position = starting_position;
direction = starting_direction;
charGrid[value.y][value.x] = '#';
while (position.x > 0 and position.y > 0 and position.x < maxLineLength and position.y < maxLines) {
if (contains(visitedWhileWalking, position)) {
const item_index = for (visitedWhileWalking.items, 0..) |pos, index| {
if (std.meta.eql(pos, position)) break index;
} else null;
if (std.meta.eql(visitedDirectionWhileWalking.items[item_index.?], direction)) {
endless_loop += 1;
break;
}
} else {
try visitedWhileWalking.append(position);
try visitedDirectionWhileWalking.append(direction);
charGrid[position.y][position.x] = 'X';
}
const new_pos = position.next(direction) catch {
break;
};
if (charGrid[new_pos.y][new_pos.x] == '#') {
direction.rotate();
} else {
try position.step(direction);
}
}
charGrid[value.y][value.x] = '.';
visitedWhileWalking.clearRetainingCapacity();
visitedDirectionWhileWalking.clearRetainingCapacity();
}
std.debug.print("\n \n {d}", .{endless_loop});
visitedWhileWalking.deinit();
visitedDirection.deinit();
visited.deinit();
visitedDirectionWhileWalking.deinit();
}