diff --git a/README.md b/README.md index 3b9a308..d728130 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,7 @@ |Challenge|Challenge Date|Contributor(s)|Source| |:-|:-|:-|:-| |[Wire Ends](src/Wire%20Ends)| |[@zacharypatten](https://github.com/ZacharyPatten)|_original_| +|[Robot Gravity](src/Robot%20Gravity)| |[@zacharypatten](https://github.com/ZacharyPatten)|_original_| Contributions are welcome. :) diff --git a/challenges.sln b/challenges.sln index 4f50694..905a2fc 100644 --- a/challenges.sln +++ b/challenges.sln @@ -5,6 +5,8 @@ VisualStudioVersion = 17.0.31903.59 MinimumVisualStudioVersion = 10.0.40219.1 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Wire Ends", "src\Wire Ends\Wire Ends.csproj", "{5468530B-AA66-4594-A126-4884664BE91D}" EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Robot Gravity", "src\Robot Gravity\Robot Gravity.csproj", "{98FAB7D8-0C64-43B0-B03C-9CF08BED9089}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -15,6 +17,10 @@ Global {5468530B-AA66-4594-A126-4884664BE91D}.Debug|Any CPU.Build.0 = Debug|Any CPU {5468530B-AA66-4594-A126-4884664BE91D}.Release|Any CPU.ActiveCfg = Release|Any CPU {5468530B-AA66-4594-A126-4884664BE91D}.Release|Any CPU.Build.0 = Release|Any CPU + {98FAB7D8-0C64-43B0-B03C-9CF08BED9089}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {98FAB7D8-0C64-43B0-B03C-9CF08BED9089}.Debug|Any CPU.Build.0 = Debug|Any CPU + {98FAB7D8-0C64-43B0-B03C-9CF08BED9089}.Release|Any CPU.ActiveCfg = Release|Any CPU + {98FAB7D8-0C64-43B0-B03C-9CF08BED9089}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/src/Robot Gravity/README.md b/src/Robot Gravity/README.md new file mode 100644 index 0000000..628128d --- /dev/null +++ b/src/Robot Gravity/README.md @@ -0,0 +1,129 @@ +> This is a challenge developed for the C# discord server.
+> Want to Participate? Join the C# discord: + +# Robot Gravity + +You are making a robot to explore other planets. Your robot has a gravity sensor but you want to +have a back up way to determine the current gravity in case the sensor is damaged.Your robot has +the ability to jump and it has an height sensor that asynchronously scans about every .01 seconds. +Use those features to determine the current acceleration due to gravity as a positive number +within 0.1 units. + +Use these members from `Robot robot`: +- `robot.Jump` +- `robot.MaxJumpForce` +- `robot.HeightSensor` +- `robot.Mass` + +
+Hint #1 + +The equation for the vertical position of a projectile is `p = -.5*g*s^2 + v*s + i` where +- `p` is vertical position +- `g` is acceleration due to gravity +- `s` is time +- `v` is initial velocity (in this case initial velocity is the parameter to `robot.Jump` divided by `robot.Mass`) +- `i` is initial vertical position (in this case i is always 0) + +
+ +
+Hint #2 + +`robot.HeightSensor` is an event. You need to make an event handler with the same method signature and subscribe to the event. + +```cs +void HandleHeightSensor(double height, DateTime time) +{ + // some code here... +} + +robot.HeightSensor += HandleHeightSensor; +``` + +
+ +### Quick start + +Cloning this repo is recommended. + +Complete the challenge in the MS Test project here: [RobotGravityTests.cs](RobotGravityTests.cs) + +### Feedback + +If you have any feedback on the challenges, [please open an issue](https://github.com/discord-csharp/challenges/issues/new/choose), mention the challenge, and ping the contributor(s) of the challenge. + +### Contributor(s) + +- [@zacharypatten](https://github.com/ZacharyPatten) + +### Source + +This was an original challege. :) + +--- + +Discord meta data. Do not edit. This is used for GitHub => Discord linking. + + + + + + + +
Name + Robot Gravity Challenge +
Description + You are making a robot to explore other planets. Your robot has a gravity sensor but you want to have a back up way to determine the current gravity in case the sensor is damaged. Your robot has the ability to jump and it has an height sensor that asynchronously scans about every .01 seconds. Use those features to determine the current acceleration due to gravity as a positive number within 0.1 units. +
Sample + + +Cloning the repo and complete the challenge in the MS Test project here: https://github.com/discord-csharp/challenges/tree/main/src/Robot%20Gravity/RobotGravityTests.cs + +```cs +public double DetermineGravity(Robot robot) +{ + // ----------------------- + // Complete challenge here. + // + // - robot.Jump + // - robot.MaxJumpForce + // - robot.HeightSensor + // - robot.Mass + // + // ----------------------- +} +``` + +
+Hint #1 + +The equation for the vertical position of a projectile is `p = -.5*g*s^2 + v*s + i` where +- `p` is vertical position +- `g` is acceleration due to gravity +- `s` is time +- `v` is initial velocity (in this case initial velocity is the parameter to `robot.Jump` divided by `robot.Mass`) +- `i` is initial vertical position (in this case i is always 0) + +
+ +
+Hint #2 + +`robot.HeightSensor` is an event. You need to make an event handler with the same method signature and subscribe to the event. + +```cs +void HandleHeightSensor(double height, DateTime time) +{ + // some code here... +} + +robot.HeightSensor += HandleHeightSensor; +``` + +
+
Contributed by + 438382611929366537 +
Self link + https://github.com/discord-csharp/challenges/tree/main/src/Robot%20Gravity +
\ No newline at end of file diff --git a/src/Robot Gravity/Robot Gravity.csproj b/src/Robot Gravity/Robot Gravity.csproj new file mode 100644 index 0000000..b9ce558 --- /dev/null +++ b/src/Robot Gravity/Robot Gravity.csproj @@ -0,0 +1,14 @@ + + + net6.0 + Space_Robot + enable + false + + + + + + + + diff --git a/src/Robot Gravity/RobotGravityTests.cs b/src/Robot Gravity/RobotGravityTests.cs new file mode 100644 index 0000000..83ebf05 --- /dev/null +++ b/src/Robot Gravity/RobotGravityTests.cs @@ -0,0 +1,115 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using System; +using System.Timers; + +namespace Space_Robot +{ + [TestClass] + public class RobotGravityTests + { + public static double DetermineGravity(Robot robot) + { + // ----------------------- + // Complete challenge here. + // + // You are making a robot to explore other planets. Your robot has a gravity sensor but you want to + // have a back up way to determine the current gravity in case the sensor is damaged.Your robot has + // the ability to jump and it has an height sensor that asynchronously scans about every .01 seconds. + // Use those features to determine the current acceleration due to gravity as a positive number + // within 0.1 units. + // + // - robot.Jump + // - robot.MaxJumpForce + // - robot.HeightSensor + // - robot.Mass + // + // ----------------------- + throw new NotImplementedException(); // <- delete + } + + public class Robot + { + [Obsolete("You don't need to make any Robots. :P")] + public Robot(Func<(double, DateTime)> getHeight, Action jump) + { + Timer timer = new((int)HeightSensorInterval.TotalMilliseconds); + timer.Elapsed += (o, e) => + { + var (height, time) = getHeight(); + HeightSensor?.Invoke(height, time); + }; + timer.Enabled = true; + JumpCallback = jump; + } + private Action JumpCallback { get; set; } + private readonly TimeSpan HeightSensorInterval = TimeSpan.FromSeconds(.01); + public double Mass { get; private set; } = 5; + public double MaxJumpForce { get; private set; } = 50d; + public event Action? HeightSensor; + public void Jump(double force) => JumpCallback?.Invoke(force); + } + + // measurements in meters per second squared (m/s²) + [TestMethod] public void TestGravityOnMercury() => Test(3.7); + [TestMethod] public void TestGravityOnVenus() => Test(8.87); + [TestMethod] public void TestGravityOnEarth() => Test(9.807); + [TestMethod] public void TestGravityOnMars() => Test(3.721); + [TestMethod] public void TestGravityOnJupiter() => Test(24.79); + [TestMethod] public void TestGravityOnSaturn() => Test(10.44); + [TestMethod] public void TestGravityOnUranus() => Test(8.87); + [TestMethod] public void TestGravityOnNeptune() => Test(11.15); + [TestMethod] public void TestGravityOnPluto() => Test(0.62); + [TestMethod] public void TestGravityOnEarthMoon() => Test(1.62); + + static void Test(double gravity) + { + try + { + Robot robot = InitializeRobot(gravity); + double determinedGravity = Math.Abs(DetermineGravity(robot)); + Assert.IsTrue(Math.Abs(determinedGravity - gravity) <= 0.1, $"expected {gravity} but got {determinedGravity}"); + } + catch (NotImplementedException) + { + Assert.Inconclusive(); + } + } + + static Robot InitializeRobot(double gravity) + { + (double InitialVelocity, DateTime Time)? jump = null; + + Robot robot = default!; +#pragma warning disable CS0618 // Type or member is obsolete + robot = new( + getHeight: GetHeight, + jump: force => + { + if (GetHeight().Height is 0) + { + jump = (force / robot.Mass, DateTime.Now); + } + }); +#pragma warning restore CS0618 // Type or member is obsolete + + (double Height, DateTime Time) GetHeight() + { + DateTime now = DateTime.Now; + if (jump is null) + { + return (0, now); + } + double seconds = (now - jump.Value.Time).TotalSeconds; + double height = -.5 * gravity * seconds * seconds + jump.Value.InitialVelocity * seconds; + if (height < 0) + { + jump = null; + height = 0; + } + return (height, now); + } + + return robot; + } + } +} \ No newline at end of file