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
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -1230,6 +1230,14 @@
],
"difficulty": 5
},
{
"slug": "prism",
"name": "Prism",
"uuid": "366fef41-bf62-4314-a6f5-bba9eacd82a0",
"practices": [],
"prerequisites": [],
"difficulty": 5
},
{
"slug": "pythagorean-triplet",
"name": "Pythagorean Triplet",
Expand Down
1 change: 1 addition & 0 deletions exercises/Exercises.slnx
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
<Project Path="practice/pig-latin/PigLatin.vbproj" />
<Project Path="practice/poker/Poker.vbproj" />
<Project Path="practice/prime-factors/PrimeFactors.vbproj" />
<Project Path="practice/prism/Prism.vbproj" />
<Project Path="practice/protein-translation/ProteinTranslation.vbproj" />
<Project Path="practice/proverb/Proverb.vbproj" />
<Project Path="practice/pythagorean-triplet/PythagoreanTriplet.vbproj" />
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.
89 changes: 89 additions & 0 deletions exercises/practice/prism/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
###############################
# 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-2022
; https://docs.microsoft.com/en-us/visualstudio/ide/create-portable-custom-editor-options?view=vs-2022

root = true

[*]
end_of_line = lf
indent_style = space
trim_trailing_whitespace = true
insert_final_newline = true

[*.{vbproj,slnx}]
indent_size = 2

[*.vb]
indent_size = 4
charset = utf-8

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

# Organize imports
dotnet_sort_system_directives_first = true
dotnet_separate_import_directive_groups = true

# Me. 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

###################################
# Visual Basic Coding Conventions #
###################################
visual_basic_preferred_modifier_order = Partial, Default, Private, Protected, Public, Friend, NotOverridable, Overridable, MustOverride, Overloads, Overrides, MustInherit, NotInheritable, Static, Shared, Shadows, ReadOnly, WriteOnly, Dim, Const, WithEvents, Widening, Narrowing, Custom, Async:suggestion
visual_basic_style_unused_variable_expression_statement_preference = unused_local_variable:suggestion
visual_basic_style_unused_value_assignment_preference = unused_local_variable:suggestion
visual_basic_style_prefer_isnot_expression = true:suggestion
visual_basic_style_prefer_simplified_object_creation = false:none
dotnet_diagnostic.IDE0090.severity = none
99 changes: 99 additions & 0 deletions exercises/practice/prism/.meta/Example.vb
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
Public Structure LaserInfo
Public Sub New(ByVal x As Double, ByVal y As Double, ByVal angle As Double)
Me.X = x
Me.Y = y
Me.Angle = angle
End Sub

Public ReadOnly Property X As Double
Public ReadOnly Property Y As Double
Public ReadOnly Property Angle As Double
End Structure

Public Structure PrismInfo
Public Sub New(ByVal id As Integer, ByVal x As Double, ByVal y As Double, ByVal angle As Double)
Me.Id = id
Me.X = x
Me.Y = y
Me.Angle = angle
End Sub

Public ReadOnly Property Id As Integer
Public ReadOnly Property X As Double
Public ReadOnly Property Y As Double
Public ReadOnly Property Angle As Double
End Structure

Public Module Prism
Private Const Epsilon = 0.000001

Public Function FindSequence(ByVal laser As LaserInfo, ByVal prisms As PrismInfo()) As Integer()
Dim x = laser.X
Dim y = laser.Y
Dim angle = laser.Angle
Dim sequence As New List(Of Integer)

While True
Dim prism = FindNextPrism(x, y, angle, prisms)
If Not prism.HasValue Then
Return sequence.ToArray()
End If

Dim hit = prism.Value
sequence.Add(hit.Id)
x = hit.X
y = hit.Y
angle = (angle + hit.Angle) Mod 360.0
End While

Throw New InvalidOperationException("unreachable")
End Function

Private Function FindNextPrism(
ByVal x As Double,
ByVal y As Double,
ByVal angle As Double,
ByVal prisms As PrismInfo()) As PrismInfo?

Dim radians = DegreesToRadians(angle)
Dim directionX = Math.Cos(radians)
Dim directionY = Math.Sin(radians)
Dim nearest As PrismInfo? = Nothing
Dim nearestDistance = Double.PositiveInfinity

For Each prism In prisms
Dim dx = prism.X - x
Dim dy = prism.Y - y
Dim distance = dx * directionX + dy * directionY

If distance > Epsilon AndAlso
IsOnRay(dx, dy, directionX, directionY, distance) AndAlso
distance < nearestDistance Then

nearest = prism
nearestDistance = distance
End If
Next

Return nearest
End Function

Private Function IsOnRay(
ByVal dx As Double,
ByVal dy As Double,
ByVal directionX As Double,
ByVal directionY As Double,
ByVal distance As Double) As Boolean

Dim offsetX = dx - distance * directionX
Dim offsetY = dy - distance * directionY
Dim offsetSquared = offsetX * offsetX + offsetY * offsetY
Dim tolerance = Epsilon * Math.Max(1.0, distance * distance)

Return offsetSquared < tolerance
End Function

Private Function DegreesToRadians(ByVal degrees As Double) As Double
Return degrees * Math.PI / 180.0
End Function
End Module
19 changes: 19 additions & 0 deletions exercises/practice/prism/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"authors": [
"BNAndras"
],
"files": {
"solution": [
"Prism.vb"
],
"test": [
"PrismTests.vb"
],
"example": [
".meta/Example.vb"
]
},
"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"
31 changes: 31 additions & 0 deletions exercises/practice/prism/Prism.vb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
Public Structure LaserInfo
Public Sub New(ByVal x As Double, ByVal y As Double, ByVal angle As Double)
Me.X = x
Me.Y = y
Me.Angle = angle
End Sub

Public ReadOnly Property X As Double
Public ReadOnly Property Y As Double
Public ReadOnly Property Angle As Double
End Structure

Public Structure PrismInfo
Public Sub New(ByVal id As Integer, ByVal x As Double, ByVal y As Double, ByVal angle As Double)
Me.Id = id
Me.X = x
Me.Y = y
Me.Angle = angle
End Sub

Public ReadOnly Property Id As Integer
Public ReadOnly Property X As Double
Public ReadOnly Property Y As Double
Public ReadOnly Property Angle As Double
End Structure

Public Module Prism
Public Function FindSequence(ByVal laser As LaserInfo, ByVal prisms As PrismInfo()) As Integer()
Throw New NotImplementedException("You need to implement this function.")
End Function
End Module
19 changes: 19 additions & 0 deletions exercises/practice/prism/Prism.vbproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<OutputType>Exe</OutputType>
<RestorePackagesWithLockFile>true</RestorePackagesWithLockFile>
</PropertyGroup>

<ItemGroup>
<Import Include="Xunit" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.3.0" />
<PackageReference Include="xunit.v3" Version="3.2.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.5" />
</ItemGroup>

</Project>
Loading
Loading