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 @@ -540,6 +540,14 @@
"prerequisites": [],
"difficulty": 4
},
{
"slug": "prism",
"name": "Prism",
"uuid": "865a0901-4bdf-44c5-a0e9-5b87c104cb9d",
"practices": [],
"prerequisites": [],
"difficulty": 4
},
{
"slug": "roman-numerals",
"name": "Roman Numerals",
Expand Down
36 changes: 36 additions & 0 deletions exercises/practice/prism/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# 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": 3, "x": 30, "y": 10, "angle": 45 },
{ "id": 1, "x": 10, "y": 10, "angle": -90 },
{ "id": 2, "x": 10, "y": 0, "angle": 90 },
{ "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.
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.
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": [
"IsaacG"
],
"files": {
"solution": [
"prism.awk"
],
"test": [
"test-prism.bats"
],
"example": [
".meta/example.awk"
]
},
"blurb": "Calculate the path of a laser through reflective prisms.",
"source": "FraSanga",
"source_url": "https://github.com/exercism/problem-specifications/pull/2625"
}
58 changes: 58 additions & 0 deletions exercises/practice/prism/.meta/example.awk
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# The first line of input is the starting x, y and angle.
# Any remaining input is the prisms: x, y, angle and ID.
# The program is expected to print out prism IDs, space separated on one line.

function abs(n) {
return (n > 0) ? n : -n
}

# Starting position
NR == 1 { x = $1; y = $2; a = $3; }

# Prism positions
NR > 1 { prisms[$1]["x"] = $2; prisms[$1]["y"] = $3; prisms[$1]["a"] = $4; }

# Compute the path
END {
# Loop for hits until no more are found.
deg_to_rad = 3.141592 / 180
do {
found = 0
distance = 0
for (id in prisms) {
# Do not count a prism we just hit.
if (prisms[id]["x"] == x && prisms[id]["y"] == y) {
continue
}
dx = prisms[id]["x"] - x
dy = prisms[id]["y"] - y
r = sqrt(dx * dx + dy * dy)
# How close did we get to the prism?
hx = abs(cos(a * deg_to_rad) * r - dx)
hy = abs(sin(a * deg_to_rad) * r - dy)
if (hx < 0.1 && hy < 0.1) {
# Track the closest prism along the laser path
if (!found || r < distance) {
fid = id
distance = r
}
found = 1
}
}
if (found) {
x = prisms[fid]["x"]
y = prisms[fid]["y"]
a += prisms[fid]["a"]
out[count++] = fid
}
} while (found == 1)

# Print the prisms we hit, space separated IDs
if (count) {
res = out[0]
for (j = 1; j < count; j++) {
res = res " " out[j]
}
print res
}
}
45 changes: 45 additions & 0 deletions exercises/practice/prism/.meta/generate_tests
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/usr/bin/env bash

if (( BASH_VERSINFO[0] < 4 )); then
echo "[Failure] This script requires bash version 4+" >&2
exit 1
fi

if (( $# != 1 )) || [[ ${1##*/} != canonical-data.json ]]; then
echo "Usage: $0 <canonical-data.json for prism>"
exit 1
fi

printf '%s\n' '#!/usr/bin/env bats' 'load bats-extra'

jq -r '
.cases |
# Add an index to each case, similar to Python enumerate()
[foreach .[] as $case ({"index": 0, "case": ""}; {"index": .index + 1, "case": $case})] |
map(
[
"",
"@test \"\(.case.description)\" {",
# Do not skip the first test
if .index == 1 then
" # [[ $BATS_RUN_SKIPPED == \"true\" ]] || skip"
else
" [[ $BATS_RUN_SKIPPED == \"true\" ]] || skip"
end,
" run gawk -f prism.awk <<EOF",
# Start position
(.case.input.start | "\(.x) \(.y) \(.angle)"),
# Prism list
if (.case.input.prisms | length > 0) then
(.case.input.prisms | sort_by(.id) | map("\(.id) \(.x) \(.y) \(.angle)") | join("\n"))
else
empty
end,
"EOF",
" assert_success",
" assert_output \"" + (.case.expected.sequence | join(" ")) + "\"",
"}"
] |
join("\n")
)
| join("\n")' "$1"
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"
Loading