-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day11.cs
114 lines (98 loc) · 2.77 KB
/
Day11.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
using System;
using System.Collections.Generic;
using System.Numerics;
class Day11 {
public static int Solution1() {
RobotController robotController = new RobotController {
InQueue = new Queue<BigInteger>(),
OutQueue = new Queue<BigInteger>()
};
Intcode robot = new Intcode("input11.txt");
robot.SetInput(robotController);
robot.SetOutput(robotController);
robot.Run();
return robotController.grid.paintedTiles;
}
public static int Solution2() {
RobotController robotController = new RobotController {
InQueue = new Queue<BigInteger>(),
OutQueue = new Queue<BigInteger>()
};
robotController.grid[new Vector2()] = true;
Intcode robot = new Intcode("input11.txt");
robot.SetInput(robotController);
robot.SetOutput(robotController);
robot.Run();
robotController.grid.Render();
return 0;
}
class ShipGrid {
readonly Dictionary<Vector2, bool> grid = new Dictionary<Vector2, bool>();
public int paintedTiles = 0;
public bool this[Vector2 coords] {
get => grid.TryGetValue(coords, out bool value) && value;
set {
if (!grid.ContainsKey(coords)) {
paintedTiles++;
}
grid[coords] = value;
}
}
public void Render() {
int minX = 0, maxX = 0, minY = 0, maxY = 0;
foreach (Vector2 coords in grid.Keys) {
minX = Math.Min(minX, coords.x);
maxX = Math.Max(maxX, coords.x);
minY = Math.Min(minY, coords.y);
maxY = Math.Max(maxY, coords.y);
}
for (int y = minY; y <= maxY; y++) {
for (int x = minX; x <= maxX; x++) {
Console.Write(this[new Vector2(x, y)] ? ' ' : '█');
}
Console.WriteLine();
}
}
}
class RobotController : IIntcode {
enum Facing {
North,
East,
South,
West
}
Vector2 position = new Vector2();
Facing facing = Facing.North;
public ShipGrid grid = new ShipGrid();
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() {
//Check if there's movement data to process in the InQueue
if (InQueue.Count > 0) {
grid[position] = InQueue.Dequeue() == 1;
facing = (Facing)(((int)facing + (InQueue.Dequeue() == 1 ? 1 : 3)) % 4);
switch (facing) {
case Facing.North:
position += new Vector2(0, -1);
break;
case Facing.East:
position += new Vector2(1, 0);
break;
case Facing.South:
position += new Vector2(0, 1);
break;
case Facing.West:
position += new Vector2(-1, 0);
break;
}
}
//Add current tile to OutQueue
OutQueue.Enqueue(grid[position] ? 1 : 0);
}
public void Continue() {
return;
}
}
}