C
C#2y ago
V0FBU1VM

❔ Write an Aggregate

public override string SolvePartOne()
{
HashSet<House> visited = new HashSet<House>()
{
new House(0, 0)
};

int x = 0,
y = 0;

foreach (char c in Input)
{
switch (c)
{
case '^': y++; break;
case 'v': y--; break;
case '>': x++; break;
case '<': x--; break;
default:
throw new InvalidDataException();
}

visited.Add(new House(x, y));
}

return visited.Count.ToString();
}
public override string SolvePartOne()
{
HashSet<House> visited = new HashSet<House>()
{
new House(0, 0)
};

int x = 0,
y = 0;

foreach (char c in Input)
{
switch (c)
{
case '^': y++; break;
case 'v': y--; break;
case '>': x++; break;
case '<': x--; break;
default:
throw new InvalidDataException();
}

visited.Add(new House(x, y));
}

return visited.Count.ToString();
}
Could I aggregate through Input to archive the same result? I'm learning the linq Aggregate, and just curious if I could archive the same result with an Aggregate?
5 Replies
KKSK@🌸🎵🌸🎵🌸
Yes, you could use the Aggregate method to achieve the same result as the code you posted. The Aggregate method allows you to perform a calculation on a sequence and return a single result. In this case, you could use it to track the visited houses and return the total number of houses visited. Here's an example of how you could use Aggregate to accomplish this:
public override string SolvePartOne()
{
// Initialize the set of visited houses with the starting house at (0, 0)
var visited = new HashSet<House>()
{
new House(0, 0)
};

// Use Aggregate to track the visited houses
visited = Input.Aggregate(visited, (houses, direction) =>
{
// Update the current position based on the direction
switch (direction)
{
case '^': y++; break;
case 'v': y--; break;
case '>': x++; break;
case '<': x--; break;
default:
throw new InvalidDataException();
}

// Add the new position to the set of visited houses
houses.Add(new House(x, y));

// Return the updated set of visited houses
return houses;
});

// Return the total number of visited houses
return visited.Count.ToString();
}
public override string SolvePartOne()
{
// Initialize the set of visited houses with the starting house at (0, 0)
var visited = new HashSet<House>()
{
new House(0, 0)
};

// Use Aggregate to track the visited houses
visited = Input.Aggregate(visited, (houses, direction) =>
{
// Update the current position based on the direction
switch (direction)
{
case '^': y++; break;
case 'v': y--; break;
case '>': x++; break;
case '<': x--; break;
default:
throw new InvalidDataException();
}

// Add the new position to the set of visited houses
houses.Add(new House(x, y));

// Return the updated set of visited houses
return houses;
});

// Return the total number of visited houses
return visited.Count.ToString();
}
As you can see, the Aggregate method allows us to perform the same operations as the code you posted, but with a more concise and functional-style syntax. I hope this helps! Let me know if you have any other questions.
V0FBU1VM
V0FBU1VMOP2y ago
Can I have multiple parameters here (houses, direction) or is it set? The solution looks wonderful, thanks by the way.
phaseshift
phaseshift2y ago
It's always two params. The accumulation thing, and the next element
V0FBU1VM
V0FBU1VMOP2y ago
I see, that's good to know. Thanks
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server