Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// part 1
var input = "<input here>";
var visited = new List<Point> { new Point(0, 0) };
var x = 0;
var y = 0;
foreach (var d in input)
{
if (d == 'v') y--;
if (d == '>') x++;
if (d == '^') y++;
if (d == '<') x--;
if (!visited.Any(a => a.X == x && a.Y == y))
{
visited.Add(new Point(x, y));
}
}
// part 2
var input = "<input here>";
var visited = new List<Point> { new Point(0, 0) };
var x1 = 0;
var y1 = 0;
var x2 = 0;
var y2 = 0;
var i = 0;
foreach (var d in input)
{
var x = 0;
var y = 0;
if (i % 2 == 0)
{
if (d == 'v') y1--;
if (d == '>') x1++;
if (d == '^') y1++;
if (d == '<') x1--;
x = x1;
y = y1;
}
else
{
if (d == 'v') y2--;
if (d == '>') x2++;
if (d == '^') y2++;
if (d == '<') x2--;
x = x2;
y = y2;
}
if (!visited.Any(a => a.X == x && a.Y == y))
{
visited.Add(new Point(x, y));
}
i++;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.