-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTask15_2.cs
264 lines (210 loc) · 7.03 KB
/
Task15_2.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
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
using FluentAssertions;
using NUnit.Framework;
namespace AoC_2024;
[TestFixture]
public class Task15_2
{
[Test]
[TestCase(@"##########
#..O..O.O#
#......O.#
#.OO..O.O#
#O#..O...#
#O..O..O.#
#.OO.O.OO#
#....O...#
##########
<vv>^<v^>v>^vv^v>v<>v^v<v<^vv<<<^><<><>>v<vvv<>^v^>^<<<><<v<<<v^vv^v>^
vvv<<^>^v^^><<>>><>^<<><^vv^^<>vvv<>><^^v>^>vv<>v<<<<v<^v>^<^^>>>^<v<v
><>vv>v^v^<>><>>>><^^>vv>v<^^^>>v^v^<^^>v^^>v^<^v>v<>>v^v^<v>v^^<^^vv<
<<v<^>>^^^^>>>v^<>vvv^><v<<<>^^^vv^<vvv>^>v<^^^^v<>^>vvvv><>>v^<<^^^^^
^><^><>>><>^^<<^^v>>><^<v>^<vv>>v>>>^v><>^v><<<<v>>v<v<v>vvv>^<><<>^><
^>><>^v<><^vvv<^^<><v<<<<<><^v<<<><<<^^<v<^^^><^>>^<v^><<<^>>^v<v^v<v^
>^>>^v>vv>^<<^v<>><<><<v<<v><>v<^vv<<<>^^v^>^^>>><<^v>>v^v><^^>>^<>vv^
<><^^>^^^<><vvvvv^v<v<<>^v<v>v<<^><<><<><<<^^<<<^<<>><<><^^^>^^<>^>v<>
^^>vv<^v^v<vv>^<><v<^v>^^^>>>^^vvv^>vvv<>>>^<^>>>>>^<<^v>^vvv<>^<><<v>
v^^>>><<^^<>>^v^<v^vv<>v^<<>^<^v^v><^<<<><<^<v><v<>vv>>v><v^<vv<>v^<<^", 9021)]
[TestCase(@"#######
#...#.#
#.....#
#..OO@#
#..O..#
#.....#
#######
<vv<<^^<<^^", 618)]
[TestCase(@"Task15.txt", 1386070)]
public void Task(string input, long expected)
{
input = File.Exists(input) ? File.ReadAllText(input) : input;
var split = input.SplitEmpty(Environment.NewLine + Environment.NewLine);
var map = split[0]
.Replace(".", "..")
.Replace("#", "##")
.Replace("O", "[]")
.Replace("@", "@.")
.SplitLines().Select(x => x.ToArray()).ToArray();
var moves = split[1].Replace("\r", "").Replace("\n", "");
var robot = new Point();
for (var i = 0; i < map.Length; i++)
for (var j = 0; j < map[i].Length; j++)
{
if (map[i][j] == '@')
{
//map[i][j] = '.';
robot = new Point(i, j);
break;
}
}
foreach (var move in moves)
{
robot = Move(robot, map, Moves[move]);
var dbg = Extensions.PrintMap(map);
}
var result = 0L;
for (var i = 0; i < map.Length; i++)
for (var j = 0; j < map[i].Length; j++)
{
if (map.Get((i, j)) == '[')
{
result += 100 * i + j;
}
}
result.Should().Be(expected);
}
private bool CheckWall(Point objPosition, char[][] map, Point move)
{
if (map.Get(objPosition) == '.')
{
return false;
}
var newPositions = GetNewPositions(objPosition, map, move);
var nextObjects = newPositions.Select(map.Get).ToArray();
if (nextObjects.Any(x => x == '#'))
{
return true;
}
if (nextObjects.All(x => x == '.'))
{
return false;
}
var boxes = newPositions.Where(x => boxChars.Contains(map.Get(x))).ToArray();
return boxes.Any(box => CheckWall(box, map, move));
}
private static readonly HashSet<char> boxChars = ['[', ']'];
private Point Move(Point objPosition, char[][] map, Point move)
{
if (move.Row != 0)
{
var checkWall = CheckWall(objPosition, map, move);
if (checkWall) return objPosition;
BruteForceMove(objPosition, map, move);
return objPosition + move;
}
return MoveHorizontal(objPosition, map, move);
}
private void BruteForceMove(Point objPosition, char[][] map, Point move)
{
var boxes = new HashSet<Point>();
FlatBoxes(objPosition, map, move, boxes);
var orderedBoxes = move.Row < 0
? boxes.OrderBy(x => x.Row).ToArray()
: boxes.OrderByDescending(x => x.Row).ToArray();
foreach (var box in orderedBoxes)
{
var newBox = box + move;
map[newBox.Row][newBox.Col] = map.Get(box);
map[box.Row][box.Col] = '.';
}
}
private void FlatBoxes(Point objPosition, char[][] map, Point move, HashSet<Point> boxes)
{
var c = map.Get(objPosition);
if (c == '.') return;
boxes.Add(objPosition);
if (c == '[')
{
boxes.Add(objPosition + (0, 1));
}
if (c == ']')
{
boxes.Add(objPosition + (0, -1));
}
var newPositions = GetNewPositions(objPosition, map, move);
foreach (var newPosition in newPositions)
{
FlatBoxes(newPosition, map, move, boxes);
}
}
private Point[] GetNewPositions(Point objPosition, char[][] map, Point move)
{
var obj = map.Get(objPosition);
if (obj == '@') return [objPosition + move];
if (obj == '[') return [objPosition + move, new Point(objPosition.Row, objPosition.Col + 1) + move];
if (obj == ']') return [objPosition + move, new Point(objPosition.Row, objPosition.Col - 1) + move];
throw new Exception("dasdsad");
}
private Point MoveHorizontal(Point robot, char[][] map, Point move)
{
var newRobot = robot + move;
var result = newRobot;
var c = map.Get(newRobot);
if (c == '.') result = newRobot;
if (c == '#') result = robot;
if (c == '[' || c == ']')
{
var newC = MoveHorizontal(newRobot, map, move);
if (newC.Equals(newRobot)) result = robot;
else result = newRobot;
}
if (result.Equals(newRobot))
{
map[newRobot.Row][newRobot.Col] = map.Get(robot);
map[robot.Row][robot.Col] = '.';
}
return result;
}
private static readonly Dictionary<char, Point> Moves = new()
{
{ '^', Extensions.UpStep },
{ 'v', Extensions.DownStep },
{ '<', Extensions.LeftStep },
{ '>', Extensions.RightStep },
};
class Robot
{
public Point Start { get; set; }
public Point Position { get; set; }
public Point Velocity { get; set; }
public Point MapSize { get; set; }
private int GetPosition(int start, int velocity, int size, int seconds)
{
var p = start + velocity * seconds;
var d = Math.Abs(p) % size;
var sign = Math.Sign(p);
return (size + d * sign) % size;
}
public Point Move(int seconds)
{
var position = new Point(
GetPosition(Start.Y, Velocity.Y, MapSize.Y, seconds),
GetPosition(Start.X, Velocity.X, MapSize.X, seconds)
);
Position = position;
return position;
}
public int GetQuadrant()
{
var middleX = MapSize.X / 2;
var middleY = MapSize.Y / 2;
var newX = Position.X - middleX;
var newY = Position.Y - middleY;
if (newX == 0 || newY == 0) return -1;
if (newX < 0 && newY < 0) return 1;
if (newX < 0 && newY > 0) return 2;
if (newX > 0 && newY > 0) return 3;
if (newX > 0 && newY < 0) return 4;
throw new Exception("dsdsdsdsd");
}
}
}