Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2148,6 +2148,21 @@
],
"difficulty": 4
},
{
"slug": "prism",
"name": "Prism",
"uuid": "c52d3d36-4852-48ff-b97c-0b030a5b365d",
"practices": [
"arrays",
"floating-point-numbers",
"while-loops"
],
"prerequisites": [
"arrays",
"floating-point-numbers"
],
"difficulty": 4
},
{
"slug": "twelve-days",
"name": "Twelve Days",
Expand Down
1 change: 1 addition & 0 deletions exercises/Exercises.slnx
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@
<Project Path="practice/pov/Pov.csproj" />
<Project Path="practice/prime-factors/PrimeFactors.csproj" />
<Project Path="practice/protein-translation/ProteinTranslation.csproj" />
<Project Path="practice/prism/Prism.csproj" />
<Project Path="practice/proverb/Proverb.csproj" />
<Project Path="practice/pythagorean-triplet/PythagoreanTriplet.csproj" />
<Project Path="practice/queen-attack/QueenAttack.csproj" />
Expand Down
38 changes: 38 additions & 0 deletions exercises/practice/prism/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Instructions

Before activating the laser array, you must predict the exact order in which crystals will be hit, identified by their sample IDs.

## Example Test Case

Consider this crystal array configuration:

```json
{
"start": { "x": 0, "y": 0, "angle": 0 },
"prisms": [
{ "id": 1, "x": 10, "y": 10, "angle": -90 },
{ "id": 2, "x": 10, "y": 0, "angle": 90 },
{ "id": 3, "x": 30, "y": 10, "angle": 45 },
{ "id": 4, "x": 20, "y": 0, "angle": 0 }
]
}
```

## What's Happening

The laser starts at the origin `(0, 0)` and fires horizontally to the right at angle 0°.
Here's the step-by-step beam path:

**Step 1**: The beam travels along the x-axis (y = 0) and first encounters **Crystal #2** at position `(10, 0)`.
This crystal has a refraction angle of 90°, which means it bends the beam perpendicular to its current path.
The beam, originally traveling at 0°, is now redirected to 90° (straight up).

**Step 2**: The beam now travels vertically upward from position `(10, 0)` and strikes **Crystal #1** at position `(10, 10)`.
This crystal has a refraction angle of -90°, bending the beam by -90° relative to its current direction.
The beam was traveling at 90°, so after refraction it's now at 0° (90° + (-90°) = 0°), traveling horizontally to the right again.

**Step 3**: From position `(10, 10)`, the beam travels horizontally and encounters **Crystal #3** at position `(30, 10)`.
This crystal refracts the beam by 45°, changing its direction to 45°.
The beam continues into empty space beyond the array.

!["A graph showing the path of a laser beam refracted through three prisms."](https://assets.exercism.org/images/exercises/prism/laser_path-light.svg)
5 changes: 5 additions & 0 deletions exercises/practice/prism/.docs/introduction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Introduction

You're a researcher at **PRISM** (Precariously Redirected Illumination Safety Management), working with a precision laser calibration system that tests experimental crystal prisms.
These crystals are being developed for next-generation optical computers, and each one has unique refractive properties based on its molecular structure.
The lab's laser system can damage crystals if they receive unexpected illumination, so precise path prediction is critical.
141 changes: 141 additions & 0 deletions exercises/practice/prism/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
###############################
# Core EditorConfig Options #
###############################

; This file is for unifying the coding style for different editors and IDEs.
; More information at:
; https://docs.microsoft.com/en-us/visualstudio/ide/editorconfig-code-style-settings-reference?view=vs-2017
; https://docs.microsoft.com/en-us/visualstudio/ide/create-portable-custom-editor-options?view=vs-2017

root = true

[*]
indent_style = space

[ProteinTranslation.cs]
indent_size = 4

###############################
# .NET Coding Conventions #
###############################

# Organize usings
dotnet_sort_system_directives_first = true
dotnet_separate_import_directive_groups = true

# this. preferences
dotnet_style_qualification_for_field = false:suggestion
dotnet_style_qualification_for_property = false:suggestion
dotnet_style_qualification_for_method = false:suggestion
dotnet_style_qualification_for_event = false:suggestion

# Language keywords vs BCL types preferences
dotnet_style_predefined_type_for_locals_parameters_members = true:suggestion
dotnet_style_predefined_type_for_member_access = true:suggestion

# Parentheses preferences
dotnet_style_parentheses_in_arithmetic_binary_operators = never_if_unnecessary:none
dotnet_style_parentheses_in_relational_binary_operators = never_if_unnecessary:none
dotnet_style_parentheses_in_other_binary_operators = never_if_unnecessary:none
dotnet_style_parentheses_in_other_operators = never_if_unnecessary:suggestion

# Modifier preferences
dotnet_style_require_accessibility_modifiers = always:suggestion
dotnet_style_readonly_field = true:suggestion

# Expression-level preferences
dotnet_style_object_initializer = true:suggestion
dotnet_style_collection_initializer = true:suggestion
dotnet_style_explicit_tuple_names = true:suggestion
dotnet_style_prefer_inferred_tuple_names = true:suggestion
dotnet_style_prefer_inferred_anonymous_type_member_names = true:suggestion
dotnet_style_prefer_auto_properties = true:suggestion
dotnet_style_prefer_is_null_check_over_reference_equality_method = true:suggestion
dotnet_style_prefer_conditional_expression_over_assignment = true:suggestion
dotnet_style_prefer_conditional_expression_over_return = true:suggestion
dotnet_style_coalesce_expression = true:suggestion
dotnet_style_null_propagation = true:suggestion

###############################
# Naming Conventions #
###############################

# Style Definitions
dotnet_naming_style.pascal_case_style.capitalization = pascal_case

# Use PascalCase for constant fields
dotnet_naming_rule.constant_fields_should_be_pascal_case.severity = suggestion
dotnet_naming_rule.constant_fields_should_be_pascal_case.symbols = constant_fields
dotnet_naming_rule.constant_fields_should_be_pascal_case.style = pascal_case_style
dotnet_naming_symbols.constant_fields.applicable_kinds = field
dotnet_naming_symbols.constant_fields.applicable_accessibilities = *
dotnet_naming_symbols.constant_fields.required_modifiers = const

###############################
# C# Code Style Rules #
###############################

# var preferences
csharp_style_var_for_built_in_types = true:none
csharp_style_var_when_type_is_apparent = true:none
csharp_style_var_elsewhere = true:none

# Expression-bodied members
csharp_style_expression_bodied_methods = true:suggestion
csharp_style_expression_bodied_constructors = true:suggestion
csharp_style_expression_bodied_operators = true:suggestion
csharp_style_expression_bodied_properties = true:suggestion
csharp_style_expression_bodied_indexers = true:suggestion
csharp_style_expression_bodied_accessors = true:suggestion

# Pattern-matching preferences
csharp_style_pattern_matching_over_is_with_cast_check = true:suggestion
csharp_style_pattern_matching_over_as_with_null_check = true:suggestion

# Null-checking preferences
csharp_style_throw_expression = true:suggestion
csharp_style_conditional_delegate_call = true:suggestion

# Modifier preferences
csharp_preferred_modifier_order = public,private,protected,internal,static,extern,new,virtual,abstract,sealed,override,readonly,unsafe,volatile,async:suggestion

# Expression-level preferences
csharp_prefer_braces = true:none
csharp_prefer_simple_default_expression = true:suggestion
csharp_style_deconstructed_variable_declaration = true:suggestion
csharp_style_pattern_local_over_anonymous_function = true:suggestion
csharp_style_inlined_variable_declaration = true:suggestion

###############################
# C# Formatting Rules #
###############################

# New line preferences
csharp_new_line_before_open_brace = all
csharp_new_line_before_else = true
csharp_new_line_before_catch = true
csharp_new_line_before_finally = true
csharp_new_line_before_members_in_object_initializers = false
csharp_new_line_before_members_in_anonymous_types = false
csharp_new_line_between_query_expression_clauses = true

# Indentation preferences
csharp_indent_case_contents = true
csharp_indent_switch_labels = true
csharp_indent_labels = flush_left

# Space preferences
csharp_space_after_cast = false
csharp_space_after_keywords_in_control_flow_statements = true
csharp_space_between_method_declaration_parameter_list_parentheses = false
csharp_space_between_method_call_parameter_list_parentheses = false
csharp_space_before_colon_in_inheritance_clause = true
csharp_space_after_colon_in_inheritance_clause = true
csharp_space_around_binary_operators = before_and_after
csharp_space_between_method_declaration_empty_parameter_list_parentheses = false
csharp_space_between_method_call_name_and_opening_parenthesis = false
csharp_space_between_method_call_empty_parameter_list_parentheses = false

# Wrapping preferences
csharp_preserve_single_line_blocks = true
csharp_preserve_single_line_statements = true
54 changes: 54 additions & 0 deletions exercises/practice/prism/.meta/Example.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
public static class Prism
{
public readonly record struct LaserInfo(double X, double Y, double Angle);

public readonly record struct PrismInfo(int Id, double X, double Y, double Angle);

public static int[] FindSequence(LaserInfo laser, PrismInfo[] prisms)
{
var x = laser.X;
var y = laser.Y;
var angle = laser.Angle;
List<int> sequence = new();

while (true)
{
var rad = double.DegreesToRadians(angle);
var dirX = double.Cos(rad);
var dirY = double.Sin(rad);

PrismInfo? nearest = null;
var nearestDist = double.PositiveInfinity;

foreach (var prism in prisms)
{
var dx = prism.X - x;
var dy = prism.Y - y;

var dist = dx * dirX + dy * dirY;
if (dist <= 1e-6)
continue;

var crossSq = double.Pow(dx - dist * dirX, 2) + double.Pow(dy - dist * dirY, 2);
if (crossSq >= 1e-6 * double.Max(1, dist * dist))
continue;

if (dist < nearestDist)
{
nearestDist = dist;
nearest = prism;
}
}

if (nearest is not { } hit)
break;

sequence.Add(hit.Id);
x = hit.X;
y = hit.Y;
angle = (angle + hit.Angle) % 360;
}

return sequence.ToArray();
}
}
22 changes: 22 additions & 0 deletions exercises/practice/prism/.meta/Generator.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
public class {{ testClass }}
{
{{- for test in tests }}
[Fact{{ if !for.first }}(Skip = "Remove this Skip property to run this test"){{ end }}]
public void {{ test.testMethod }}()
{
Prism.LaserInfo laser = new({{ test.input.start.x }}, {{ test.input.start.y }}, {{ test.input.start.angle }});
{{- if test.input.prisms.empty? }}
Prism.PrismInfo[] prisms = [];
{{- else }}
Prism.PrismInfo[] prisms =
[
{{- for p in test.input.prisms }}
new({{ p.id }}, {{ p.x }}, {{ p.y }}, {{ p.angle }}){{ if !for.last }},{{ end }}
{{- end }}
];
{{- end }}
int[] expected = [{{ array.join test.expected.sequence ", " }}];
Assert.Equal(expected, {{ testedClass }}.{{ test.testedMethod }}(laser, prisms));
}
{{ end -}}
}
22 changes: 22 additions & 0 deletions exercises/practice/prism/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"authors": [
"BNAndras"
],
"files": {
"solution": [
"Prism.cs"
],
"test": [
"PrismTests.cs"
],
"example": [
".meta/Example.cs"
],
"invalidator": [
"Prism.csproj"
]
},
"blurb": "Calculate the path of a laser through refractive prisms.",
"source": "FraSanga",
"source_url": "https://github.com/exercism/problem-specifications/pull/2625"
}
52 changes: 52 additions & 0 deletions exercises/practice/prism/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# This is an auto-generated file.
#
# Regenerating this file via `configlet sync` will:
# - Recreate every `description` key/value pair
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
# - Preserve any other key/value pair
#
# As user-added comments (using the # character) will be removed when this file
# is regenerated, comments can be added via a `comment` key.

[ec65d3b3-f7bf-4015-8156-0609c141c4c4]
description = "zero prisms"

[ec0ca17c-0c5f-44fb-89ba-b76395bdaf1c]
description = "one prism one hit"

[0db955f2-0a27-4c82-ba67-197bd6202069]
description = "one prism zero hits"

[8d92485b-ebc0-4ee9-9b88-cdddb16b52da]
description = "going up zero hits"

[78295b3c-7438-492d-8010-9c63f5c223d7]
description = "going down zero hits"

[acc723ea-597b-4a50-8d1b-b980fe867d4c]
description = "going left zero hits"

[3f19b9df-9eaa-4f18-a2db-76132f466d17]
description = "negative angle"

[96dacffb-d821-4cdf-aed8-f152ce063195]
description = "large angle"

[513a7caa-957f-4c5d-9820-076842de113c]
description = "upward refraction two hits"

[d452b7c7-9761-4ea9-81a9-2de1d73eb9ef]
description = "downward refraction two hits"

[be1a2167-bf4c-4834-acc9-e4d68e1a0203]
description = "same prism twice"

[df5a60dd-7c7d-4937-ac4f-c832dae79e2e]
description = "simple path"

[8d9a3cc8-e846-4a3b-a137-4bfc4aa70bd1]
description = "multiple prisms floating point precision"

[e077fc91-4e4a-46b3-a0f5-0ba00321da56]
description = "complex path with multiple prisms floating point precision"
11 changes: 11 additions & 0 deletions exercises/practice/prism/Prism.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
public static class Prism
{
public readonly record struct LaserInfo(double X, double Y, double Angle);

public readonly record struct PrismInfo(int Id, double X, double Y, double Angle);

public static int[] FindSequence(LaserInfo laser, PrismInfo[] prisms)
{
throw new NotImplementedException("You need to implement this method.");
}
}
Loading
Loading