-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day20.cs
206 lines (176 loc) · 7.23 KB
/
Day20.cs
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
using System;
using System.Collections.Generic;
using System.IO;
class Day20 {
public static int Solution1() => new Grid("input20.txt").Path();
public static int Solution2() => new RecursiveGrid("input20.txt").Path();
class Grid {
readonly bool[,] grid;
readonly Dictionary<Vector2, Vector2> portals;
readonly Vector2 start, end;
public Grid(string fileName) {
portals = new Dictionary<Vector2, Vector2>();
Dictionary<string, Vector2> portalEnds = new Dictionary<string, Vector2>();
string[] lines = File.ReadAllLines(fileName);
grid = new bool[lines.Length - 4, lines[0].Length - 4];
for (int y = 0; y < lines.Length - 1; y++) {
for (int x = 0; x < lines[y].Length - 1; x++) {
char c = lines[y][x];
if (c == '.') {
grid[y - 2, x - 2] = true;
} else if (c >= 'A' && c <= 'Z') {
char d = lines[y + 1][x];
Vector2 thisEnd = new Vector2(x - 2, y == 0 || lines[y - 1][x] != '.' ? y : y - 3);
if (!(d >= 'A' && d <= 'Z')) {
d = lines[y][x + 1];
thisEnd = new Vector2(x == 0 || lines[y][x - 1] != '.' ? x : x - 3, y - 2);
if (!(d >= 'A' && d <= 'Z')) {
continue;
}
}
string name = new string(new char[] { c, d });
if (name == "AA") {
start = thisEnd;
continue;
} else if (name == "ZZ") {
end = thisEnd;
continue;
} else if (portalEnds.TryGetValue(name, out Vector2 otherEnd)) {
portalEnds.Remove(name);
portals[thisEnd] = otherEnd;
portals[otherEnd] = thisEnd;
} else {
portalEnds[name] = thisEnd;
}
}
}
}
}
//Breadth-first search
public int Path() {
Dictionary<Vector2, int> enqueued = new Dictionary<Vector2, int>();
Queue<Vector2> tilesToCheck = new Queue<Vector2>();
tilesToCheck.Enqueue(start);
enqueued[start] = 0;
while (tilesToCheck.Count > 0) {
Vector2 tile = tilesToCheck.Dequeue();
if (portals.TryGetValue(tile, out Vector2 otherEnd) && !enqueued.ContainsKey(otherEnd)) {
tilesToCheck.Enqueue(otherEnd);
enqueued[otherEnd] = enqueued[tile] + 1;
}
foreach (Vector2 neighbourTile in new Vector2[] { tile + Vector2.up, tile + Vector2.down, tile + Vector2.left, tile + Vector2.right }) {
if (neighbourTile.Equals(end)) {
return enqueued[tile] + 1;
}
if (neighbourTile.x >= 0 && neighbourTile.y >= 0 && neighbourTile.x < grid.GetLength(1) && neighbourTile.y < grid.GetLength(0) && grid[neighbourTile.y, neighbourTile.x] && !enqueued.ContainsKey(neighbourTile)) {
tilesToCheck.Enqueue(neighbourTile);
enqueued[neighbourTile] = enqueued[tile] + 1;
}
}
}
return -1;
}
}
class RecursiveGrid {
struct Position : IEquatable<Position> {
public Vector2 coords;
public int layer;
public Position(int x, int y, int layer = 0) : this(new Vector2(x, y), layer) { }
public Position(Vector2 coords, int layer = 0) {
this.coords = coords;
this.layer = layer;
}
public static Position operator +(Position a, Vector2 b) => new Position(a.coords + b, a.layer);
public static Position operator -(Position a, Vector2 b) => new Position(a.coords - b, a.layer);
public static Position operator +(Position a, int b) => new Position(a.coords, a.layer + b);
public static Position operator -(Position a, int b) => new Position(a.coords, a.layer - b);
public static bool operator ==(Position a, Position b) => a.Equals(b);
public static bool operator !=(Position a, Position b) => !a.Equals(b);
public override bool Equals(object obj) => obj is Position position && Equals(position);
public bool Equals(Position other) => coords.x == other.coords.x && coords.y == other.coords.y && layer == other.layer;
public override int GetHashCode() => layer << 16 | coords.x << 8 | coords.y;
}
readonly bool[,] grid;
readonly Dictionary<Vector2, Vector2> outerPortals;
readonly Dictionary<Vector2, Vector2> innerPortals;
readonly Position start, end;
Position? GetOtherPortalPosition(Position position) {
if (position.layer > 0 && outerPortals.TryGetValue(position.coords, out Vector2 otherEnd)) {
return new Position(otherEnd, position.layer - 1);
}
if (innerPortals.TryGetValue(position.coords, out otherEnd)) {
return new Position(otherEnd, position.layer + 1);
}
return null; //Layer 0 outer portal encountered
}
public RecursiveGrid(string fileName) {
outerPortals = new Dictionary<Vector2, Vector2>();
innerPortals = new Dictionary<Vector2, Vector2>();
Dictionary<string, Vector2> portalEnds = new Dictionary<string, Vector2>();
string[] lines = File.ReadAllLines(fileName);
grid = new bool[lines.Length - 4, lines[0].Length - 4];
for (int y = 0; y < lines.Length - 1; y++) {
for (int x = 0; x < lines[y].Length - 1; x++) {
char c = lines[y][x];
if (c == '.') {
grid[y - 2, x - 2] = true;
} else if (c >= 'A' && c <= 'Z') {
char d = lines[y + 1][x];
Vector2 thisEnd = new Vector2(x - 2, y == 0 || lines[y - 1][x] != '.' ? y : y - 3);
if (!(d >= 'A' && d <= 'Z')) {
d = lines[y][x + 1];
thisEnd = new Vector2(x == 0 || lines[y][x - 1] != '.' ? x : x - 3, y - 2);
if (!(d >= 'A' && d <= 'Z')) {
continue;
}
}
string name = new string(new char[] { c, d });
if (name == "AA") {
start = new Position(thisEnd);
continue;
} else if (name == "ZZ") {
end = new Position(thisEnd);
continue;
} else if (portalEnds.TryGetValue(name, out Vector2 otherEnd)) {
portalEnds.Remove(name);
if (thisEnd.x == 0 || thisEnd.y == 0 || thisEnd.x == grid.GetLength(1) - 1 || thisEnd.y == grid.GetLength(0) - 1) {
outerPortals[thisEnd] = otherEnd;
innerPortals[otherEnd] = thisEnd;
} else {
innerPortals[thisEnd] = otherEnd;
outerPortals[otherEnd] = thisEnd;
}
} else {
portalEnds[name] = thisEnd;
}
}
}
}
}
//Breadth-first search
public int Path() {
Dictionary<Position, int> enqueued = new Dictionary<Position, int>();
Queue<Position> tilesToCheck = new Queue<Position>();
tilesToCheck.Enqueue(start);
enqueued[start] = 0;
while (tilesToCheck.Count > 0) {
Position tile = tilesToCheck.Dequeue();
Position? otherEnd = GetOtherPortalPosition(tile);
if (otherEnd.HasValue && !enqueued.ContainsKey(otherEnd.Value)) {
tilesToCheck.Enqueue(otherEnd.Value);
enqueued[otherEnd.Value] = enqueued[tile] + 1;
}
foreach (Position neighbourTile in new Position[] { tile + Vector2.up, tile + Vector2.down, tile + Vector2.left, tile + Vector2.right }) {
if (neighbourTile.Equals(end)) {
return enqueued[tile] + 1;
}
if (neighbourTile.coords.x >= 0 && neighbourTile.coords.y >= 0 && neighbourTile.coords.x < grid.GetLength(1) && neighbourTile.coords.y < grid.GetLength(0) && grid[neighbourTile.coords.y, neighbourTile.coords.x] && !enqueued.ContainsKey(neighbourTile)) {
tilesToCheck.Enqueue(neighbourTile);
enqueued[neighbourTile] = enqueued[tile] + 1;
}
}
}
return -1;
}
}
}