-
Notifications
You must be signed in to change notification settings - Fork 0
/
2016_13.cs
160 lines (134 loc) · 3.84 KB
/
2016_13.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
namespace AOC.CSharp;
public class AOC2016_13
{
public static long Solve1(string[] lines, string extra)
{
int favorite = int.Parse(lines[0]);
Layout layout = new(favorite);
HashSet<XY> seen = new();
Queue<XY> queue = new();
string[] targetSplits = extra.Split(",");
int targetX = int.Parse(targetSplits[0]);
int targetY = int.Parse(targetSplits[1]);
XY target = new(targetX, targetY);
// Perform BFS to find the optimal path
int steps = 0;
queue.Enqueue(new XY(1, 1));
while (queue.Count > 0)
{
int levelLength = queue.Count;
while (levelLength > 0)
{
XY curr = queue.Dequeue();
seen.Add(curr);
if (curr == target)
{
// Search until we arrive at the target cell
return steps;
}
List<XY> moves = layout.GetMovesFrom(curr, seen);
foreach (XY move in moves)
{
queue.Enqueue(move);
}
levelLength--;
}
steps++;
}
return 0L;
}
public static long Solve2(string[] lines)
{
int favorite = int.Parse(lines[0]);
Layout layout = new(favorite);
HashSet<XY> seen = new();
Queue<XY> queue = new();
// Perform BFS to fully explore the graph to a certain depth
int steps = 0;
queue.Enqueue(new XY(1, 1));
while (steps <= 50)
{
int levelLength = queue.Count;
while (levelLength > 0)
{
XY curr = queue.Dequeue();
seen.Add(curr);
List<XY> moves = layout.GetMovesFrom(curr, seen);
foreach (XY move in moves)
{
queue.Enqueue(move);
}
levelLength--;
}
steps++;
}
// Exhaustively search to a certain number of steps and note every distinct spot we visited
return seen.Count;
}
private static State CalcState(XY xy, long favorite)
{
long x = xy.X;
long y = xy.Y;
long result = (x * x) + (3 * x) + (2 * x * y) + y + (y * y) + favorite;
long bits = 0;
while (result > 0)
{
bits += result & 1L;
result >>= 1;
}
return bits % 2 == 0 ? State.Open : State.Wall;
}
private class Layout
{
private readonly Dictionary<XY, State> _dict = new();
private readonly long _favorite;
public Layout(long favorite)
{
_favorite = favorite;
}
public State Get(XY xy)
{
if (_dict.TryGetValue(xy, out State cached))
{
return cached;
}
State calculated = CalcState(xy, _favorite);
_dict.Add(xy, calculated);
return calculated;
}
public List<XY> GetMovesFrom(XY xy, HashSet<XY> seen)
{
XY[] options = new[]
{
xy with
{
Y = xy.Y - 1
},
xy with
{
X = xy.X + 1
},
xy with
{
Y = xy.Y + 1
},
xy with
{
X = xy.X - 1
},
};
return options
.Where(o => o.IsInBounds && Get(o) == State.Open && !seen.Contains(o))
.ToList();
}
}
private record XY(long X, long Y)
{
public bool IsInBounds => X >= 0 && Y >= 0;
}
private enum State
{
Open,
Wall,
}
}