-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day17.cs
182 lines (160 loc) · 4.76 KB
/
Day17.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Numerics;
class Day17 {
public static int Solution1() {
RobotController controller = new RobotController {
InQueue = new Queue<BigInteger>(),
OutQueue = new Queue<BigInteger>()
};
Intcode robot = new Intcode("input17.txt");
robot.SetInput(controller);
robot.SetOutput(controller);
robot.Run();
int sum = 0;
for (int y = 0; y < controller.grid.width; y++) {
for (int x = 0; x < controller.grid.height; x++) {
if (controller.grid[new Vector2(x, y)] && controller.grid[new Vector2(x - 1, y)] && controller.grid[new Vector2(x + 1, y)] && controller.grid[new Vector2(x, y - 1)] && controller.grid[new Vector2(x, y + 1)]) {
sum += x * y;
}
}
}
return sum;
}
public static int Solution2() {
RobotController controller = new RobotController {
InQueue = new Queue<BigInteger>(),
OutQueue = new Queue<BigInteger>()
};
BigInteger[] memory = Array.ConvertAll(File.ReadAllText("input17.txt").Split(','), BigInteger.Parse);
memory[0] = 2; //Scrub4Free hax
Intcode robot = new Intcode(memory);
robot.SetInput(controller);
robot.SetOutput(controller);
robot.Run();
/* Manual solving help
Vector2 currentDirection = Vector2.up;
Vector2 GetFreeDirection() {
foreach (Vector2 dir in new Vector2[] { Vector2.up, Vector2.down, Vector2.left, Vector2.right }) {
if (!dir.Equals(-currentDirection) && controller.grid[controller.robotPos + dir]) {
if (currentDirection.x == 0) {
Console.Write(currentDirection.y == dir.x ? 'L' : 'R');
} else {
Console.Write(currentDirection.x == dir.y ? 'R' : 'L');
}
return dir;
}
}
return new Vector2();
}
while (!(currentDirection = GetFreeDirection()).Equals(new Vector2())) {
int segmentLength = 0;
while (controller.grid[controller.robotPos + currentDirection]) {
controller.robotPos += currentDirection;
segmentLength++;
}
Console.Write(segmentLength + ", ");
}
Console.WriteLine();
*/
return 0;
}
class Grid {
readonly Dictionary<Vector2, bool> grid = new Dictionary<Vector2, bool>();
public int height = -1;
public int width = -1;
public bool this[Vector2 coords] {
get => grid.TryGetValue(coords, out bool isFloor) && isFloor;
set => grid[coords] = value;
}
public void Render(Vector2 robotPos, Facing facing) {
//Initialize width and height
if (height == -1) {
foreach (Vector2 coords in grid.Keys) {
width = Math.Max(width, coords.x + 1);
height = Math.Max(height, coords.y + 1);
}
}
string screen = "";
for (int y = 0; y < width; y++) {
for (int x = 0; x < height; x++) {
if (y == robotPos.y && x == robotPos.x) {
screen += facing == Facing.North ? '^' : (facing == Facing.South ? 'v' : (facing == Facing.West ? '<' : '>'));
continue;
}
screen += this[new Vector2(x, y)] ? '#' : '.';
}
screen += "\n";
}
Console.Write(screen);
}
}
class RobotController : IIntcode {
public Vector2 robotPos = new Vector2();
Facing facing = Facing.North;
public Grid grid = new Grid();
public IIntcode Input { get; set; }
public IIntcode Output { get; set; }
public Queue<BigInteger> InQueue { get; set; }
public Queue<BigInteger> OutQueue { get; set; }
public void RequestInput() {
ReadQueueToGrid();
EnqueueString("A,B,B,C,B,C,B,C,A,A\nL,6,R,8,L,4,R,8,L,12\nL,12,R,10,L,4\nL,12,L,6,L,4,L,4\nn\n"); //Manual solution
}
public void Continue() {
ReadQueueToGrid();
grid.Render(robotPos, facing);
if (InQueue.Count > 0) {
Console.WriteLine(InQueue.Dequeue());
}
}
void EnqueueString(string toQueue) {
foreach (char c in toQueue) {
OutQueue.Enqueue(c);
}
}
void ReadQueueToGrid() {
int y = 0;
int x = 0;
while (InQueue.Count > 0) {
if (InQueue.Peek().CompareTo(char.MaxValue) > 0) {
return;
}
char tile = (char)InQueue.Dequeue();
if (tile == '\n') {
x = 0;
y++;
continue;
}
if (tile == '.' || tile == 'X') {
grid[new Vector2(x, y)] = false;
if (tile == 'X') {
robotPos = new Vector2(x, y);
}
} else if (tile == '#' || tile == '^' || tile == 'v' || tile == '<' || tile == '>') {
grid[new Vector2(x, y)] = true;
if (tile != '#') {
robotPos = new Vector2(x, y);
if (tile == '^') {
facing = Facing.North;
} else if (tile == 'v') {
facing = Facing.South;
} else if (tile == '<') {
facing = Facing.West;
} else {
facing = Facing.East;
}
}
}
x++;
}
}
}
enum Facing {
North,
South,
West,
East
}
}