-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path22-1.linq
76 lines (64 loc) · 2 KB
/
22-1.linq
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
<Query Kind="Program" />
int _bursts = 10000;
void Main()
{
Directory.SetCurrentDirectory(Path.GetDirectoryName(Util.CurrentQueryPath));
string[] lines = File.ReadAllLines("22.txt");
HashSet<Tuple<int, int>> infections = new HashSet<Tuple<int, int>>();
int minX;
int maxX;
int minY;
int maxY;
maxY = lines.Length / 2;
minY = maxY * -1;
maxX = lines.First().Length / 2;
minX = maxX * -1;
for (int y = minY ; y <= maxY; y++)
{
for (int x = minX; x <= maxX; x++)
{
if (lines[y+maxY][x+maxX] == '#') infections.Add(new Tuple<int, int>(y, x));
}
}
Direction d = Direction.N;
var pos = new Tuple<int, int>(0, 0);
int newInfections = 0;
for (int i = 1; i <= _bursts; i++)
{
bool infected = infections.Contains(pos);
if (infected) d = Infected[d];
else d = Clean[d];
if (infected) infections.Remove(pos);
else
{
infections.Add(pos);
newInfections++;
}
if (d == Direction.N) pos = new Tuple<int, int>(pos.Item1 - 1, pos.Item2);
else if (d == Direction.S) pos = new Tuple<int, int>(pos.Item1 + 1, pos.Item2);
else if (d == Direction.W) pos = new Tuple<int, int>(pos.Item1, pos.Item2 - 1);
else if (d == Direction.E) pos = new Tuple<int, int>(pos.Item1, pos.Item2 + 1);
}
newInfections.Dump();
}
public enum Direction
{
N,
S,
E,
W
}
public static Dictionary<Direction, Direction> Infected = new Dictionary<Direction, Direction>
{
{ Direction.N, Direction.E },
{ Direction.E, Direction.S },
{ Direction.S, Direction.W },
{ Direction.W, Direction.N }
};
public static Dictionary<Direction, Direction> Clean = new Dictionary<Direction, Direction>
{
{ Direction.N, Direction.W },
{ Direction.W, Direction.S },
{ Direction.S, Direction.E },
{ Direction.E, Direction.N }
};